Home > Database > Redis > body text

Share some interview questions about distributed caching in Redis (with answer analysis)

青灯夜游
Release: 2021-04-07 10:50:26
forward
2843 people have browsed it

This article will share with you some interview questions about distributed caching in Redis, including analysis of the answers. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

Share some interview questions about distributed caching in Redis (with answer analysis)

Interview questions


What is the difference between redis and memcached? What is the threading model of redis? Why can redis support high concurrency with a single thread?

Interviewer Psychological Analysis

This is the most basic question when asking redis, the most basic internal part of redis The principle and characteristics are that redis is actually a single-threaded working model. If you don’t know this, then when you play with redis later, wouldn’t you know nothing if something goes wrong?

It is also possible that the interviewer will ask you about the difference between redis and memcached, but memcached was a common caching solution used by major Internet companies in the early years, but now it is basically redis in recent years, and few companies use memcached. . [Related recommendations: Redis video tutorial]

Interview question analysis

What is the difference between redis and memcached ?

redis supports complex data structures

Compared with memcached, redis has more data structures and can support richer data operations. If you need the cache to support more complex structures and operations, redis would be a good choice.

redis natively supports cluster mode

In the redis3. Enter data.

Performance comparison

Since redis only uses single core, while memcached can use multi-core, so on average redis stores small data on each core Higher performance than memcached. For data of more than 100k, memcached performance is higher than redis. Although redis has recently been optimized for big data storage performance, it is still slightly inferior to memcached.

Threading model of redis

redis internally uses file event handler file event handler. This file event handler is single-threaded, so Redis is called a single-threaded model. It uses an IO multiplexing mechanism to listen to multiple sockets at the same time, and pushes the socket that generates the event into the memory queue. The event dispatcher selects the corresponding event processor for processing based on the event type on the socket.

The structure of the file event handler contains 4 parts:

  • Multiple sockets
  • IO multiplexer
  • File event dispatching Device
  • Event processor (connection response processor, command request processor, command reply processor)

Multiple sockets may generate different operations concurrently, and each operation corresponds to Different file events, but the IO multiplexer will monitor multiple sockets and put the sockets that generate events into the queue. The event dispatcher will take out one socket from the queue each time and hand it to the corresponding socket according to the event type of the socket. event handler for processing.

Let’s look at a communication process between the client and redis:

Share some interview questions about distributed caching in Redis (with answer analysis)

You must understand that communication is completed through sockets. Students who don’t understand can go first Take a look at socket network programming.

First of all, when the redis server process is initialized, the AE_READABLE event of the server socket will be associated with the connection response processor.

Client socket01 requests the server socket of the redis process to establish a connection. At this time, the server socket will generate an AE_READABLE event. After the IO multiplexing program monitors the event generated by the server socket, Push the socket into the queue. The file event dispatcher obtains the socket from the queue and hands it to the Connection Response Processor. The connection response handler will create a socket01 that can communicate with the client, and associate the AE_READABLE event of the socket01 with the command request handler.

Assume that the client sends a set key value request. At this time, socket01 in redis will generate the AE_READABLE event, and the IO multiplexer will socket01 Pushed into the queue, the event dispatcher obtains the AE_READABLE event generated by socket01 from the queue. Since the previous AE_READABLE event of socket01 has been associated with the command request processor, the event dispatcher Handle the event to the command request handler for processing. The command request processor reads the key value of socket01 and completes the setting of key value in its own memory. After the operation is completed, it associates socket01's AE_WRITABLE event with the command reply handler.

If the client is ready to receive the return result at this time, then socket01 in redis will generate an AE_WRITABLE event, which is also pushed into the queue. The event dispatcher finds the associated command reply processor. The command reply processor inputs a result of this operation to socket01, such as ok, and then releases the association between socket01's AE_WRITABLE event and the command reply processor.

This completes a communication. Regarding the communication process of Redis, readers are recommended to read "Redis Design and Implementation - Huang Jianhong" for systematic learning.

Why can the redis single-thread model be so efficient?

  • Pure memory operation.
  • The core is based on non-blocking IO multiplexing mechanism.
  • C language implementation. Generally speaking, programs implemented in C language are "closer" to the operating system and the execution speed will be relatively faster.
  • Single thread avoids the problem of frequent context switching of multi-threads and prevents possible competition problems caused by multi-threads.

Interview questions


#What is the concurrency contention issue in edis? how to solve this problem? Do you know the CAS solution for redis transactions?

Interviewer Psychological Analysis

This is also a very common problem online, that isMultiple clients concurrently When writing a key, the data that should have arrived first may arrive later, causing the data version to be wrong; or multiple clients may obtain a key at the same time, modify the value and then write it back. As long as the order is wrong, the data will be wrong.

And redis itself has a CAS-like optimistic locking solution that naturally solves this problem.

Analysis of interview questions

#At a certain time, multiple system instances update a certain key. Distributed locks can be implemented based on zookeeper. Each system obtains distributed locks through zookeeper to ensure that only one system instance can operate a certain key at the same time, and no one else is allowed to read or write.

Share some interview questions about distributed caching in Redis (with answer analysis)

The data you want to write to the cache is all found from mysql and must be written to mysql. When writing to mysql, a timestamp must be saved. , when checking it out from mysql, the timestamp is also checked out.

Every time before writing, first determine whether the timestamp of the current value is newer than the timestamp of the value in the cache. If it is, then it can be written, otherwise, it cannot overwrite the new data with the old data.

For more programming related knowledge, please visit: Programming Video! !

The above is the detailed content of Share some interview questions about distributed caching in Redis (with answer analysis). For more information, please follow other related articles on the PHP Chinese website!

source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template