What’s the difference between Redis and memcached? What is the threading model of Redis? Why is single-threaded memcached so much more efficient than multi-threaded memcached (and why redis single-threaded can support high concurrency)?

Answer: Redis is different from memcached

Redis supports server-side data operations: Redis supports more data structures and richer data operations than memcached. Redis supports complex data structures and operationsCopy the code

The thread model of Redis

Redis develops a network event handler based on the reactor(Java Nio is a reactor model) pattern, which is called file Event Handler. The file event handler is single-threaded, which is why Redis is called single-threaded. It uses IO multiplexing to listen on multiple sockets at the same time. If the socket being listened on is ready to accept, read, write, or close, the corresponding file event will be generated. The file handler structure consists of four parts: multiple sockets, IO multiplexing routines, file event dispatcher, event handler (command request handler, command reply handler, connection reply handler, etc.). The corresponding multiple sockets generate multiple different operations, each corresponding to a different file event, but the IO multiplexer listens for multiple sockets, but puts them in a queue. When a socket becomes readable (such as a client performing a write operation on Redis or a close operation), or when a new answerable socket appears, the socket will generate an AE_READABLE event. When the socket becomes readable and writable (the client performs the read operation on Redis), the socket generates an AE_WRITABLE event. The IO multiplexer can listen for both AE_REABLE and AE_WRITABLE events. If a socket generates two types of events at the same time, the file dispatcher processes the first (read) event first and the second (write) event second. 3. 4. A process of communication between the client and Redis. When Redis starts initialization, Redis associates the connection response handler with the AE_READABLE event. Then, if a client initiates a connection to Redis, an AE_READABLE event will be generated. The connection reply handler will then connect to the client and create the socket for the client. Also associate the socket AE_READABLE event with the command request handler. When a client makes a request to Redis (whether it is read or written), an AE_READABLE event is first generated on the socket and then processed by the corresponding command request handler. The request handler will read the data from the socket and execute and process it and then redis will associate the AE_WRITABLE event of the socket with the command reply handler when the client is ready to read the data, An AE_WRITABLE event will be generated on the socket, which will be processed by the corresponding command reply handler. That is, the corresponding data will be written to the socket for the client to read the command reply handler. Removes the association between the socket AE_WRITABLE event and the command reply handlerCopy the code

Why is the Redis single-threaded model so efficient

1. Pure memory operation 2. Core IO multiplexing based on non-blocking mechanism 3Copy the code

What data types do Redis have? Which scenarios are appropriate?

(If you ask this question as if you haven’t studied the technology thoroughly, see if you can use it)

1, the basic type of string, ordinary set and get, do simple kv cache 2, hash: A structure similar to map, provided that simple objects (no other objects are nested) are cached in Redis, and then every time a read or write to the cache, You can manipulate a hash field like key= ABC value={"id":12,"age":12}, you can change the age to 20. 5, sortedSet can be sorted to replay, make a leaderboard to write each user and its corresponding score into it. Zadd board score username, then zrevrange board 0 99, can get the top 100 users; Zrank board, username, You can view user rankings in the leaderboard zadd board 85 Zhangsan Zadd board 72 Wangwu Zadd board 96 lisi Zadd board 62 Zhaoliu 96 lisi 85 Zhangsan 72 Wangwu 62 Zhaoliu zrevrange board 0 3 Obtain top 3 users 96 lisi 85 Zhangsan 72 WangwuCopy the code

What are the Redis expiration policies? What are the memory flushing mechanisms? Write an LRU code implementation?

1, Set the expiration time, when set key can give an expiration time, if expired how redis delete this key? A: Delete periodically + lazy delete

Periodic deletion is to randomly select some keys with expiration time set every 100ms. If they expire, they will be deleted. However, there is a problem that some keys will be missed because of random search.

But when you get the key again and you can’t find it, that’s lazy delete, so when you get the key, Redis will check, if the key is expired, it will delete it and it won’t give you anything back.

However, there is another problem with this, if the delete is not deleted, and your system does not check the key, it is left in memory, redis memory will crash. This time will go memory elimination mechanism

If the redis memory usage is too high, this time will be memory flushing

Redis 10 keys, now full, redis will need to delete 5 keys 1 key, last 1 minute 100 times 1 key, last 10 minutes 50 times 1 key, last hour times 1 key 2) allkeys-lru: Remove the least recently used key from the key space when the memory is insufficient to accommodate new writes. Volatile -lru: Removes a random key from the key space when memory is insufficient to accommodate new data writes. When memory is insufficient to accommodate new writes, remove the least recently used key from the expired key space (this is generally not appropriate). 6) volatile- TTL: When the memory is insufficient to accommodate new writes, the key whose expiration time is earlier is removed from the key spaceCopy the code