Redis interview questions

  • 2020-10-07 18:56:53
  • OfStack

Which data types Redis supports ?

string: Most basic data type, base 2 safe string, maximum 512M list: A list of strings that remain in order of addition set: An unordered collection of strings with no duplicate elements sorted set: A sorted collection of strings hash: One set of key/value pairs

Is Redis single-process or single-threaded?

Redis is single-process single-thread. Redis USES queue technology to change concurrent access to serial access, eliminating the overhead of traditional database serial control.

Why is Redis single-threaded?

Multithreaded processing is designed to lock, and multithreaded processing is designed to switch threads and consume CPU. Since CPU is not a bottleneck for Redis, the bottleneck for Redis is most likely machine memory or network bandwidth. Single-threaded CPU can't perform multi-core CPU performance, but it can be solved by opening the Redis instance on a single machine.

The advantage of Redis

Speed is fast. Because the data is stored in memory, like HashMap, the advantage of HashMap is that the time complexity of finding and operating is O(1). Support for rich data types, string, list, set, sorted set, hash Transactions are supported, and operations are atomicity, which means that changes to the data are either executed in full or not at all Rich features: Can be used for caching, messages, set expiration time by key, after which it will be deleted automatically

What are the advantages of Redis and memcached

All memcached values are simple strings, and Reids serves as a substitute for them, supporting richer data types The Redis is much faster than the memcached Redis can persist its data Redis supports backup of data, that is, data backup in master/slave mode

Redis has several data elimination strategies

In Redis, the user is allowed to set the maximum memory size of ES60en.maxmemory, and when the Redis memory data set size rises to 1 fixed size, the data elimination strategy is performed

volatile-lru: Select the least-recently used cull from the data set that has been set to expire volatile-ttl: Select data that will expire from a set of data that has been set to expire volatile-random: Arbitrarily select data from a data set that has been set to expire allkeys-lru: Select the least recently used data from the data set for elimination allkeys-random: Arbitrarily select data from a data set for elimination noenviction: Elimination of data is prohibited

What kinds of persistence are supported by Redis

RDB persistence

The principle is to timing dump to an RDB file on disk when Redis's data is recorded in memory

The snapshot of the data set in memory is written to disk within the specified time interval. The actual operation process is fork1 child process. The data set is first written to a temporary file, and after the successful writing, the previous file is replaced and the storage is compressed in base 2.

AOF (append only file) persistence

The idea is to append the Redis operation log to a file.

Every write and delete operation processed by the server will be recorded in the form of log. The query operation will not be recorded, but will be recorded in the form of text. You can open the file and see the detailed operation record. These commands are reexecuted when the server is restarted to restore the original data. The AOF command appends the Reids protocol to save each write to the end of the file. Redis can also do background rewriting on AOF files so that the AOF files are not too large.

What are the advantages and disadvantages of Redis?

RDB persistence

Advantages: RDB is compact, small in size, fast in network transmission, suitable for full replication; The recovery is much faster than the AOF. Of course, one of the most important advantages of RDB over AOF is that it has a relatively small impact on performance

Disadvantages: The fatal shortcoming of RDB file is that it cannot be persisted in real time due to the persistence mode of its data snapshot. However, with the increasing importance of data today, the massive loss of data is unacceptable in many cases. Therefore, AOF persistence is called the mainstream. In addition, RDB files need to be in a specific format and are not compatible.

AOF persistence

Corresponding to RDB persistence,AOF has the advantages of supporting second-level persistence and good compatibility, while its disadvantages include large file size, slow recovery speed and great impact on performance

How to select the Redis persistence strategy?

Before I introduce the persistence strategy, it is important to understand that both RDB and AOF require performance costs to enable persistence. Compared with RDB persistence, the first aspect is that when bdsave performs fork operation, the main PROCESS of Redis will block, and the other aspect is that when the child process writes data to the hard disk, it will also bring IO pressure. For AOF persistence, the frequency of writing data to the hard disk is greatly increased (in seconds under everysec policy), and IO is even more stressful, which may cause AOF to append blocking files. In addition, the AOF file rewrite is similar to RDB's basave, with fork blocking and child process IO stress issues. The impact on the performance of the Redis main process will be greater because the AOF writes data to the hard disk more frequently.

In the actual production environment, there will be a variety of persistence strategies according to different situations such as data volume, application security requirements for data, budget constraints, etc. If no persistence is used at all, RDB or AOF1 will be used, or colleagues will enable RDB and AOF persistence, etc. In addition, the choice of persistence must be considered with the master-slave strategy 1 of Redis, because master-slave replication and persistence have the same function of data backup, and host master and slave slave can independently choose the persistence scheme.

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

In order to survive the failure of some nodes or the failure of most nodes to communicate, the master-slave replication model is used for the cluster, with es161EN-1 replica for each node

Will Redis clusters lose write operations? Why is that?

Redis does not guarantee strong 1 data consistency, which means that in practice clusters can lose write operations under certain conditions

How are Redis clusters replicated

Asynchronous replication

How does Redis optimize memory

As far as possible use hash (hashes), hash table (mean list stored number less) use of memory is very small, so you should as far as possible to your abstract data model into a hash table, such as your web there are 1 user object in the system, not for the user's name, surname, email, password separate key, but should put all the user information stored in the 1 a hash table

How does the Redis recycling process work?

1 Client runs a new command and adds new data. Redis checks memory usage and if it exceeds the maxmemory limit, it will recycle according to the set policy

Redis common usage scenarios

Session Sharing (Single sign-on) Page caching The queue Leaderboard/Calculator

That's all for Redis interview questions. For more information on Redis interview questions, check out the other articles on this site!


Related articles: