Home > Database > Redis > The most complete 50 Redis interview questions and answers in history

The most complete 50 Redis interview questions and answers in history

青灯夜游
Release: 2020-08-05 16:08:31
forward
4988 people have browsed it

I saw 50 interview questions about Redis on the Internet, but no answers were given. I was also looking for the answer to this Redis interview question before, so I will share the answer today. I spent a lot of time compiling this set of Redis interview questions and answers. I hope it will be helpful to everyone.

The most complete 50 Redis interview questions and answers in history

After understanding these Redis interview questions, you can basically become a master of interviews and beat the interviewer, haha~

1. What Is it Redis?

Redis is essentially a Key-Value type in-memory database, much like memcached. The entire database is loaded into the memory for operation, and the database data is flushed to the hard disk for storage through asynchronous operations on a regular basis. Because it is a pure memory operation, Redis has excellent performance and can handle more than 100,000 read and write operations per second. It is the fastest Key-Value DB known to perform.

The excellence of Redis is not only its performance. The biggest charm of Redis is that it supports saving a variety of data structures. In addition, the maximum limit of a single value is 1GB. Unlike memcached, which can only save 1MB of data, Redis can It is used to implement many useful functions, such as using its List to make a FIFO doubly linked list, implementing a lightweight high-performance message queue service, and using its Set to make a high-performance tag system, etc.

In addition, Redis can also set the expire time for the stored Key-Value, so it can also be used as an enhanced version of memcached. The main disadvantage of Redis is that the database capacity is limited by physical memory and cannot be used for high-performance reading and writing of massive data. Therefore, the scenarios suitable for Redis are mainly limited to high-performance operations and calculations of smaller amounts of data.

Topic recommendation: 2020 redis interview questions (latest)

2. What are the advantages of Redis compared to memcached?

(1) All values ​​in memcached are simple strings, and redis, as its replacement, supports richer data types

(2) Redis is faster than memcached Much faster

(3) redis can persist its data

3. What data types does Redis support?

String, List, Set, Sorted Set, hashes

4. What physical resources does Redis mainly consume?

Redis is a memory-based high-performance database --- mainly relying on memory.

5. What is the full name of Redis?

Remote Dictionary Server

6. What data elimination strategies does Redis have?

noeviction: Returns an error when the memory limit is reached and the client attempts to execute a command that would cause more memory to be used (most write commands, but DEL and a few exceptions)

allkeys-lru: Try to recycle least used keys (LRU) so that there is room for newly added data.

volatile-lru: Try to recycle least used keys (LRU), but only keys in the expired set, so that there is room for newly added data to be stored.

allkeys-random: Recycle random keys so that there is space for newly added data.

volatile-random: Recycle random keys so that there is room for newly added data, but only for keys in the expired set.

volatile-ttl: Recycle keys in the expired set, and give priority to keys with shorter lifetimes (TTL), so that there is space for newly added data to be stored.

7. Why doesn’t Redis officially provide a Windows version?

Because the current Linux version is quite stable and has a large number of users, there is no need to develop a windows version, which will cause compatibility and other problems.

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

512M

9. Why does Redis need to put all data in memory?

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

10. How should the Redis cluster solution be implemented? What are the plans?

1), twemproxy, the general concept is that it is similar to a proxy method, and the usage method is no different from ordinary redis. After setting up multiple redis instances under it, when using it, change the place where redis needs to be connected to Connect to twemproxy, it will receive the request as a proxy and use a consistent hash algorithm to transfer the request to the specific redis, and return the result to twemproxy. It is easy to use (compared to redis, you only need to modify the connection port), and it is the first choice for extending old projects. Problem: Due to the pressure of twemproxy's own single-port instance, after using consistent hashing, the calculated value changes when the number of redis nodes changes, and the data cannot be automatically moved to the new node.

2), codis, the most commonly used cluster solution at present, basically has the same effect as twemproxy, but it supports the recovery of old node data to new hash nodes when the number of nodes changes.

3), the cluster that comes with redis cluster3.0 is characterized by its distributed algorithm not consistent hashing, but the concept of hash slots, and its own support for node setting slave nodes. See the official documentation for details.
4. Implemented at the business code layer, create several unrelated redis instances. At the code layer, perform hash calculation on the key, and then operate the data on the corresponding redis instance. This method has relatively high requirements for the hash layer code. Considerations include alternative algorithm solutions after node failure, automatic script recovery after data shock, instance monitoring, etc.

11. Under what circumstances will the Redis cluster solution cause the entire cluster to become unavailable?

In a cluster with three nodes A, B, and C, without a replication model, if node B fails, the entire cluster will think that there is a lack of slots in the range of 5501-11000. unavailable.

12. There are 20 million data in MySQL, but only 20 million data are stored in redis. How to ensure that the data in redis are hot data?

When the size of the redis memory data set increases to a certain size, the data elimination strategy will be implemented.

13. What are the suitable scenarios for Redis?

(1) Session Cache One of the most commonly used scenarios for using Redis is 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 would be unhappy if all the user's shopping cart information was lost. Now, would they still be? Fortunately, as Redis has improved over the years, it's easy to figure out how to properly use Redis to cache session documents. Even the well-known commercial platform Magento provides Redis plug-ins.

(2), Full Page Cache (FPC) In addition to the basic session token, Redis also provides a very simple FPC platform. Back to the consistency issue, even if the Redis instance is restarted, users will not see a decrease in page loading speed because of disk persistence. This is a great improvement, similar to PHP local FPC. Taking Magento as an example again, Magento provides a plugin to use Redis as a full page cache backend. In addition, for WordPress users, Pantheon has a very good plug-in wp-redis, which can help you load the pages you have browsed as quickly as possible.

(3) One of the great advantages of queue Redis in the field of memory storage engines is that it provides list and set operations, which makes Redis a good message queue platform. The operations used by Redis as a queue are similar to the push/pop operations of list in local programming languages ​​(such as Python). If you quickly search "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 view it from here.

(4), Ranking list/counter Redis implements the operation of incrementing or decrementing numbers in memory very well. Sets and Sorted Sets also make it very simple for us to perform these operations. Redis just provides these two data structures. So, to get the top 10 users from the sorted set – let’s call them “user_scores”, we just need to do it like this: Of course, this assumes you’re doing it based on your user’s scores Increasing sorting. If you want to return the user and the user's score, you need to execute it like this: ZRANGE user_scores 0 10 WITHSCORESAgora Games is a good example, implemented in Ruby, and its rankings use Redis to store data. You can see it here arrive.

(5), Publish/Subscribe Last (but certainly not least) is the publish/subscribe function of Redis. There are indeed many use cases for publish/subscribe. I've seen people use it in social network connections, as triggers for publish/subscribe based scripts, and even to build chat systems using Redis' publish/subscribe functionality! (No, this is true, you can check it out).

14. What are the Java clients supported by Redis? Which one is officially recommended?

Redisson, Jedis, lettuce, etc., the official recommendation is to use Redisson.

15. What is the relationship between Redis and Redisson?

Redisson is an advanced distributed coordination Redis client that can help users easily implement some Java objects (Bloom filter, BitSet, Set, SetMultimap, ScoredSortedSet, SortedSet, Map) in a distributed environment , ConcurrentMap, List, ListMultimap, Queue, BlockingQueue, Deque, BlockingDeque, Semaphore, Lock, ReadWriteLock, AtomicLong, CountDownLatch, Publish / Subscribe, HyperLogLog).

16. What are the advantages and disadvantages of Jedis and Redisson?

Jedis is a client implemented by Redis in Java. Its API provides relatively comprehensive support for Redis commands; Redisson implements a distributed and scalable Java data structure. Compared with Jedis, its functions It is relatively simple, does not support string operations, and does not support Redis features such as sorting, transactions, pipelines, and partitions. The purpose of Redisson is to promote the separation of users' concerns from Redis, so that users can focus more on processing business logic.

17. How to set password and verify password for Redis?

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

18. Talk about the concept of Redis hash slot?

The Redis cluster does not use consistent hashing, but introduces the concept of hash slots. The Redis cluster has 16384 hash slots. Each key is determined by taking the modulo 16384 after passing the CRC16 check. Which slot to place, each node in the cluster is responsible for a part of the hash slot.

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

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.

20. Will write operations be lost in the Redis cluster? Why?

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

21. How are Redis clusters replicated?

Asynchronous replication

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

16384.

23. How to choose a database for Redis cluster?

Redis cluster currently cannot select database, and defaults to database 0.

24. How to test the connectivity of Redis?

ping

#25. What are the uses of pipelines in Redis?

A request/response server can handle new requests even if old requests have not yet been responded to. This makes it possible to send multiple commands to the server without waiting for a reply, and finally read that reply in one step. This is pipelining, a technique that has been widely used for decades. For example, many POP3 protocols have implemented support for this function, which greatly speeds up the process of downloading new emails from the server.

26. How to understand Redis transactions?

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

27. What are the commands related to Redis transactions?

MULTI、EXEC、DISCARD、WATCH ##28. How to set the expiration time and permanent validity of Redis key respectively? EXPIRE and PERSIST commands.

29. How does Redis optimize memory?

Use hash tables (hashes) as much as possible. Hash tables (meaning that the number stored in the hash table is small) use very small memory, so you should abstract your data model as much as possible Inside a hash table. 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. Instead, store all the user's information in a hash table.

30. How does the Redis recycling process work?

A client ran a new command and added new data. Redi checks the memory usage. If it is greater than the limit of maxmemory, it will be recycled according to the set policy. A new command is executed, etc. So we are constantly crossing the boundary of the memory limit by constantly reaching the boundary and then constantly recycling back below the boundary. If the result of 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 won't take long for the memory limit to be exceeded by this memory usage.

31. What algorithm is used for Redis recycling?

LRU algorithm

32. How does Redis insert large amounts of data?

Starting with Redis 2.6, redis-cli supports a new mode called pipe mode for performing large amounts of data insertion work.

33. Why do you need Redis partitioning?

Partitioning allows Redis to manage larger memory, and Redis will be able to use the memory of all machines. Without partitions, you can only use up to one machine's memory. Partitioning enables the computing power of Redis to be doubled by simply adding computers, and the network bandwidth of Redis will also double as the number of computers and network cards increases.

34. Do you know what Redis partition implementation solutions are available?

Client-side partitioning means that the client has already decided which redis node the data will be stored in or read from. Most clients already implement client-side partitioning. Proxy partitioning means that the client sends the request to the proxy, and then the proxy decides which node to go to to write data or read data. The agent decides which Redis instances to request based on partition rules, and then returns them to the client based on the Redis response results. A proxy implementation of redis and memcached is Twemproxy query routing (Query routing), which means that the client randomly requests any redis instance, and then Redis forwards the request to the correct Redis node. Redis Cluster implements a hybrid form of query routing, but instead of forwarding requests directly from one redis node to another redis node, it redirects directly to the correct redis node with the help of the client.

35. What are the disadvantages of Redis partition?

Operations involving multiple keys are generally not supported. For example, you cannot intersect two collections because they may be stored in different Redis instances (actually there is a way for this situation, but the intersection command cannot be used directly). If you operate multiple keys at the same time, you cannot use Redis transactions. The partitioning granularity is the key, so it is not possible to shard a dataset with a very long sorting key. single huge key like a very big sorted set). When using partitions, data processing will be very complicated, for example for backup you have to collect RDB/AOF files from different Redis instances and hosts simultaneously. Dynamically scaling up or down when partitioning can be complex. Redis cluster adds or deletes Redis nodes at runtime, which can achieve data rebalancing that is transparent to users to the greatest extent. However, some other client partitioning or proxy partitioning methods do not support this feature. However, there is a pre-sharding technology that can also solve this problem better.

36. How to expand Redis persistent data and cache?

If Redis is used as a cache, use consistent hashing to achieve dynamic expansion and contraction. If Redis is used as a persistent storage, a fixed keys-to-nodes mapping relationship must be used, and the number of nodes cannot be changed once determined. Otherwise (that is, when Redis nodes need to change dynamically), a system that can rebalance data at runtime must be used, and currently only Redis cluster can do this.

37. Should distributed Redis be done in the early stage or done in the later stage when the scale is increased? Why?

Since Redis is so lightweight (a single instance only uses 1M memory), the best way to prevent future expansion is to start more instances at the beginning. Even if you only have one server, you can have Redis run in a distributed manner from the beginning, using partitions to start multiple instances on the same server. Setting up a few more Redis instances at the beginning, such as 32 or 64 instances, may be cumbersome for most users, but it is worth the sacrifice in the long run. In this case, when your data continues to grow and you need more Redis servers, all you need to do is just migrate the Redis instance from one service to another server (without considering the issue of repartitioning). Once you add another server, you need to migrate half of your Redis instances from the first machine to the second machine.

38. What is Twemproxy?

Twemproxy is a (caching) proxy system maintained by Twitter, which proxies Memcached's ASCII protocol and Redis protocol. It is a single-threaded program, written in C language, and runs very fast. It is open source software using the Apache 2.0 license. Twemproxy supports automatic partitioning. If one of its proxy Redis nodes is unavailable, the node will be automatically excluded (this will change the original keys-instances mapping relationship, so you should only use Twemproxy when using Redis as a cache). Twemproxy itself does not have a single point of problem because you can start multiple Twemproxy instances and then let your client connect to any Twemproxy instance. Twemproxy is an intermediate layer between the Redis client and the server. It should not be complicated to handle the partition function, and it should be relatively reliable.

39. What clients support consistent hashing?

Redis-rb, Predis, etc.

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

Redis has more complex data structures and provides atomic operations on them. This is an evolutionary path different from other databases. Redis's data types are based on basic data structures and are transparent to programmers, without the need for additional abstractions. Redis runs in memory but can be persisted to disk, so memory needs to be weighed when performing high-speed reading and writing of different data sets. The amount of data should not be larger than the hardware memory. Another advantage of in-memory databases is that compared to the same complex data structures on disk, operating in memory is very simple, so Redis can do a lot of things with strong internal complexity. Also, in terms of disk format they are compact append-generated since they do not require random access.

41. What is the memory usage of Redis?

Let me give you an example: 1 million key-value pairs (the keys are 0 to 999999 and the value is the string "hello world") uses 100MB on my 32-bit Mac notebook. Putting the same data into a key only requires 16MB. This is because the key value has a large overhead. Execution on Memcached has similar results, but the overhead is slightly smaller than that of Redis, because Redis will record type information, reference counting, etc. Of course, the ratio between the two is much better with large key-value pairs. 64-bit systems require more memory overhead than 32-bit systems, especially when the key-value pairs are small. This is because pointers occupy 8 bytes in 64-bit systems. But, of course, 64-bit systems support larger memory, so running a large Redis server will more or less require a 64-bit system.

42. What are some ways to reduce the memory usage of Redis?

If you are using a 32-bit Redis instance, you can make good use of collection type data such as Hash, list, sorted set, set, etc., because usually many small Key-Values ​​can be used more compactly stored together.

43. What command is used to view Redis usage and status information?

info

44. What happens when Redis runs out of memory?

If the set 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. When Redis reaches the memory Old content will be flushed out within a certain time limit.

45. Redis is single-threaded. How to improve the utilization of multi-core CPU?

You can deploy multiple Redis instances on the same server and use them as different servers. At some point, one server is not enough anyway, so if you want With multiple CPUs, you can consider sharding.

46. How many keys can a Redis instance store at most?

How many elements can List, Set and Sorted Set store at most? In theory, Redis can handle up to 232 keys, and in actual tests, each instance stored at least 250 million keys. 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 available memory value in the system.

47. Redis common performance problems and solutions?

(1) Master is best not to do any persistence work, such as RDB memory snapshots and AOF log files

(2) If the data is important, a Slave should enable AOF backup Data, policy is set to synchronize once per 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 slave libraries to the master library that is under great pressure

(5) Do not use a graph structure for master-slave replication. It is more stable to use a one-way linked list structure, that is: Master <- Slave1 <- Slave2 <- Slave3... This structure is convenient to solve the single point of failure problem and realize the replacement of Master by Slave. If the Master hangs up, you can immediately enable Slave1 as the Master, leaving everything else unchanged.

48. What persistence methods does Redis provide?

RDB persistence mode can store snapshots of your data at specified time intervals. AOF persistence mode records each operation written to the server. When the server restarts, these commands will be re-executed to restore the original data. , the AOF command uses the redis protocol to append and save each write operation to the end of the file. Redis can also rewrite the AOF file in the background, so that the size of the AOF file will not be too large. If you only want your data to be stored when the server is running exists, you can also not use any persistence method. You can also enable both persistence methods at the same time. In this case, when redis restarts, the AOF file will be loaded first to restore the original data, because under normal circumstances The data set saved by the AOF file is more complete than the data set saved by the RDB file. The most important thing is to understand the difference between RDB and AOF persistence methods. Let's start with the RDB persistence method.

49. How to choose the appropriate persistence method?

Generally speaking, if you want to achieve data security comparable to PostgreSQL, you should use both persistence functions at the same time. If you care deeply about your data but can still afford data loss within minutes, then you can just use RDB persistence. Many users only use AOF persistence, but this method is not recommended: because regularly generating RDB snapshots is very convenient for database backup, and RDB restores data sets faster than AOF, except In addition, using RDB can also avoid the bugs of the AOF program mentioned earlier.

50. Will modifying the configuration take effect in real time without restarting Redis?

There are many configuration options for running instances that can be modified via the CONFIG SET command without performing any form of reboot. Starting with Redis 2.2, it is possible to switch from AOF to RDB's snapshot persistence or other means without restarting Redis. Retrieve the ‘CONFIG GET *’ command for more information. But occasionally a restart is necessary, such as to upgrade the Redis program to a new version, or when you need to modify some configuration parameters that are not currently supported by the CONFIG command.

Recommended learning: Redis video tutorial

The above is the detailed content of The most complete 50 Redis interview questions and answers in history. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
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