Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the classic Redis interview questions?

2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "what are the classic Redis interview questions". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

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

Redis is essentially an in-memory database of Key-Value type, much like memcached, the whole database is loaded and operated in memory, and the database data is saved to the hard disk by asynchronous operation periodically.

Because of its pure memory operation, Redis has excellent performance, which can handle more than 100000 read and write operations per second, making it the fastest Key-Value DB known.

The excellence of Redis is not only the performance, the greatest charm of Redis is to support the preservation of multiple data structures, in addition, the maximum limit of a single value is 1GB, unlike memcached can only save 1MB data, so Redis can be used to achieve a lot of useful functions.

For example, using his List to do FIFO two-way linked list, to achieve a lightweight high-performance message queue service, with his Set can do high-performance tag system, and so on.

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 can not be used for high-performance read and write of massive data, so the suitable scenarios of 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 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. Which data types does Redis support?

String 、 List 、 Set 、 Sorted Set 、 hashes

4. What are the main physical resources consumed by Redis?

Memory.

5. What is the full name of Redis?

Remote Dictionary Server .

6. 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 allow more memory to be used (most write instructions, but DEL and a few exceptions)

Allkeys-lru: try to recycle the least used key (LRU) so that the newly added data has room to store.

Volatile-lru: try to recycle the least used keys (LRU), but only those keys in expired collections, so that newly added data has room to store.

Allkeys-random: reclaim random keys to make room for newly added data.

Volatile-random: reclaiming random keys gives room for newly added data, but only for keys in expired collections.

Volatile-ttl: reclaim keys in expired collections, and give priority to keys with a short time to live (TTL) so that newly added data can be stored.

7. Why doesn't Redis officially provide the Windows version?

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

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

512M

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

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

So redis has the characteristics of high speed and data persistence. If you do not put the data in memory, the speed of the disk Imax O will seriously affect the performance of the redis.

Today, when memory is getting cheaper and cheaper, redis will become more and more popular. If the maximum used memory is set, the new value cannot be inserted after the number of records of the data has reached the memory limit.

10. What should the Redis cluster solution do? What are the plans?

1.codis .

At present, the most widely used cluster scheme has the same effect as twemproxy, but it supports that the data of the old node can be restored to the new hash node when the number of nodes changes.

2.redis cluster3.0 's own cluster is characterized by the fact that his distributed algorithm is not consistent hash, but the concept of hash slot and its own support for nodes to set slave nodes. Take a look at the official document introduction.

3. In the business code layer, there are several unrelated redis instances. In the code layer, the key is calculated by hash, and then the corresponding redis instance is used to manipulate the data. This method has high requirements for hash layer code, including the alternative algorithm after node failure, automatic script recovery after data shock, instance monitoring, and so on.

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

For a cluster with three nodes, without replication model, if node B fails, the whole cluster will think that there is a lack of slots in the range of 5501-11000.

12. 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 redis in-memory dataset size rises to a certain size, the data elimination strategy is implemented.

13. What are the suitable scenarios for Redis?

(1) session caching (Session Cache)

One of the most common scenarios where Redis is used is session caching (session cache). The advantage of caching sessions with Redis over other stores, such as Memcached, is that Redis provides persistence. When maintaining a cache that is not strictly consistent, most people will be unhappy if all the users' shopping cart information is lost. Will they still do so now?

Fortunately, as Redis has improved over the years, it's easy to find out how to properly use Redis to cache session documents. Even the well-known business platform Magento provides plug-ins for Redis.

(2) full page cache (FPC)

In addition to the basic session token, Redis provides a very simple FPC platform. Back to consistency, even if the Redis instance is restarted, users will not see a drop in page loading speed because of disk persistence, which is a great improvement, similar to PHP native FPC.

Again, take Magento as an example. Magento provides a plug-in to use Redis as the 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 visited as quickly as possible.

(3) queue

One of the advantages of Reids in the field of memory storage engines is that it provides list and set operations, which makes Redis a good message queuing platform to use. The operation used by Redis as a queue is similar to the push/pop operation of list by a native program language such as Python.

If you quickly search for "Redis queues" in Google, you will immediately find a large number of open source projects designed to use Redis to create very good back-end tools to meet a variety of queue needs. For example, Celery has a background that uses Redis as broker, which you can check from here.

(4) rankings / counters

Redis does a great job of incrementing or decrementing numbers in memory. Set and Sorted Set also make it easy for us to perform these operations, and Redis just happens to provide these two data structures.

So, to get the top 10 users from the sorted set-we call it "user_scores", we just need to do something like this:

Of course, this assumes that you are sorting incrementally according to the scores of your users. If you want to return the user and the user's score, you need to do this:

ZRANGE user_scores 0 10 WITHSCORES

Agora Games is a good example, implemented in Ruby, and its ranking uses Redis to store data, as you can see here.

(5) publish / subscribe

Last (but certainly not least) is Redis's publish / subscribe capabilities. There are indeed a lot of publish / subscribe usage scenarios. I've seen people use it in social networking connections, act as publish / subscribe based scripting triggers, and even use Redis's publish / subscribe feature to set up chat systems!

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

Redisson, Jedis, lettuce, etc., Redisson is officially recommended.

15. What is the relationship between Redis and Redisson?

Redisson is an advanced distributed coordination Redis client, which 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.

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

Jedis is the client of Redis's Java implementation, and its API provides more comprehensive support for Redis commands.

Redisson implements a distributed and extensible Java data structure. Compared with Jedis, the function is relatively simple, does not support string operation, does not support sorting, transaction, pipeline, partition and other Redis features. The purpose of Redisson is to promote the separation of users' attention from Redis, so that users can focus more on dealing with business logic.

17. How does Redis set and verify passwords?

Set password: config set requirepass 123456

Authorization password: auth 123456

18. What is 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 uses a module of 16384 after CRC16 verification to decide which slot to place, and each node of the cluster is responsible for 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 can not communicate, the cluster uses the master-slave replication model, and each node will have a replica.

20. Will any writes be lost in the Redis cluster? Why?

Redis does not guarantee strong consistency of data, which means that in practice, clusters may lose writes 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 does the Redis cluster select the database?

Currently, Redis cluster cannot make database selection. Default is 0 database.

24. How to test the connectivity of Redis?

Ping

25. What is the use of pipes in Redis?

A request / response server can process a new request even if the old request has not been answered. This allows you to send multiple commands to the server without waiting for a reply, and finally read the reply in one step.

This is pipelining, a technology that has been widely used for decades. For example, many POP3 protocols have implemented support for this feature, greatly speeding up the process of downloading new mail from the server.

26. How to understand Redis transactions

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

A transaction is an atomic operation: either all or none of the commands in the transaction 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?

EXPIRE and PERSIST commands.

29. How does Redis optimize memory?

Use a hash table (hashes) as much as possible. A hash table (that is, a small number stored in a hash table) uses very little memory, so you should abstract your data model into a hash table as much as possible.

For example, if you have a user object in your web system, don't set a separate key for the user's name, last name, mailbox, and password. Instead, store all the user's information in a hash table.

30. How does the Redis recycling process work?

A client runs a new command and adds new data.

Redi checks the memory usage, and if it is greater than the limit of maxmemory, it will be reclaimed according to the set policy.

A new order is executed, and so on.

So we constantly cross 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 (for example, the intersection of large sets is saved to a new key), it will not be long before the memory limit is exceeded by this memory usage.

This is the end of the content of "what are the classic interview questions for Redis". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report