Redis application summary

      First figure out what Redis is? In actual application development, what problems can it help us solve? What kind of problems should be paid attention to in the application to avoid indiscriminate use.

      Redis is actually an in-memory database-noSql database, and data storage is key-value. You can simply think of it as a map structure. Redis is single-threaded, so a single command request operation is thread-safe. If you want to execute multiple commands at a time and require uninterrupted execution, use things (use multi to open, exec to submit). It supports more data structures than memcached, and there are five structures of string\list\hash\set\zset. In application development, we usually use Redis for distributed caching, traffic restriction, concurrent access control, and distributed locks. When used as a distributed cache, the general factor to consider is what data is suitable for caching, and the hit rate of cached data should be considered. If the hit rate is low, the effect of the cache is counterproductive. Cache data should consider the optimization of memory, so that more data can be cached (the internal encoding provided by redis can optimize storage, but other conditions are to follow the principle of internal encoding). Since it is a distributed cache, high availability must be considered. In actual development, there are many high-availability solutions for redis, but redis itself also provides cluster solutions. I prefer the high-availability solution of redis itself, because it is decentralized and caches data shards (which can be cached to different locations according to the key). different slots of the instance). However, there are some points that need to be paid attention to. Because the data is fragmented, when using multiple key operations or set operation commands, the data under different slots cannot be manipulated. It must be ensured that the data in multiple key or set operations are in the same one. Below the slot. So how to put these data under the same slot? The solution is to use hash tags. Our use of redis cluster is nothing more than improving the high availability and load balancing of the system. Therefore, when caching data, in combination with the characteristics of the business itself, try to ensure that the data is evenly distributed on all slots. When using redis, in order to build multiple interactions, we try to use pipeline (this operation has no atomic feature), and submit multiple commands at a time. Of course, if you use a redis cluster, you have to pay attention to whether the data you operate is in the same slot (whether you use hash tag). Sometimes we also use lua script to help us operate redis, why use lua script, because the script can be executed on the server side and ensure the atomicity of the operation, but the script is not suitable for complex business, the business business complexity will occupy When the server resource time is too long and the concurrency is large, it is easy to cause an avalanche.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326012611&siteId=291194637
Recommended