Table of Contents
Irregular phenomena in the use of Redis
Redis usage business Scenario recommendations and suggestions
How to design an elegant key
1. Follow the following best practice conventions
2. Try to avoid bigkey
3. Use appropriate data types
Recommendations for using Redis cache in practical applications
Use business specifications
Home Database Redis What is the method used in Redis key-value design?

What is the method used in Redis key-value design?

May 28, 2023 pm 04:44 PM
redis

    Irregular phenomena in the use of Redis

    • The key names stored in Redis are not standardized and are more arbitrary;

    • Redis is used as a repository, there is a risk of data loss, and there is no reloading plan;

    • Redis cache key, no expiration time is set, and the cache takes up low-frequency data A large amount of memory, which in turn causes the service to crash;

    • Redis caches a large number of big keys, which will occupy a large amount of network bandwidth when the application is obtained, and deletion can also easily cause congestion;

    • Improper use of the Redis client causes other client connection timeouts. The reason may be that the client password is incorrect and the connection pool is not used. A large number of connection retries lead to the exhaustion of system port resources;

    • Improper use of Redis client commands leads to a large number of slow queries and affects other application services, such as using keys* or flushall commands during peak business periods;

    Redis usage business Scenario recommendations and suggestions

    • High concurrency scenarios: hotspot data caching can improve the overall system response speed and reduce database IO pressure;

    • Limited time scenarios : Use the Redis expire command to set session expiration and renewal, mobile phone verification code, etc.;

    • Using Redis's list and ordered set data structures can implement a variety of complex ranking applications

    • Data set operations: Use Redis list, set, sorted set to facilitate data calculations, such as intersection, union, difference, etc.;

    • Continuous sign-in: You can use the bitmap data structure of redis to implement sign-in related services;

    • Counter: Use Redis incr and incrby commands to implement api call count statistics, api current limiting and other scenarios;

    • Distributed lock: Use the setnx function of Redis to write distributed locks, typical open source components such as redisson;

    How to design an elegant key

    It can be said that when it comes to redis performance optimization online, unreasonable key design is often the root cause of the problem. In essence, from what I have seen personally, most When students use redis, they have almost no concept of key design, because the scenario most students use is key/val, and the corresponding data structure is string key/string val;

    Students who have a deeper understanding of redis may know that the key design should be as short as possible when storing, and it is best to have a sense of hierarchy in the middle. It is best to divide it with:...

    So how can we design a more elegant key? Let’s talk about it in detail based on the editor’s actual experience in use and the pitfalls that have been encountered;

    1. Follow the following best practice conventions

    1. Follow the basics Format: [Business Name]:[Data Name]:[id];

    2. The length of the key shall not exceed 44 bytes;

    3. No Contains special characters;

    Regarding the above suggestions, this has the following advantages:

    • It is highly readable, such as when we Design such a key structure, order:user:10. At a glance, you can tell that this is a key related to user orders;

    • is convenient for maintenance and management, different applications, or different businesses Using different prefixes makes it easy to find and locate keys in visual client tools or command lines;

    • avoid key conflicts and avoid multiple people using userId during use. Cache key conflict caused by value as key;

    • Use string type as key, and the underlying encoding includes int, embstr and raw, which can effectively reduce memory usage. Using embstr can process strings smaller than 44 bytes with smaller memory usage because it uses continuous memory space

    Recommended value:

    • The value of a single key is less than 10KB;

    • For collection type keys, it is recommended that the number of elements be less than 1000;

    2. Try to avoid bigkey

    1. What is bigkey

    BigKey is usually judged comprehensively based on the size of the Key and the number of members in the Key, for example :

    • The data volume of Key itself is too large: a Key of String type, its value is 5 MB;

    • Members in Key Too many numbers: A ZSET type Key, its number of members is 10,000;

    • The number of members in the Key is too large: A Hash type Key, its number of members is although There are only 1,000 but the total value size of these members is 100 MB;

    2. The dangers of BigKey

    Network Blocking

    • When executing a read request for BigKey, a small amount of QPS may cause the bandwidth usage to be full, causing the Redis instance and even the physical machine where it is located to slow down;

    Data skew

    • The memory usage of the Redis instance where BigKey is located is much higher than that of other instances, and the memory resources of the data sharding cannot be reached. Balance;

    Redis blocking

    • Computing hash, list, zset, etc. with many elements will take a long time and cause the main thread to be blocked;

    CPU Pressure

    • Data serialization and deserialization of BigKey will cause CPU usage to soar, affecting Redis instances and other local applications;

    3. How to discover BigKey

    Execute the redis-cli --bigkeys command on the installed machine

    • Using the --bigkeys parameter provided by redis-cli, you can traverse and analyze all keys, and return the overall statistical information of the Key and the top 1 big key of each data;

    Scan by scan

    • Write a program, use scan to scan all keys in Redis, and use strlen, hlen and other commands to determine the length of the key (MEMORY USAGE is not recommended here) );

    Use third-party tools

    • Use third-party tools, such as Redis-Rdb-Tools to analyze RDB snapshots file to comprehensively analyze memory usage;

    Use network monitoring

    • Custom tools to monitor the network in and out of Redis Data will proactively alarm when it exceeds the warning value;

    3. Use appropriate data types

    As mentioned above, many students who are using redis for the first time have many business scenarios. , all are done with a simple structure of key/val, without thinking deeply about whether it is reasonable to do so, or whether it will cause related performance problems in the future;

    Regarding this issue, from Fundamentally speaking, we need to deeply understand and master the commonly used data types of redis. On this basis, we can design efficient storage structure data for different business scenarios;

    Let us think about how to cache What about data like a list of user objects?

    • Option 1: key is usrId, value is the serialized string of the object, and the data structure is similar to the following;

    What is the method used in Redis key-value design?

    Advantages: Convenient access, simple and rough, you only need to convert json and objects when accessing;

    Disadvantages: Data coupling, not flexible enough, once the object adds new fields Or the fields are deleted, and the cost of cache reconstruction is very high;

    • Option 2: Use a list structure to cache the user ID list, the data structure is as follows;

    What is the method used in Redis key-value design?

    Advantages: small memory usage and efficient operation;

    Disadvantage: after obtaining val, further database search is required to obtain the complete object;

    Option 3: Use hash structure, cache objects, and the data is as follows;

    What is the method used in Redis key-value design?

    Advantages: The bottom layer uses ziplist, which takes up little space and can flexibly access any field of the object. ;

    Disadvantages: Relatively complex coding;

    Recommendations for using Redis cache in practical applications

    • [Recommended] Warm up the cache. Before accessing data, the cache should be preheated to avoid a large number of requests directly entering the data storage layer; appropriate hot and cold data should be divided according to business conditions, and hot data should be preheated. Such as authorization information, apikey, etc.;

    • [Recommended] Use local cache together. In a distributed architecture, although local caching can improve the stability and speed of data access, it needs to be used with caution to avoid introducing stateful server nodes. Avoid local caches from excessively occupying application server resources and causing application node crashes

    • [Recommended] Cache change strategy, the database should be updated first, and then the cache;

    • [Recommendation] A business call needs to access multiple redis servers, and pipeline or other batch operation methods can be used;

    • [Recommendation] Large List, Set, Hash, storage The quantity is huge. When fetching a large number of elements, a large delay will occur, blocking the execution of other commands. It is recommended to split it into multiple small lists, sets or hash tables

    Use business specifications

    Whether it is redis or other intermediates used in development When developing and using software, it is best to formulate a set of reasonable specifications in advance. This specification should be recognized by most developers and tested in practice, and can effectively avoid some problems. Once designated as a specification, It should become a daily rule to guide internal developers. Here are the following points:

    • Redis should be positioned as cache data and cannot be used to store large-scale data (cannot replace the database);

    • Redis is suitable for scenarios where there is more reading and less writing. If there are high-frequency writes and low-frequency query scenarios, it is not recommended;

    • When the key is not sure When it comes to the survival time, it is best to set the expiration time to control the life cycle of the key;

    • Separation of hot and cold data should be considered. For queries, use Redis for high-frequency business queries, and consider using the database for low-frequency queries;

    • When the program processes data, It should be considered that Redis has the risk of data loss, so it is necessary to automatically load and cache lost data from the database to Redis

    • Use O(N) commands with caution, such as list, set, hash data When performing structure operations, hgetall, lrange, smembers, zrange, etc. are not unusable. Priority is given to using hscan, sscan, and zscan instead.

    The above is the detailed content of What is the method used in Redis key-value design?. For more information, please follow other related articles on the PHP Chinese website!

    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    Video Face Swap

    Video Face Swap

    Swap faces in any video effortlessly with our completely free AI face swap tool!

    Hot Tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    How to build the redis cluster mode How to build the redis cluster mode Apr 10, 2025 pm 10:15 PM

    Redis cluster mode deploys Redis instances to multiple servers through sharding, improving scalability and availability. The construction steps are as follows: Create odd Redis instances with different ports; Create 3 sentinel instances, monitor Redis instances and failover; configure sentinel configuration files, add monitoring Redis instance information and failover settings; configure Redis instance configuration files, enable cluster mode and specify the cluster information file path; create nodes.conf file, containing information of each Redis instance; start the cluster, execute the create command to create a cluster and specify the number of replicas; log in to the cluster to execute the CLUSTER INFO command to verify the cluster status; make

    How to clear redis data How to clear redis data Apr 10, 2025 pm 10:06 PM

    How to clear Redis data: Use the FLUSHALL command to clear all key values. Use the FLUSHDB command to clear the key value of the currently selected database. Use SELECT to switch databases, and then use FLUSHDB to clear multiple databases. Use the DEL command to delete a specific key. Use the redis-cli tool to clear the data.

    How to read redis queue How to read redis queue Apr 10, 2025 pm 10:12 PM

    To read a queue from Redis, you need to get the queue name, read the elements using the LPOP command, and process the empty queue. The specific steps are as follows: Get the queue name: name it with the prefix of "queue:" such as "queue:my-queue". Use the LPOP command: Eject the element from the head of the queue and return its value, such as LPOP queue:my-queue. Processing empty queues: If the queue is empty, LPOP returns nil, and you can check whether the queue exists before reading the element.

    How to use the redis command How to use the redis command Apr 10, 2025 pm 08:45 PM

    Using the Redis directive requires the following steps: Open the Redis client. Enter the command (verb key value). Provides the required parameters (varies from instruction to instruction). Press Enter to execute the command. Redis returns a response indicating the result of the operation (usually OK or -ERR).

    How to use redis lock How to use redis lock Apr 10, 2025 pm 08:39 PM

    Using Redis to lock operations requires obtaining the lock through the SETNX command, and then using the EXPIRE command to set the expiration time. The specific steps are: (1) Use the SETNX command to try to set a key-value pair; (2) Use the EXPIRE command to set the expiration time for the lock; (3) Use the DEL command to delete the lock when the lock is no longer needed.

    How to read the source code of redis How to read the source code of redis Apr 10, 2025 pm 08:27 PM

    The best way to understand Redis source code is to go step by step: get familiar with the basics of Redis. Select a specific module or function as the starting point. Start with the entry point of the module or function and view the code line by line. View the code through the function call chain. Be familiar with the underlying data structures used by Redis. Identify the algorithm used by Redis.

    How to solve data loss with redis How to solve data loss with redis Apr 10, 2025 pm 08:24 PM

    Redis data loss causes include memory failures, power outages, human errors, and hardware failures. The solutions are: 1. Store data to disk with RDB or AOF persistence; 2. Copy to multiple servers for high availability; 3. HA with Redis Sentinel or Redis Cluster; 4. Create snapshots to back up data; 5. Implement best practices such as persistence, replication, snapshots, monitoring, and security measures.

    How to use the redis command line How to use the redis command line Apr 10, 2025 pm 10:18 PM

    Use the Redis command line tool (redis-cli) to manage and operate Redis through the following steps: Connect to the server, specify the address and port. Send commands to the server using the command name and parameters. Use the HELP command to view help information for a specific command. Use the QUIT command to exit the command line tool.

    See all articles