preamble
Redis is used so frequently in our daily development work that it has become one of the essential technologies.
There are also many scenarios where Redis can be used.
For example: save user login state, do flow limiting, do distributed locking, do caching to improve data access speed, etc.
So the question is, how is Redis performance going to be optimized?
In order to give full play to the performance of Redis, this article with you to talk about Redis performance optimization of 18 strokes, I hope you will be helpful.
1. Choosing the right data structure
Redis supports a variety of data structures, such as strings, hashes, lists, collections, and ordered collections. Choosing the right data structure based on actual needs can improve performance.
If you want to store user information, consider using hashes instead of multiple strings:
("user:1001", "name", "Alice");
("user:1001", "age", "30");
This allows multiple attributes to be stored and accessed efficiently.
2. Avoiding the use of oversized keys and values
Longer keys and values take up more memory and may also affect performance.
Keep the key short and use a concise naming convention.
For example:
Simplify "user:1001:profile" to "u:1001:p".
Other optimizations such as compression can also be done.
For those who are more interested in the big KEY issue, check out my other post,From 2s to 0.1s optimization, I used these 5 steps", which is described in great detail.
3. Using the Redis Pipeline
For batch operations of multiple commands, using Pipeline can significantly reduce network latency and improve performance.
For example, setting keys in bulk can be done like this:
Pipeline p = ();
for (int i = 0; i < 1000; i++) {
("key:" + i, "value:" + i);
}
();
This allows multiple commands to be sent at once, which reduces network round-trip time and can improve performance.
4. Controlling the number of connections
Too many connections can be a waste of resources, using theconnection pool
The number of connections can be managed efficiently.
For example, use JedisPool:
JedisPool pool = new JedisPool("localhost");
try (Jedis jedis = ()) {
("key", "value");
}
With connection pooling, this way connections are reused instead of creating a new connection each time and putting it back into the pool when you're done using it.
Effectively saves connection creation and destruction time.
5. Rationalizing the use of expiration strategies
Setting a reasonable expiration policy prevents memory from being filled up with data that is no longer in use.
For example, cached hotspot data can have an expiration time set.
For example, set an expiration time for session data:
("session:12345", 3600, "data");
Redis internally cleans up expired caches on a regular basis.
6. Using Redis clusters
When the amount of data increases, using Redis clusters can spread the data to multiple nodes and improve concurrent performance.
Data hashes can be sharded to multiple Redis instances.
This avoids the problem of a single Redis instance, with too much data, taking up too much memory.
7. Leverage memory optimization
Choose a suitable memory management policy. Redis supports the LRU (Least Recently Used) policy, which automatically deletes infrequently used data.
For example, configure Redis' maxmemory:
maxmemory 256mb
maxmemory-policy allkeys-lru
8. Use of Lua scripts
Lua scripts allow multiple commands to be executed atomically in Redis, reducing network latency.
For example, use Lua to prevent network latency for multiple commands:
EVAL "('set', KEYS[1], ARGV[1]) return ('get', KEYS[1])" 1 "key" "value"
Using Lua scripts, you can ensure that multiple commands in Redis are atomic operations.
9. Monitoring and tuning
Use the INFO command to monitor Redis performance data such as command support, memory usage, etc. for timely tuning.
For example, use the command to get monitoring information:
INFO memory
INFO clients
10. Avoid hot keys
Hotspot keys can cause stress on a single node, which is avoided by randomizing accesses.
For example, a random suffix can be added to the hotspot key:
String key = "hotkey:" + (() % 10);
(key);
11. Use of compression
When storing large objects, consider using compression techniques to save memory.
For example, you can use theGZIP
Compress JSON data:
byte[] compressed = gzipCompress(jsonString);
("data", compressed);
12. Using the Geo Location feature
Redis supports geolocation storage and querying using theGEOADD
Geographic data can be managed efficiently.
For example, storing location information:
("locations", longitude, latitude, "LocationName");
13. Control of data persistence
rationalizationRDB
cap (a poem)AOF
persistence strategy to avoid performance degradation caused by frequent disk writes.
Example:
Sets the time interval for persistence:
save 900 1
appendonly yes
14. Minimizing the use of transactions
Avoid excessive use of MULTI/EXEC in high concurrency scenarios, as transactions can lock the key.
A single command can be used directly in place of a transaction.
15. Rationalizing the client
Adjust the client's connection timeout and reconnection policy to adapt to high-load scenarios and ensure stable connections.
Example:
JedisPoolConfig poolConfig = new JedisPoolConfig();
(128); // Maximum number of connections
(64); // Maximum free connections
(16); // Minimum free connections
(true); // Minimum free connections
(true);; // Minimum free connections
(true); (true); // minimum free connections
JedisPool jedisPool = new JedisPool(poolConfig, "localhost", 6379, 2000); // Connection timeout 2000ms
16. Using Redis Sentinel
utilizationSentinel
Monitoring is performed to achieve high availability and ensure that the system can be quickly switched over in the event of a failure.
Configure Sentinel for master-slave replication.
17. Optimizing network configuration
Ensure that the Redis server has good network bandwidth to avoid network bottlenecks.
Use a dedicated internal server line to reduce latency.
18. Regular cleansing of unnecessary data
Lifecycle management is critical to keep memory efficiently utilized by regularly deleting expired or unnecessary data.
Can be setCron
Tasks are cleared on a regular basis.
Although Redis internally cleans up expired data, there are some long-standing garbage data that it is also recommended to clean up in a timely manner.
summarize
These are the 18 rules of the road for Redis performance optimization, and the flexible application of these strategies can bring significant performance improvements to your project. I hope this helps you, and feel free to share your optimization experience!
One final note (ask for attention, don't patronize me)
If this article is helpful to you, or if you are inspired, help scan the QR code below to pay attention to it, your support is my biggest motivation to keep writing.
Ask for a one-click trifecta: like, retweet, and watch at.
Concerned about the public number: [Su San said technology], in the public number reply: into the big factory, you can get free access to my recent organization of 100,000 words of the interview dictionary, a lot of partners rely on this dictionary to get a number of big factory offers.