Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

Best scenario — small volume, hot data

Redis provides a cache system with higher performance and better availability due to its use of pure memory storage. So, in terms of cost, resource utilization, and caching system features, Squirrel is more suitable for storing hot data — that is, frequently accessed data can be stored in Redis. We hope that the vast majority of data in Redis will be accessed within a day, and it will be a waste to store a large amount of data that will not be accessed for more than a few days in Redis.

Best practices and pits to be aware of

Choose the right data structure

Redis natively supports String, Hash, Set, SortedSet, List and other data structures to support complex scenarios. Introduction to Redis data structures

Avoid large values and hot keys

For example, the number of String values exceeds 512KB, and the number of Hash, Set, Zset, and List elements exceeds 1W

The hotspot key may cause heavy load on the CPU and network. It is recommended that services downgrade hotspot keys

The number of keys in a cluster is less than 100 million

Too many keys will consume a lot of extra memory

Proactively update the cache

Passive updates can put pressure on DB. It is recommended that keys in the cache be updated by subscribes to database changes or other means

Focus on Cluster Memory

The presence of full load conditions can have a significant performance impact on a business. It is recommended to optimize the cluster memory usage and contact the DBA for capacity expansion.

Set a reasonable expiration time

1. The expiration time of a key should be as short as possible to reduce memory usage. 2. Avoid a large number of keys that expire at the same time

Speed up using multiGet or pipeline

Using multiGet or Pipeline can combine multiple cache operations into a single network request, greatly reducing network overhead.

Avoid complex operations

The number of keys in batch operations such as multi and Pipeline should not be too large, and the complexity of O(N) set operations, such as Hgetall, should be avoided.

Multi pipeline hGETALL How to choose? If the number of keys is less than 100, you are advised to save them in hash. Obtaining hash keys in batches reduces network I/OS (the difference between obtaining one key and obtaining multiple keys).

Don’t overuse

For example, do not use lists for message queues. Redis does not guarantee the reliability of these features. Those with such requirements can use other components of the architecture, and professional work is done by professional components