Knowledge points finishing, Redis interview questions

Knowledge points finishing, Redis interview questions

Write in front

I sorted out some interview questions from major Internet companies. These interview questions are often asked, and they are also some knowledge points that a Java engineer needs to master. After all, the combination of theory and practice is the kingly way. Sort by pieces, and take some knowledge points every day , Happy every day, if it helps you, remember to pay attention and like it .

Related series of articles

Sorting out knowledge points, sorting out knowledge points of MyBatis interview questions,
sorting out knowledge points of ZooKeeper interview questions,
sorting out knowledge points of Dubbo interview questions, sorting out knowledge points of
Elasticsearch interview questions, sorting out knowledge points of
Redis interview
questions, sorting out knowledge points of MySQL
interview questions, sorting out knowledge points of MySQL interview questions , and sorting out Java concurrent programming interviews. Questions (1)
Arrangement of knowledge points, Java concurrent programming interview questions (2)
Arrangement of knowledge points, Java basic interview questions (1)

What is Redis?

Redis is completely open source and free, complies with the BSD protocol, and is a high-performance key-value database. Redis and other key-value caching products have the following three characteristics:

  • Redis supports data persistence. The data in the memory can be saved on the disk, and it can be loaded again for use when restarting.
  • Redis not only supports simple key-value type data, but also provides
    list, set, zset, hash
    And other data structure storage.
  • Redis supports data backup, namely
    Mode data backup.

Redis advantage

Very high performance-Redis can read 110,000 times/s, and write 81,000 times/s. Rich data types-Redis supports binary case

Strings, Lists, Hashes, Sets and Ordered Sets
Data type operations. Atomic-All Redis operations are atomic, which means that they are either executed successfully or not executed at all if they fail. A single operation is atomic. Multiple operations also support transactions, that is, atomicity, packaged by MULTI and EXEC instructions. Rich features-Redis also supports
, Notification, key expiration and other features.

How is Redis different from other key-value stores?

Redis has a more complex data structure and provides atomic operations on them, which is an evolutionary path different from other databases. Redis data types are based on basic data structures and are transparent to programmers without additional abstraction.

Redis runs in memory but can be persisted to disk, so memory needs to be weighed when reading and writing different data sets at high speed, because the amount of data cannot be greater than hardware memory. Another advantage of the in-memory database is that compared to the same complex data structure on disk, it is very simple to operate in memory, so that Redis can do a lot of internally complex things. At the same time, in terms of disk format, they are generated in a compact manner because they do not require random access.

Redis data type?

Redis supports five data types: string (string), hash (hash), list (list), set (collection) and zsetsorted set: ordered collection).

String and hash are commonly used in our actual projects. If you are an advanced user of Redis, you also need to add the following data structures

HyperLogLog, Geo, Pub/Sub

If you say you have played Redis Module, like

BloomFilter, RedisSearch, Redis-ML
, The interviewer's eyes started to shine.

What are the benefits of using Redis?

  • Fast, because the data is stored in memory, similar to HashMap, the advantage of HashMap is that the time complexity of search and operation is O(1)
  • Support rich data types, support
    string, list, set, Zset, hash
  • Support transactions, operations are all atomic. The so-called atomicity means that all changes to the data are executed or not executed at all.
  • Rich features: can be used for cache, message, set expiration time according to key, it will be deleted automatically after expiration

What are the advantages of Redis over Memcached?

  • All values of Memcached are simple strings. Redis, as its replacement, supports richer data types.
  • Redis is much faster than Memcached
  • Redis can persist its data

What are the differences between Memcache and Redis?

  • Storage method
    Store all the data in the memory, it will hang after power failure, and the data cannot exceed the memory size. Part of Redis is stored on the hard disk, which can ensure the durability of the data.
  • Data support type Memcache supports relatively simple data types. Redis has complex data types.
  • The use of the underlying model is different between the underlying implementation methods and the application protocol for communication with the client. Redis directly built the VM mechanism itself, because the general system calls system functions, it will waste a certain amount of time to move and request.

Is Redis single-process single-threaded?

Redis is single-process and single-threaded. Redis uses queue technology to turn concurrent access into serial access, eliminating the overhead of traditional database serial control.

What is the maximum capacity that a string value can store?


What is the persistence mechanism of Redis? Their advantages and disadvantages?

Redis provides two persistence mechanisms: RDB and AOF mechanisms:
(RDBRedis DataBase) Persistence mode : refers to the semi-persistent mode using a snapshot of the data set) records all the key-value pairs of the redis database, and writes the data at a certain point in time Enter a temporary file, and after the end of persistence, replace the last persisted file with this temporary file to achieve data recovery.


  • Only one file
    , To facilitate persistence.
  • Disaster tolerance is good, and a file can be saved to a safe disk.
  • To maximize performance, fork the child process to complete the write operation and let the main process continue to process commands, so IO is maximized. Use a separate sub-process for persistence, the main process will not perform any IO operations, to ensure the high performance of redis) 4. Compared with the large data set, the startup efficiency is higher than that of AOF.


  • Data security is low. RDB is persisted at intervals. If redis fails between persistence, data loss will occur. So this method is more suitable when the data requirements are not rigorous)

AOFAppend-only file) Persistence mode : It means that all command line records are completely persistently stored in the format of the redis command request protocol) and saved as an aof file.


  • Data security, aof persistence can be configured
    Attribute, there is always, every time a command operation is performed, it will be recorded in the aof file once.
  • Write files in append mode, even if the server is down in the middle, you can pass
    Tools to solve the problem of data consistency.
  • The rewrite mode of the AOF mechanism. Before the AOF file is rewrite (when the file is too large, the command will be merged and rewritten), you can delete some of the commands (such as the incorrectly operated flushall))


  • AOF files are larger than RDB files, and the recovery speed is slower.
  • When the data set is large, the startup efficiency is lower than that of rdb.

Redis common performance problems and solutions:

  • It is best not to write memory snapshots for Master. If Master writes memory snapshots, the save command is used to schedule
    Function, will block the work of the main thread, when the snapshot is relatively large, the performance impact will be very large, and the service will be suspended intermittently
  • If the data is more important, a Slave enables AOF to back up the data, and the policy is set to synchronize once per second.
  • For the speed of master-slave replication and the stability of the connection, it is best for Master and Slave to be in the same LAN
  • Try to avoid adding slaves to the stressful master library
  • Do not use graph-like structure for master-slave replication. It is more stable to use a singly linked list structure, namely:
    Master <- Slave1 <- Slave2 <- Slave3...
    This structure is convenient to solve the single point of failure problem and realize the replacement of Slave to Master. If the master hangs up, you can immediately enable Slave1 as the master, and the others remain unchanged.

Redis expired key deletion strategy?

  • Timed deletion: While setting the key expiration time, create a timer timer). Let the timer execute the key deletion operation immediately when the key expiration time comes.
  • Lazy deletion: Let the key expire regardless, but every time you get a key from the key space, check whether the obtained key expires, if it expires, delete the key; if it does not expire, return the key.
  • Periodic deletion: The program checks the database at regular intervals and deletes expired keys. As for how many expired keys to delete and how many databases to check, it is up to the algorithm.

Redis recycling strategy (elimination strategy)?

  • volatile-lru : From a data set that has an expiration time set (
    ) To select the least recently used data to eliminate
  • volatile-ttl : select the data to be expired from the data set (server.db[i].expires) that has set expiration time
  • volatile-random : arbitrarily select data to be eliminated from the data set (server.db[i].expires) for which the expiration time has been set
  • allkeys-lru : select the least recently used data from the data set (server.db[i].dict) to eliminate
  • allkeys-random : arbitrarily select data from the data set (server.db[i].dict) to eliminate
  • no-enviction (eviction): prohibit eviction of data

Pay attention to the six mechanisms here. Volatile and allkeys specify whether to eliminate data from a data set with an expiration time set or to eliminate data from all data sets. The following lru, ttl, and random are three different elimination strategies, plus one Species

The strategy of never recycling.

Use policy rules:

  • If the data exhibits a power-law distribution, that is, part of the data is accessed with high frequency and part of the data is accessed with low frequency, use allkeys-lru
  • If the data is equally distributed, that is, all data is accessed at the same frequency, use

Why does edis need to put all the data in memory?

Answer: In order to achieve the fastest read and write speed, Redis reads the data into the memory and writes the data to the disk in an asynchronous manner. So redis has the characteristics of fast and data persistence. If you don't put the data in the memory, the disk I/O speed will seriously affect the performance of redis. Today, when memory is getting cheaper and cheaper, redis will become more and more popular. If the maximum memory usage is set, new values cannot be inserted after the number of existing data records reaches the memory limit.

Do you understand the synchronization mechanism of Redis?

Answer: Redis can use master-slave synchronization and slave-slave synchronization. During the first synchronization, the master node performs a bgsave and records subsequent modification operations to the memory buffer at the same time. After completion, the RDB file is fully synchronized to the replication node. After the replication node accepts the completion, the RDB image is loaded into the memory. After the loading is completed, the master node is notified to synchronize the operation records modified during the period to the replication node for replay, and the synchronization process is completed.

What are the benefits of pipelines, why use pipelines?

Answer: The time for multiple IO round trips can be reduced to one, provided that there is no causal correlation between the instructions executed by the pipeline. use

When performing stress testing, it can be found that an important factor that affects the peak QPS of redis is the number of pipeline batch instructions.

Have you ever used Redis cluster? What is the principle of cluster?

  • Redis Sentinal 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. When a single redis memory is insufficient, Cluster is used for shard storage.

Under what circumstances will the Redis cluster solution cause the entire cluster to be unavailable?

Answer: A cluster with three nodes A, B, and C. Without a replication model, if node B fails, the entire cluster will think that it lacks slots in the range of 5501-11000 and is unavailable.

What are the Java clients supported by Redis? Which one is the official recommendation?

Answer: Redisson, Jedis, lettuce, etc., Redisson is officially recommended.

What are the advantages and disadvantages of Jedis and Redisson?


It is a client of Redis's Java implementation, and its API provides comprehensive Redis command support;
A distributed and extensible Java data structure is implemented. Compared with Jedis, the function is simpler. It does not support string operations, and does not support Redis features such as sorting, transactions, pipes, and partitions.
The purpose is to promote the separation of concerns from users to Redis, so that users can focus more on processing business logic.

How to set password and verify password in Redis?

  • set password:
    config set requirepass 123456
  • Authorization password:
    auth 123456

Talk about the concept of Redis hash slot?

Answer: Redis cluster does not use consistent hash, but introduces the concept of hash slot. Redis cluster has 16384 hash slots. After CRC16 check, each key modulates 16384 to determine which slot to place. Each node is responsible for part of the hash slot.

What is the master-slave replication model of Redis cluster?

Answer: In order to make the cluster still available when some nodes fail or most nodes cannot communicate, the cluster uses a master-slave replication model, and each node will have N-1 replicas.

Will there be write loss in Redis cluster? why?

Answer: Redis does not guarantee strong data consistency, which means that in practice, the cluster may lose write operations under certain conditions.

How is replication between Redis clusters?

Answer: Asynchronous replication

What is the maximum number of nodes in a Redis cluster?

Answer: 16,384.

How to choose a database for Redis cluster?

Answer: Redis cluster currently cannot be used for database selection, the default is 0 database.

How to test the connectivity of Redis?

Answer: Use the ping command.

How to understand Redis transaction?

  • The transaction is a separate isolated operation: all commands in the transaction are serialized and executed sequentially. During the execution of the transaction, it will not be interrupted by the command request sent by other clients.
  • A transaction is an atomic operation: either all commands in the transaction are executed, or none of them are executed.

What are the commands related to Redis transactions?


How to set the expiration time and permanent validity of Redis key?

Answer: EXPIRE and PERSIST commands.

How does Redis optimize memory?

Answer: Use hash tables as much as possible. The memory used by hash tables (which means that the number stored in a hash table is small) is very small, so you should abstract your data model into a hash table as much as possible. For example, if you have a user object in your web system, do not set a separate key for the user's name, surname, email, and password, but store all the user's information in a hash table.

How does the Redis recycling process work?

Answer: A client ran a new command and added new data. Redi checks the memory usage, if it is greater than

Recycling is carried out according to the set strategy. A new command is executed, and so on. So we continue to cross the boundary of the memory limit, by continuously reaching the boundary and then continuously reclaiming back below the boundary. If the result of a command causes a large amount of memory to be used (for example, the intersection of a large set is saved to a new key), it will not take long for the memory limit to be exceeded by this memory usage.

Are there any ways to reduce the memory usage of Redis?

Answer: If you are using a 32-bit Redis instance, you can make good use of it

Hash,list,sorted set,set
Wait for collection type data, because usually many small Key-Values can be stored together in a more compact way.

What happens when Redis runs out of memory?

Answer: If the set upper limit is reached, Redis write commands will return an error message (but read commands can also return normally.) Or you can use Redis as a cache to use the configuration elimination mechanism. When Redis reaches the memory limit, it will flush out the old ones. content.

How many keys can a Redis instance store at most? How many elements can be stored in List, Set, Sorted Set?

Answer: In theory, Redis can handle up to 232 keys, and has been tested in practice. Each instance stores at least 250 million keys. We are testing some larger values. Any list, set, and sorted set can contain 232 elements. In other words, the storage limit of Redis is the available memory value in the system.

There are 2000w data in MySQL and only 20w data in redis. How to ensure that the data in redis is all hot data?

Answer: When the size of the Redis memory data set rises to a certain size, a data elimination strategy will be implemented.

Related knowledge: Redis provides 6 data elimination strategies:

  • volatile-lru : From a data set that has an expiration time set (
    ) To select the least recently used data to eliminate
  • volatile-ttl : From the data set that has set expiration time (
    ) To select the data that will expire
  • volatile-random : arbitrarily select data to be eliminated from the data set (server.db[i].expires) for which the expiration time has been set
  • allkeys-lru : From the data set (
    ) To select the least recently used data to eliminate
  • allkeys-random : From the data set (
    ) Arbitrarily select data elimination
  • no-enviction (eviction): prohibit eviction of data

The most suitable scenario for Redis?

  • Session Cache The
    most commonly used scenario for using Redis is the session cache. The advantage of using Redis to cache sessions over other storage (such as Memcached) is that Redis provides persistence. When maintaining a cache that does not strictly require consistency, most people will be unhappy if all the user's shopping cart information is lost. Now, will they still be like this? Fortunately, as Redis has improved over the years, it is easy to find out how to properly use Redis to cache session documents. Even Magento, a well-known commercial platform, provides plugins for Redis.
  • Full page cache (FPC)
    In addition to the basic session token, Redis also provides a very simple FPC platform. Going back to the consistency problem, even if the Redis instance is restarted, users will not see the page load speed drop because of disk persistence. This is a great improvement, similar to the PHP local FPC. Taking Magento as an example again, Magento provides a plug-in to use Redis as a full-page cache backend. In addition, for WordPress users, Pantheon has a very good plugin wp-redis, which can help you load the pages you have browsed as quickly as possible.

  • One of the advantages of queue Reids in the field of memory storage engines is to provide list and set operations, which makes Redis a good message queue platform to use. The operation used by Redis as a queue is similar to the push/pop operation of a local programming language (such as Python) on a list. If you quickly search for "Redis queues" in Google, you will immediately find a large number of open source projects. The purpose of these projects is to use Redis to create very good back-end tools to meet various queue needs. For example, Celery has a backend that uses Redis as a broker. You can check it from here.
  • The leaderboard/counter
    Redis implements the operation of incrementing or decrementing numbers in memory very well. Set and Sorted Set also make it very easy for us to perform these operations. Redis just provides these two data structures. So, we need to get the top 10 users from the sorted set-we call it "user_scores", we just need to do the following: Of course, this assumes that you are based on the scores of your users Ascending sort. If you want to return users and their scores, you need to do this:
    ZRANGE user_scores 0 10 WITHSCORES Agora Games
    It is a good example, implemented in Ruby. Its leaderboard uses Redis to store data. You can see it here.
    5. Publish/Subscribe
    Finally (but certainly not the least important) is the publish/subscribe feature of Redis. There are indeed many usage scenarios for publish/subscribe. I have seen people use it in social network connections, can also be used as a script trigger based on publish/subscribe, and even use the publish/subscribe function of Redis to build a chat system!

If there are 100 million keys in Redis, 10w of them start with a fixed, known prefix. What if you can find them all?

Answer: Use the keys command to scan out the key list of the specified mode.

The other party then asked: If this redis is providing services to online businesses, what are the problems with using the keys command?

At this time, you have to answer a key feature of redis: redis's single-threaded. The keys instruction will cause the thread to block for a period of time, and the online service will be paused. The service cannot be restored until the instruction is executed. At this time, you can use the scan command. The scan command can extract the key list of the specified mode without blocking, but there will be a certain probability of repetition. It is enough to do a deduplication on the client side, but the overall time will be more than direct The keys instruction is long.

If there are a large number of keys that need to be set to expire at the same time, what should I pay attention to?

Answer: If the expiration time of a large number of keys is set too concentrated, redis may have a short period of time when it expires. Generally, it is necessary to add a random value to the time, so that the expiration time is scattered.

Have you used Redis as an asynchronous queue? How do you use it?

Answer: Generally, the list structure is used as a queue, rpush produces messages, and lpop consumes messages. When there is no message from lpop, you need to sleep for a while and try again.

If the other party asks, can I not sleep?

The list also has an instruction called blpop. When there is no message, it will block until the message arrives. If the other party asks whether it can produce once and consume many times? Using the pub/sub topic subscriber model, a 1:N message queue can be realized.

If the other party asks what are the disadvantages of pub/sub?

When the consumer goes offline, the produced message will be lost, and a professional message queue such as RabbitMQ must be used.

If the other party asks how redis implements the delay queue?

I guess now you really want to beat the interviewer to death. If you have a baseball bat in your hand, how can you ask in such detail? But you are restrained, and then replied calmly: Use sortedset, use the timestamp as the score, and the message content as the key to call zadd to produce the message, and the consumer uses

The instruction fetches the data polled for N seconds before for processing. At this point, the interviewer secretly gave you a thumbs up. But what he didn't know was that you raised your middle finger at the moment, behind the chair.

Have you ever used Redis distributed locks? What is it about?

First use setnx 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 it.

At this time, the other party will tell you that you answered well, and then ask if the process crashes unexpectedly or restarts maintenance after executing expire after setnx, what will happen?

At this time, you have to give surprising feedback: Oh, yes, this lock will never be released. Then you need to grab your own head, pretending to think for a while, as if the next result is your active thinking, and then answer: I remember the set command has very complicated parameters, this should be able to setnx and expire at the same time Synthesize an instruction to use! The other party will show a smile at this time, and begin to mute in his heart: Press, this kid is not bad.