Redis face-to-face 2

6. Redis cache penetration and breakdown

Cache penetration refers to data that neither the cache nor the database has, and users (hackers) continue to initiate requests. For example: the id of our database is always incremented from 1. If data with id=-1 or data with a particularly large id does not exist, such continuous attacks will cause a lot of pressure on the database and seriously destroy the database;

Cache breakdown means that a certain key is very hot, and it is constantly carrying a large number of requests to access this point intensively. When the key fails at the moment, the continuous large concurrency directly falls on the database. The cache is broken down at the point of this Key. The cache avalanche is due to large-scale cache failure.

Solution:

  1. Cache centralization can add verification at the interface layer, such as user authentication, parameter verification, and direct return for illegal verification, such as basic verification of id, and direct interception of id<=0;

  2. "Bloom Filter" can prevent the penetration of the affected side very well. It uses efficient data structures and algorithms to quickly judge whether your Key exists in the database. If there is no return, it will be fine. If it exists, check the DB to refresh the KV and then return;

  3. If the cache breaks down, set the hotspot data to never expire, and add a mutex to get it done.

7. What data structures does Redis have?

String : general key/value cache application; general count: number of microblogs, number of fans, etc. Hash : hash is especially suitable for storing objects. User information, product information. List : Weibo's follow list, follower list, message list. You can use the Irang command to implement paging queries. Set : The function provided by set to the outside world is similar to that of list, which is a list function. The special feature is that set can be automatically sorted. SortedSet : In the live broadcast system, the real-time ranking information includes the list of online users in the live broadcast room, various gift rankings, message barrage and other information. It is suitable to use the sorted set structure in Redis for storage. HyperLogLog : It is an algorithm used for cardinality statistics. The advantage is that when the number or volume of input elements is very, very large, the space required to compute the cardinality is always fixed and small. Because HyperLogLog will only calculate the cardinality based on the input elements, and will not store the input elements themselves. Probability calculation Geo : Redis GEO is mainly used to store geographical location information and operate on the stored information Pub/SubRedis : Redis publish subscription (pub/sub) is a message communication mode: sender (pub) sends messages, subscribers (sub) Receive a message. A Redis client can subscribe to any number of channels.

8. redis distributed lock

First get setnx (set if not exists) to compete for the lock, and then use expire to add an expiration time to the lock to prevent the lock from forgetting to release.

9. There are a large number of keys that need to be set to expire at the same time. What should you pay attention to?

If there are a large number of keys whose expiration time is set too centrally, redis may experience a short lag when it expires. If it is serious, there will be a cache avalanche . We generally need to add a random value to a time, so that the expiration time seems to be scattered.

10. Have you ever used a Redis cluster, how to ensure the high availability of the cluster, and what is the principle of the cluster?

Redis Sentinal (Sentinel) focuses on high availability. When the master is down, it will automatically promote the slave to the master and continue to provide services. Redis Cluster focuses on scalability, and uses Cluster for shard storage when a single redis memory is insufficient.

11. Do you understand the threading model of Redis?

Redis uses the file event handler file event handler internally. This file event handler is single-threaded, so Redis is called a single-threaded model. It uses the IO multiplexing mechanism to monitor multiple Sockets at the same time, and selects the corresponding event handler for processing according to the events on the Socket.

The structure of a file event handler consists of 4 parts:

  • Multiple Sockets

  • IO multiplexer

  • file event dispatcher

  • Event handlers (connection response handlers, command request handlers, command reply handlers)

Multiple Sockets may generate different operations concurrently, and each operation corresponds to a different file event, but the IO multiplexing program will listen to multiple Sockets, and will put the events generated by the Socket into the queue, and the event dispatcher will Take an event from the queue and hand the event to the corresponding event handler for processing.

12. How to solve the cache consistency

There are two ways to delete the cache:

Delete the cache first, then update the database. The solution is to use delayed double delete. Update the database first, then delete the cache. The solution is to synchronize message queues or other binlogs. The introduction of message queues will cause more problems, and it is not recommended to use them directly.

13. What is the difference between redis and memcached? Why is sometimes single-threaded redis more efficient than multi-threaded memcached under high concurrency?

the difference:

  1. mc can cache pictures and videos. rd supports more data structures than k/v;

  2. rd can use virtual memory, rd can be persistent and aof disaster recovery, rd supports data backup through master-slave; 3. rd can be used as a message queue.

Reason: The mc multi-threading model introduces cache consistency and locks, and locking brings performance loss.

Guess you like

Origin blog.csdn.net/slave_of_life/article/details/130667767