Must ask Redis interview questions-Mom no longer has to worry about being asked about Redis in my interview!

As an open source, advanced key-value store and an applicable solution, Redis has increasingly played a pivotal role in building "high-performance" and "scalable" Web applications.

In today's Internet technology architecture, Redis has become one of the most widely used middleware. It is also one of the most popular engineering skills for interviewers in mid- to high-end back-end engineering technical interviews. It not only requires us to use basic To master, it is necessary to have a deeper understanding of the detailed principles of Redis internal implementation.

If you are proficient in Redis, it is no exaggeration to say that you have half your feet in your favorite company. Let's take a look at the classic interview questions of Redis together, so don't be asked by the interviewer to make your face green!

In addition to these Redis interview questions, I have also compiled a complete set of video tutorials for architects and systematic materials on java. Friends in need can click the link below to get them for free!
Link: 1103806531 Password: CSDN

Insert picture description here

Redis basic interview questions

1. What is Redis? Briefly describe its advantages and disadvantages?

  1. Redis is essentially a Key-Value type memory database, much like memcached.

  2. The entire database is loaded in the memory for operation, and the database data is flushed to the hard disk for storage periodically through asynchronous operations.

  3. Because it is a pure memory operation, Redis has excellent performance. It can handle more than 100,000 read and write operations per second. It is the fastest known Key-Value DB.

  4. The excellence of Redis is not only performance. The biggest charm of Redis is that it supports saving multiple data structures. In addition, the maximum limit of a single value is 1G. Unlike memcached, which can only store 1MB of data, Redis can be used to implement many useful functions. .

  5. For example, use his List as a FIFO doubly linked list to realize a lightweight high-performance message queue service, and use his Set to make a high-performance tag system and so on.

  6. Redis can also set the expire time for the stored Key-Value, so it can also be used as an enhanced version of memcached.

  7. 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 calculations with a small amount of data.

2. What are the advantages of Redis over memcached?

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

  2. Redis is much faster than memcached

  3. redis can persist its data

3. Redis has five data types, where is each data type used?

  1. Str (cache)

  2. List (message queue)

  3. hashes (user id+corresponding attribute name as a unique identifier to obtain the value of the corresponding attribute)

  4. Set (set is similar to list, the special feature is that set can automatically sort the weight: find the mutual friends of two people on Weibo)

  5. The use scenario of sorted set sorted set is similar to set, the difference is that set is not automatically ordered and sorted set can sort the members by providing a priority (score) parameter by the user, and it is inserted in order, that is, automatic sorting .

    For example: Twitter's public timeline can be stored with the publication time as the score, so that it is automatically sorted by time when it is obtained.

4. What kinds of data elimination strategies does Redis have?

noeviction: returns an error when the memory limit is reached and the client tries to execute commands that will make more memory used (most write commands, but DEL and a few exceptions)

allkeys-lru: Try to reclaim the least used keys (LRU) so that the newly added data has space for storage.

Volatile-lru: Try to reclaim the least used keys (LRU), but only in the expired set of keys, so that the newly added data has space for storage.

allkeys-random: Reclaim random keys so that the newly added data has space for storage.

Volatile-random: Recycling random keys allows space for newly added data to be stored, but only for keys in the expired collection.

Volatile-ttl: Reclaim the keys in the expired collection, and give priority to reclaim the keys with a shorter time to live (TTL), so that newly added data has space for storage.
Insert picture description here

5. What is the maximum storage capacity for a string type value?

512M

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

In order to achieve the fastest read and write speed, Redis reads all data into the memory and writes the data to disk in an asynchronous manner.

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

If the maximum memory usage is set, new values ​​cannot be inserted after the number of existing data records reaches the memory limit.

7. What should the Redis cluster solution do? What are the options?

1) codes。

Codis currently uses the most clustering scheme, basically the same effect as twemproxy, but it supports the data of the old node can be restored to the new hash node when the number of nodes changes.

2) The cluster that comes with redis cluster3.0

The characteristic is that his distributed algorithm is not a consistent hash, but the concept of a hash slot, and its own support for node setting slave nodes.

3) Realize at the business code layer

Start a few unrelated redis instances, perform hash calculation on the key at the code layer, and then go to the corresponding redis instance to manipulate the data.

This method requires a relatively high level of hash code. Some considerations include alternative algorithm schemes after node failure, automatic script recovery after data shock, instance monitoring, and so on.

8. Under what circumstances will the Redis cluster solution cause the entire cluster to be unavailable?

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

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

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

10. 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 16,384 hash slots.

After each key passes the CRC16 check, it modulates 16384 to determine which slot to place. Each node of the cluster is responsible for a part of the hash slot.

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

12. Will there be write operation loss in Redis cluster? why?

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

Must ask Redis interview questions-mother no longer has to worry about being asked about Redis in my interview

What are the suitable scenarios for Redis

1. Session Cache

One of the most commonly used scenarios for using Redis is the session cache.

The advantage of using Redis to cache sessions over other storage (such as Memcached) is that Redis provides persistence.

2. Full page cache (FPC)

In addition to the basic session token, Redis also provides a very simple FPC platform.

Going back to the consistency problem, even if the Redis instance is restarted, users will not see the page load speed drop because of disk persistence. This is a great improvement, similar to the PHP local FPC.

3. Queue

One of the great advantages of Reids in the field of memory storage engines is to provide list and set operations, which makes Redis a good message queue platform to use.

The operation used by Redis as a queue is similar to the push/pop operation of a local programming language (such as Python) on a list.

4. Leaderboard/Counter

Redis implements the operation of incrementing or decrementing numbers in memory very well.

Set and Sorted Set also make it very easy for us to perform these operations. Redis just provides these two data structures.

So, we need to get the top 10 users from the sorted set-we call it "user_scores", we just need to execute as follows:

Of course, this assumes that you are sorting in ascending order based on 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, implemented in Ruby, and its leaderboard uses Redis to store data, you can see here.

5. Publish/Subscribe

There are indeed many usage scenarios for publish/subscribe. I have seen people use it in social network connections, can also be used as a script trigger based on publish/subscribe, and even use Redis' publish/subscribe function to build a chat system!

At last

Hope this article is helpful to everyone!

I have also compiled a complete set of video tutorials for architects and systematic materials on java, including core knowledge points of java, interview topics, and the latest Internet real questions and e-books for 20 years. Friends in need can click the link below to get it for free!
Link: 1103806531 Password: CSDN
Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_48655626/article/details/108581374