Redis interview questions and answers

1. What is redis? Describe its advantages and disadvantages?

The full name of redis is remote dictionary Server is essentially a key value type memory database, much like memcached. The entire database is loaded into memory for operation, and the database data is periodically flushed to the hard disk for storage through asynchronous operation.

Because it is a pure memory operation, redis has excellent performance. It can handle more than 100000 read and write operations per second. It is the fastest known key value dB.

Redis's excellence lies not only in its performance, but also in its support of saving multiple data structures. In addition, the maximum limit of a single value is 1GB, unlike memcached, which can only save 1MB of data, so redis can be used to realize many useful functions.

For example, his list can be used as a FIFO two-way linked list to realize a lightweight high-performance message queue service, and his set can be used as a high-performance tag system.

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 suitable scenarios for redis are mainly limited to high-performance operations and operations with a small amount of data.

2. What are the advantages of redis over memcached?

1. All memcached values are simple strings. Redis, as its substitute, supports richer data types

2. Redis is much faster than memcached, and redis is much faster than memcached

3. Redis can persist its data. Redis can persist its data

3. What data types does redis support?

String、List、Set、Sorted Set、hashes

4. What physical resources does redis consume?

Memory.

5. What data elimination strategies does redis have?

1. Noeviction: returns an error when the memory limit is reached and the client attempts to execute a command that will make more memory used.

2. Allkeys LRU: try to recycle the least used key (LRU) to make room for the newly added data.

3. Volatile LRU: try to recycle the least used key (LRU), but only the keys in the expired set, so that the newly added data has space to store.

4. Allkeys random: Reclaim random keys to make room for newly added data.

5. Volatile random: Reclaim random keys to make room for newly added data, but only for keys in expired sets.

6. Volatile TTL: reclaim the keys in the expired set, and give priority to reclaiming the keys with short TTL, so that the newly added data can be stored.

6. Why doesn't redis officially provide the Windows version?

Because the current Linux version is quite stable and has a large number of users, there is no need to develop the Windows version, but it will bring compatibility and other problems.

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

512M

8. Why does redis need to put all data in memory?

In order to achieve the fastest reading and writing speed, redis reads data into memory and writes data to disk asynchronously.

Therefore, redis has the characteristics of fast and data persistence. If the data is not placed in memory, the disk I / O speed will seriously affect the performance of redis.

Today, with the memory getting cheaper and cheaper, redis will become more and more popular. If the maximum memory used is set, the existing data records cannot be inserted after reaching the memory limit.

9. What should the redis cluster scheme do? What are the plans?

1.codis

2. The most commonly used cluster scheme basically has the same effect as twoproxy, but it supports the recovery of old node data to new hash nodes when the number of nodes changes. redis cluster3. 0's own cluster is characterized in that its distributed algorithm is not a consistent hash, but the concept of hash slot and its own supporting node setting slave node. See the official documents for details.

3. In the business code layer, start several unrelated redis instances. In the code layer, hash the key, and then operate the data in the corresponding redis instance. This method has high requirements for hash layer code. The considered parts include alternative algorithm scheme after node failure, automatic script recovery after data shock, instance monitoring, etc.

10. Under what circumstances will the redis cluster scheme make the whole cluster unavailable?

For a cluster with three nodes a, B and C, if node B fails without the replication model, the whole cluster will be considered unavailable due to the lack of slots in the range of 5501-11000.

11. There are 2000W data in MySQL and only 20W data in redis. How to ensure that all data in redis are hot data?

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

In fact, in addition to investigating redis during the interview, many companies attach great importance to highly concurrent and highly available technologies, especially front-line Internet companies. Knowledge points such as distributed, JVM, spring source code analysis and microservices are necessary for the interview. I have organized a collection of a system of architecture technology, which is aimed at the current Internet Co's technical needs and mainstream technology. These things may be contacted by you in your work, but you lack comprehensive and systematic learning. You can add the backend Development Group: 943918498, or pay attention to WeChat official account: Java information base, reply to "architecture". Get the structure information free of charge.

12. 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 will be unhappy if all users' shopping cart information is lost. Will they still do so now?

Fortunately, with the improvement of redis over the years, it is easy to find out how to properly use redis to cache session documents. Even Magento, a well-known business platform, provides redis plug-ins.

(2) Full page cache (FPC)

In addition to the basic session token, redis also provides a very simple FPC platform. Returning to the consistency problem, 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.

Again, take Magento as an example. Magento provides a plug-in to use redis as the back-end of full page caching.

In addition, for WordPress users, Pantheon has a very good plug-in WP redis, which can help you load the pages you have visited as quickly as possible.

(3) Queue

One of the advantages of IDS in the field of memory storage engine is that it provides list and set operations, which makes redis a good message queuing platform. The operation of redis as a queue is similar to the push / pop operation of a local program language (such as Python) on a list.

If you quickly search "redis queues" in Google, you can 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 requirements. For example, a background of celery uses redis as a broker. You can check it here.

(4) Leaderboard / counter

Redis implements the operation of increasing or decreasing numbers in memory very well. Set and sortedset also make it very simple for us to perform these operations. Redis just provides these two data structures.

Therefore, we need to get the top 10 users from the sorting set – we call them "user_scores". We just need to do the following:

Of course, this assumes that you sort incrementally according to your users' scores. If you want to return users and their scores, you need to do this:

ZRANGE user_ scores 0 10 WITHscoreS

Agora games is a good example. It is implemented in ruby. Its leaderboard uses redis to store data. You can see it here.

(5) Publish / subscribe

Last (but certainly not least) is redis's publish / subscribe function. There are indeed many use scenarios for publish / subscribe. I have seen that people use it in social network connections. It can also be used as a script trigger based on publish / subscribe, and even use redis's publish / subscribe function to establish a chat system!

13. What are the Java clients supported by redis? Which is the official recommendation?

Redisson, jedis, lettuce, etc. redisson is officially recommended.

14. What is the relationship between redis and redisson?

Redisson is an advanced distributed coordination redis customer service terminal, It can help users easily implement some Java objects (Bloom filter, BitSet, set, setmultimap, scoredsortedset, sortedset, map, concurrentmap, list, listmultimap, queue, BlockingQueue, deque, blockingdeque, semaphore, lock, readwritelock, atomiclong, countdownlatch, publish / subscribe, hyperloglog) in a distributed environment.

15. What are the advantages and disadvantages of jedis compared with redisson?

Jedis is the Java implementation client of redis, and its API provides comprehensive redis command support;

Redisson implements a distributed and extensible Java data structure. Compared with jedis, redison has simpler functions and does not support string operation, sorting, transaction, pipeline, partition and other redis features. Redisson aims to promote the separation of users' attention to redis, so that users can focus more on processing business logic.

16. Talk about the concept of redis hash slot?

Redis cluster does not use consistent hash, but introduces the concept of hash slot. Redis cluster has 16384 hash slots. Each key takes the module of 16384 after CRC16 verification to determine which slot to place. Each node of the cluster is responsible for part of the hash slot.

17. 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 the master-slave replication model, and each node will have n-1 replicas

18. Will write operations be lost in redis cluster? Why?

Redis cannot guarantee strong data consistency, which means that in practice, the cluster may lose write operations under specific conditions.

19. How are redis clusters replicated?

Asynchronous replication

20. What is the maximum number of nodes in redis cluster?

16384

21. How do redis clusters select databases?

Redis cluster cannot make database selection at present. The default database is 0.

22. What is the use of pipes in redis?

The one-time request / response server can process the new request even if the old request has not been responded, so it can send multiple commands to the server without waiting for a reply. Finally, read the reply in one step.

This is pipelining, which has been widely used for decades. For example, many POP3 protocols have realized this function, which greatly speeds up the process of downloading new mail from the server.

23. How to understand redis transactions?

Transaction is a separate isolation operation: all commands in the transaction will be serialized and executed sequentially. During the execution of the transaction, it will not be interrupted by command requests sent by other clients.

A transaction is an atomic operation: all commands in the transaction are either executed or not executed at all.

24. What are the commands related to redis transactions?

MULTI、EXEC、DISCARD、WATCH

25. How to set the expiration time and permanent validity of redis key respectively?

Execute and persist commands

26. How does redis optimize memory?

Use hashes as much as possible. The memory used by hash tables (that is, the number stored in hash tables is small) is very small, so you should abstract your data model into a hash table as much as possible.

For example, there is a user object in your web system. Do not set a separate key for the user's name, last name, mailbox and password, but store all the user's information in a hash table.

27. How does the redis recycling process work?

A client runs a new command and adds new data.

RedI checks the memory usage. If it is greater than the maxmemory limit, it will recycle according to the set policy.

A new command is executed, and so on.

So we constantly cross the boundary of memory limit, reach the boundary, and then recycle 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), the memory limit will soon be exceeded by this memory usage.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>