Redis 40 Interview Questions

Date: Oct 25, 2022

Related Tags:1. ApsaraDB for Redis
2. Key-value pair storage

Abstract: In recent years, microservices have become more and more popular, and more and more applications are deployed in distributed environments. One of the commonly used distributed implementations is Redis.

In recent years, microservices have become more and more popular, and more and more applications are deployed in distributed environments. One of the commonly used distributed implementations is Redis.

For programmers who want to change their employers after the year, if they still don't understand Redis, it is easy to make mistakes during the interview. So we have summarized 40 Redis interview questions that may be asked during the interview. Let’s collect them and read them slowly!

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, which can save data in memory on disk, and can be loaded again for use when restarted.
*Redis not only supports simple key-value type data, but also provides storage of data structures such as list, set, zset, and hash.
*Redis supports data backup, that is, data backup in master-slave mode.

1. Advantages of Redis

Extremely high performance - Redis can read at 110,000 times/s and write at 81,000 times/s.

Rich data types - Redis supports Strings, Lists, Hashes, Sets and Ordered Sets data type operations for binary cases.

Atomic – All operations in Redis are atomic, meaning they either succeed or fail at all. A single operation is atomic. Multiple operations also support transactions, i.e. atomicity, wrapped by MULTI and EXEC instructions.

(4) Rich features - Redis also supports publish/subscribe, notification, key expiration and other features.

2. What is the difference between Redis and other key-value stores?

*Redis has more complex data structures 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 traded off when reading and writing different datasets at high speed, because the amount of data cannot be larger than hardware memory. Another advantage in terms of in-memory databases is that it is very simple to operate in memory compared to the same complex data structures on disk, so Redis can do a lot of things with a lot of internal complexity. At the same time, they are compact in terms of on-disk format and are generated appending because they do not require random access.

Redis data type?

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

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

If you say that you have played Redis Module, like BloomFilter, RedisSearch, Redis-ML, the interviewer's eyes will start to light up.

What are the benefits of using Redis?

1. Fast speed, because the data is stored in memory, similar to HashMap, the advantage of HashMap is that the time complexity of search and operation is O1)

2. Support rich data types, support string, list, set, Zset, hash, etc.

3. Transactions are supported, and operations are atomic. The so-called atomicity means that all changes to the data are either executed or not executed at all.

4. Rich features: can be used for cache, message, set expiration time according to key, it will be automatically deleted after expiration

What are the advantages of Redis over Memcached?

1. All values ​​of Memcached are simple strings, and redis, as its replacement, supports richer data classes

2. Redis is much faster than Memcached

3. Redis can persist its data

What are the differences between Memcache and Redis?

1. Storage method Memecache stores all the data in the memory, it will hang up after a power failure, and the data cannot exceed the memory size. Part of Redis is stored on the hard disk, which ensures the persistence of data.

2. Data support type Memcache supports relatively simple data types. Redis has complex data types.

3. Different underlying models are used. The underlying implementation methods between them and the application protocol for communication with the client are different. Redis directly builds the VM mechanism itself, because if the general system calls system functions, it will waste a certain amount of time to move and request.

Is Redis single-process single-threaded?

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

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

Answer: 512M

What is the persistence mechanism of Redis? Advantages and disadvantages of each?

Redis provides two persistence mechanisms RDB and AOF mechanisms:

1. RDBRedis DataBase) persistence method:

Refers to the semi-persistent mode of data set snapshots) to record all key-value pairs of the redis database, write the data to a temporary file at a certain point in time, and replace the last persistent file with this temporary file after the persistence is over. files to achieve data recovery.


(1) There is only one file dump.rdb, which is convenient for persistence.

(2) Good disaster tolerance, a file can be saved to a safe disk.

(3) 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, ensuring 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. Therefore, this method is more suitable when the data requirements are not strict.

2. AOFAppend-only file) persistence method:

It means that all command line records are fully persistently stored in the format of the redis command request protocol) and saved as aof files.


(1) Data security, aof persistence can be configured with appendfsync attribute, there is always, every command operation is recorded in the aof file once.

(2) By writing files in append mode, even if the server goes down in the middle, the data consistency problem can be solved by the redis-check-aof tool.

(3) The rewrite mode of the AOF mechanism. Before the AOF file is rewritten (commands will be merged and rewritten when the file is too large), some of the commands can be deleted (such as the misoperation flushall))


(1) AOF files are larger than RDB files, and the recovery speed is slower.

(2) When the data set is large, the startup efficiency is lower than that of rdb.

Redis common performance issues and solutions

1. It is best for the Master not to write a memory snapshot. If the Master writes a memory snapshot, the save command schedules the rdbSave function, which will block the work of the main thread. When the snapshot is relatively large, the performance impact is very large, and the service will be suspended intermittently.

2. If the data is more important, a Slave enables AOF to back up data, and the policy is set to synchronize every second.

3. For the speed of master-slave replication and the stability of the connection, it is best for the Master and Slave to be in the same LAN

4. Try to avoid adding slaves to the main library under great pressure

5. The master-slave replication does not use a graph structure, but a singly linked list structure is more stable, that is: 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 to be the Master, and the others remain unchanged.

Deletion strategy for redis expired keys?

1. Timed deletion: Create a timer while setting the expiration time of the key. Let the timer delete the key immediately when the expiration time of the key comes.

2. Lazy deletion: Let the key expire regardless, but every time a key is obtained from the key space, check whether the obtained key has expired. If it expires, delete the key; if it does not expire, return the key.

3. Periodic deletion: The program checks the database every once in a while and deletes the expired keys in it. How many expired keys to delete, and how many databases to check, is up to the algorithm.

Redis recycling strategy (elimination strategy)?

volatile-lru: Pick the least recently used data out of the data set (server.db[i].expires) with set expiration time

volatile-ttl: Select the data to be expired from the data set (server.db[i].expires) that has set the expiration time

volatile-random: arbitrarily select data eviction from the dataset with set expiration time (server.db[i].expires)

allkeys-lru: pick least recently used data from dataset (server.db[i].dict) for elimination

allkeys-random: Randomly select data from the dataset (server.db[i].dict) for elimination

no-enviction: prohibit eviction of data

Pay attention to the six mechanisms here, volatile and allkeys specify whether to eliminate data for the data set with an expired time or to eliminate data from all data sets. The latter lru, ttl and random are three different elimination strategies, plus a A no-enviction policy of never recycling.

Use policy rules:

(1) If the data exhibits a power-law distribution, that is, some data have high access frequency and some data have low access frequency, use allkeys-lru

(2) If the data is equally distributed, that is, all data access frequencies are the same, use allkeys-random

Why does edis need to put all data in memory?

A: In order to achieve the fastest read and write speed, Redis reads all data into memory, and writes data to disk in an asynchronous manner. So redis has the characteristics of fast speed and data persistence. If the data is not placed in 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 used memory is set, new values ​​cannot be inserted after the number of existing records of data reaches the memory limit.

Do you understand the synchronization mechanism of Redis?

A: 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 it, 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 replica node for replay, and the synchronization process is completed.

What are the benefits of Pipeline, why use pipeline?

Answer: It is possible to reduce the time of multiple IO round trips to one, provided that there is no causal correlation between the instructions executed by the pipeline. When using redis-benchmark for stress testing, it can be found that an important factor affecting the QPS peak of redis is the number of pipeline batch instructions.

Have you used Redis cluster and what is the principle of clustering?

1. 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.

2. Redis Cluster focuses on scalability. When a single redis memory is insufficient, Cluster is used for sharded storage.

When does the Redis cluster solution make the entire cluster unavailable?

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

What Java clients does Redis support? Which is the official recommendation?

Answer: Redisson, Jedis, le

What are the pros and cons of Jedis vs Redisson?

Answer: Jedis is the client of Redis's Java implementation, and its API provides comprehensive Redis command support; Redisson implements a distributed and scalable Java data structure. Compared with Jedis, its functions are simpler and do not support strings. Operations, Redis features such as sorting, transactions, pipelines, and partitioning are not supported.

The purpose of Redisson is to promote the separation of users' concerns about Redis, so that users can focus more on processing business logic.

How does Redis set and verify passwords?

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

Tell me about the concept of Redis hash slots?

Answer: Redis cluster does not use consistent hashing, but introduces the concept of hash slots. Redis cluster has 16384 hash slots. After each key is checked by CRC16, the modulo of 16384 is used to determine which slot to place. Each node is responsible for a portion of the hash slots.

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

Answer: In order to make the cluster still available even if 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 Redis Cluster lose write operations? Why?

A: 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: 16384

How does the cluster choose the database?

A: Redis cluster currently cannot do database selection, the default is 0 database.

How to test Redis connectivity?

A: Use the ping command.

How to understand Redis transactions?

1. A transaction is a single isolated operation: all commands in a 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.

2. A transaction is an atomic operation: the commands in the transaction are either all 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 do memory optimization?

Answer: Use hash tables as much as possible. A hash table (meaning that the number stored in a hash table is small) uses very little memory, so you should abstract your data model into a hash table as much as possible. For example, if there is a user object in your web system, don't set a separate key for the user's name, surname, email address, and password, but store all the user's information in a hash table.

How does the Redis recycling process work?

A: A client runs a new command, adding new data. Redi checks the memory usage, and if it is greater than the maxmemory limit, it will be recycled according to the set policy. A new command is executed, and so on.

So we keep crossing the boundaries of the memory limit, by constantly hitting the boundaries and then constantly recycling back below the boundaries. If a command results in a large amount of memory being used (such as saving the intersection of a large set to a new key), it doesn't take long for the memory limit to be exceeded by this memory usage.

What are the ways to reduce the memory usage of Redis?

A: If you are using a 32-bit Redis instance, you can make good use of Hash, list, sorted set, set and other 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?

A: If the upper limit is reached, the Redis write command will return an error message (but the read command can still return normally.) Or you can use Redis as a cache to use the configuration elimination mechanism, and when Redis reaches the memory limit, the old ones will be flushed out content.

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

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

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

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

Related knowledge: Redis provides 6 data elimination strategies:

volatile-lru: Pick the least recently used data out of the data set (server.db[i].expires) with set expiration time

volatile-ttl: Select the data to be expired from the data set (server.db[i].expires) that has set the expiration time

volatile-random: arbitrarily select data eviction from the dataset with set expiration time (server.db[i].expires)

allkeys-lru: pick least recently used data from dataset (server.db[i].dict) for elimination

allkeys-random: Randomly select data from the dataset (server.db[i].dict) for elimination

no-enviction: prohibit eviction of data

Where is Redis best suited?

1. Session Cache

One of the most common use cases for Redis is the session cache. The advantage of caching sessions with Redis over other stores 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 do this? Fortunately, as Redis has improved over the years, it's easy to find documentation on how to properly use Redis to cache sessions. Even the well-known commercial platform Magento offers plugins for Redis.

2. Full Page Cache (FPC)

In addition to basic session tokens, Redis also provides a very simple FPC platform. Back to the consistency issue, even if the Redis instance is restarted, because of disk persistence, users will not see a drop in page loading speed. This is a great improvement, similar to PHP's local FPC. Taking Magento as an example again, Magento provides a plugin to use Redis as a full page cache backend. Also, for WordPress users, Pantheon has a very good plugin wp-redis that helps you load the pages you've ever viewed as fast as possible.

3. Queue

One of the great advantages of Reids in the field of in-memory storage engines is that it provides list and set operations, which makes Redis a good message queuing platform. The operations used by Redis as a queue are similar to the push/pop operations on lists in native programming languages ​​such as Python. If you do a quick Google search for "Redis queues", you'll immediately find a plethora of open source projects that aim to use Redis to create great backend tools for various queueing needs. For example, Celery has a backend that uses Redis as the broker, which you can check from here.

4. Leaderboard/Counter

Redis does a great job of incrementing or decrementing numbers in memory. Sets and Sorted Sets also make it very simple for us to perform these operations. Redis just provides these two data structures. So, we want to get the top 10 users from the sorted set - let's call it "user_scores", we just need to do the following: Of course, this assumes you are doing this based on your user's score ascending sort. If you want to return the user and the user's score, you need to do this: ZRANGE

Related Articles

Explore More Special Offers

  1. Short Message Service(SMS) & Mail Service

    50,000 email package starts as low as USD 1.99, 120 short messages start at only USD 1.00

phone Contact Us