Technology Sharing

Redis operation and maintenance interview questions

2024-07-11

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

In order to assist you on your interview journey, for those students who are still unsure about Redis, we have compiled 40 common Redis interview questions to help you stay calm during the interview and get as many offers as possible!

1. What is Redis?

Redis is completely open source and free, complies with the BSD protocol, and is a high-performance key-value database.

Compared with other key-value cache products, Redis has the following three characteristics:

  • Redis supports data persistence. It can save the data in memory to the disk and load it again for use when restarting.

  • Redis not only supports simple key-value data, but also provides storage for data structures such as list, set, zset, and hash.

  • Redis supports data backup, that is, data backup in master-slave mode.

Redis advantages:

  • Very high performance: Redis can read at a speed of 110,000 times/s and write at a speed of 81,000 times/s.

  • Rich data types: Redis supports Strings, Lists, Hashes, Sets and Ordered Sets data type operations in binary cases.

  • Atomic: All Redis operations are atomic, meaning they either succeed or fail and are not executed at all. Single operations are atomic. Multiple operations also support transactions, i.e. atomicity, wrapped up in MULTI and EXEC instructions.

  • Rich features: Redis also supports publish/subscribe, notification, key expiration and other features.

How is Redis different from 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 the need for additional abstraction.

Redis runs in memory but can persist to disk, so there is a trade-off between high-speed read and write of different data sets, because the amount of data cannot be larger than the hardware memory. Another advantage of in-memory databases is that compared to the same complex data structures on disk, it is very simple to operate in memory, so Redis can do a lot of things that are very complex internally. At the same time, in terms of disk formats, they are compact and generated in an append-only manner, because they do not need random access.

2. What are the data types of Redis?

Redis supports five data types: string, hash, list, set, and sorted set.

In our actual projects, string and hash are more commonly used. If you are an intermediate or advanced user of Redis, you also need to add the following data structures: HyperLogLog, Geo, and Pub/Sub.

If you say that you have played with Redis Modules, such as BloomFilter, RedisSearch, and Redis-ML, the interviewer's eyes will start to light up.

3. 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 O1)

  • Supports rich data types, including string, list, set, Zset, hash, etc.

  • Supports transactions, and all operations are atomic. The so-called atomicity means that all changes to data are either executed or not executed.

  • Rich features, can be used for caching, messages, set expiration time by key, and will be automatically deleted after expiration

4. What are the advantages of Redis over Memcached?

  • All Memcached values ​​are simple strings. Redis, as its replacement, supports richer data types.

  • Redis is much faster than Memcached

  • Redis can persist its data

5. What are the differences between Memcache and Redis?

  • Memecache stores all data in memory, which will crash after a power outage. Data cannot exceed the memory size. Redis stores part of the data on the hard disk, which ensures data persistence.

  • Data Support Types Memcache supports relatively simple data types. Redis has complex data types.

  • Different underlying models are used, and the underlying implementation methods and application protocols for communication with clients are different. Redis directly builds a VM mechanism by itself, because if the general system calls system functions, it will waste a certain amount of time to move and request.

6. Is Redis a single-process, single-threaded system?

Redis is a single-process, single-threaded database. Redis uses queue technology to convert concurrent access into serial access, eliminating the overhead of serial control in traditional databases.

7. What is the maximum capacity of a string type smart storage?

512M。

8. What is the persistence mechanism of Redis? What are their respective advantages and disadvantages?

Redis provides two persistence mechanisms: RDB and AOF:

RDB (Redis DataBase) persistence mode: refers to recording all key-value pairs of the Redis database in a semi-persistent mode using a dataset snapshot, writing the data to a temporary file at a certain point in time, and after the persistence is completed, using this temporary file to replace the last persistent file to achieve data recovery.

advantage:

  • There is only one file dump.rdb, which is convenient for persistence.

  • Good disaster recovery, a file can be saved to a safe disk.

  • Maximize performance, fork child process to complete write operation, let the main process continue to process commands, so IO is maximized. Use a separate child process for persistence, the main process will not perform any IO operation, to ensure the high performance of Redis.

  • When the data set is large, the startup efficiency is higher than AOF.

Disadvantages: Low data security. RDB persists data at intervals. If Redis fails during persistence, data loss will occur. Therefore, this method is more suitable when data requirements are not strict.

AOF (Append-only file) persistence mode: refers to the complete persistence storage of all command line records in the format of Redis command request protocol as aof files.

advantage:

  • For data security, aof persistence can be configured with the appendfsync attribute, which has the value always. Each command operation is recorded in the aof file once.

  • When writing files in append mode, even if the server crashes in the middle, the redis-check-aof tool can be used to solve data consistency issues.

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

shortcoming:

  • AOF files are larger than RDB files and are slower to restore.

  • When the data set is large, the startup efficiency is lower than RDB.

9. Common Redis performance issues and solutions

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

  • If the data is important, a slave can enable AOF to back up the data and set the policy to synchronize once per second.

  • For the speed of master-slave replication and the stability of the connection, it is best if the Master and Slave are in the same LAN.

  • Try to avoid adding slaves to a master database that is under heavy pressure.

  • Master-slave replication does not use a graph structure, a one-way linked list structure is more stable, that is: Master