1.1. Big Key
The Redis big key problem is not actually a big Key problem, but the value corresponding to the Key is too large, so technically it is a Big Value problem, Redis value is too large (key value is too large).
There is no unified standard for how large a value will cause a big key problem.
For example, for String values, sometimes more than 5M is a big key, sometimes more than 10K can be counted as a Bigey to be on the safe side.
Big Key can lead to what problems?
Because of the large value, serialization and deserialization time is too long, and network latency is also long, resulting in a long time to operate Big Key, which reduces the performance of Redis.
Load balancing is not possible in cluster mode, resulting in load skewing to a certain instance, which results in higher QPS and more memory usage for a single instance.
Since Redis is single-threaded, if you want to perform a delete operation on this Big Key, the instance being operated on may be blocked, thus making it impossible to respond to the request.
How is the Big Key generated?
Generally, the Big Key is generated when the program designer does not anticipate the size of the data, or when design considerations are omitted.
In some business scenarios, it is easy to generate Big Key, for example, the fan list of KOL or traffic stars, the statistics of voting, the cache of large batch data, and so on.
1.2. Hot Key
Hot Key, also known as Hotspot Key, is a problem that occurs when a particular Key suddenly receives a large number of requests, and traffic is concentrated on a particular instance, causing the Redis server to go down because it has reached the physical NIC.
Hotspot Keys can cause many system problems.
The traffic is overly concentrated and cannot take advantage of the cluster, and if it reaches the processing limit of that instance, it causes the node to go down, which in turn impacts the database and risks causing an avalanche of cache and leaving the entire system hanging.
Since Redis is a single-threaded operation, hotkeys can affect the operation of other keys in the example.
2. How to discover BigKey and HotKey
- Query BigKey by Redis command.
The following command scans the entire Key space of Redis for the largest Key of different data types. -i 0.1 parameter allows the command to execute sleep 0.1 seconds every 100 times during the scan.
Redis’ own bigkeys command makes it easy to scan large keys online with minimal impact on service performance, with the single disadvantage of less information and only the largest key of each type.
1 | $ redis-cli -p 999 --bigkeys -i 0.1 |
2.Query BigKey through open source tools.
Using open source tools, the advantage is that the obtained key information is detailed, optional parameters, support for customization needs, easy follow-up processing, the disadvantage is that offline operation is required, and the time to obtain the results is longer.
For example, redis-rdb-tools and so on.
1 | $ cd redis-rdb-tools |
2.2. Discovering HotKeys
- hotkeys parameters
Redis added the hotkeys (github.com/redis/redis…) lookup feature in version 4.0.3, so you can directly use redis-cli –hotkeys to get the hotkeys of the current keyspace, which is achieved by scan + object freq This is done by scan + object freq.
- monitor command
The monitor command can capture the commands received by the Redis server in real time, and capture the data by redis-cli monitor, and combine with some ready-made analysis tools, such as redis-faina, to count the hot keys.
- Solution to BigKey Problem
To find and solve the BigKey problem, you can refer to the following ideas.
At the beginning of the design process, estimate the size of the value, and avoid the appearance of too large value in the business design.
Discover BigKey as early as possible by monitoring.
If it is impossible to avoid large Key, then a Key can be split into multiple parts and stored in different Keys.
The following is an example of List type value to demonstrate the method of splitting to solve the big Key problem.
There is a list of User Id with 10 million data, if all of them are stored under one Key, it will be very large, so you can access the data by splitting it into pages.
1 | public void pushBigKey(List<Long> userIdList) { |
4. HotKey problem solving methods
If HotKey is present, consider the following solutions.
- Use local caching. For example, cache the hot data that needs to be requested at the server, so that you can avoid sending large traffic requests to Redis through load balancing of the server cluster.
However, local caching can introduce data consistency issues and waste server memory.
- HotKey will replicate multiple copies, break them up randomly, and use proxy requests.
1 |
|