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

The most common interview questions and answers for Redis

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

This article will explain in detail the most common interview questions and answers about Redis. The content of the article is of high quality, so I hope you can get something after reading this article.

1. What is Redis?

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.

Redis's outstanding place is not only performance, Redis's greatest charm is to support the preservation of a variety of 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 many useful functions, such as 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?

Redis is a memory-based, high-performance database-mainly dependent on 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. What should the Redis cluster solution do? What are the plans?

1), twemproxy, the general concept is that it is similar to a proxy method, and its use method is no different from that of ordinary redis. After setting up multiple redis instances under it, it will connect to twemproxy where you need to connect redis. It will receive the request as an agent and use the consistent hash algorithm to transfer the request to a specific redis and return the result to twemproxy. Easy to use (relative to redis only need to modify the connection port), the first choice for the expansion of old projects. Problem: due to the pressure of twemproxy's own single-port instance, after using consistent hash, the data cannot be automatically moved to the new node due to the change of the calculated value when the number of redis nodes is changed.

2), codis, the most widely used cluster scheme, is basically the same as twemproxy, but it supports that the data of old nodes can be restored to new hash nodes when the number of nodes changes.

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

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

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

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

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

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

512M

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.

13. What are the suitable scenarios for Redis?

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 also 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) one of the advantages of queue Reids in the field of memory storage engine is that it provides list and set operations, which enables Redis to be used as a good message queue platform. 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) ranking / counter Redis performs very well in increasing or decreasing 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, we need to get the top 10 users from the sorted set-we call it "user_scores", and we just need to do something like this: of course, this assumes that you are sorting incrementally based on your user's score. If you want to return users and their scores, you need to do this: ZRANGE user_scores 010 WITHSCORESAgora 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 the publish / subscribe function of Redis. 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! No, it's true. You can check it out.

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 scalable Java data structure, compared with Jedis, the function is relatively simple, does not support string operation, does not support sorting, transactions, pipes, partitions 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?

How to set the expiration time and permanent validity of MULTI, EXEC, DISCARD, WATCH # # 28 and Redis key, respectively? 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, do not 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.

31. What algorithm is used for Redis recycling?

LRU algorithm

32. How does Redis insert a large amount of data?

Redis2.6 began redis-cli to support a new mode called pipe mode for performing large amounts of data insertion.

33. Why do you want to do Redis partition?

Partitioning will allow Redis to manage more memory, and Redis will be able to use the memory of all machines. If there is no partition, you can only use the memory of one machine at most. Partition increases the computing power of Redis by simply increasing the number of computers, and the network bandwidth of Redis increases exponentially with the increase of computers and network cards.

34. Do you know what Redis partitioning schemes are available?

The client partition is where the client has decided which redis node the data will be stored in or which redis node to read from. Most clients have implemented client partitioning. Agent partitioning means that the client sends a request to the agent, and the agent then decides which node to write or read data to. The agent decides which Redis instances to request according to the partition rules, and then returns it to the client based on the response result of the Redis. One proxy implementation of redis and memcached is Twemproxy query routing (Query routing), which means that the client requests any redis instance randomly, and then the 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 directly redirected to the correct redis node with the help of the client.

35. What are the disadvantages of Redis partitioning?

Operations that involve multiple key are usually not supported. For example, you can't intersect two sets because they may be stored in different Redis instances (in fact, there is a way, but you can't use the intersection instruction directly). If you operate on multiple key at the same time, you cannot use Redis transactions. Partitions use key granularity and cannot use a very long sorted key to store a dataset (The partitioning granularity is the key, so it is not possible to shard a dataset with a single huge key like a very big sorted set). When using partitions, data processing can be very complex, for example, you have to collect RDB / AOF files from different Redis instances and hosts at the same time in order to back up. Dynamic capacity expansion or reduction during partitioning may be very complex. Redis cluster can rebalance data transparently to users by adding or deleting Redis nodes at run time, but some other client or agent partitioning methods do not support this feature. However, there is a pre-slicing technology that can also solve this problem.

36. How to expand the persistent data and cache of Redis?

If Redis is used as a cache, use consistent hash to achieve dynamic expansion and reduction. If Redis is used as a persistent store, a fixed keys-to-nodes mapping relationship must be used, and the number of nodes cannot be changed once determined. Otherwise (that is, where Redis nodes need to change dynamically), you must use a system that can rebalance data at run time, which only Redis clusters can currently do.

37. Should distributed Redis be done in the early stage or on a later scale? Why?

Since Redis is so lightweight (only 1 MB of memory is used for a single instance), the best way to prevent future expansion is to start more instances in the first place. Even if you have only one server, you can start with Redis running in a distributed manner, using partitions, and launching multiple instances on the same server. Setting up a few more Redis instances in the first place, such as 32 or 64, may be cumbersome for most users, but it's worth the sacrifice in the long run. That way, as your data grows and you need more Redis servers, all you need to do is simply move Redis instances from one service to another (without thinking about repartitioning). Once you have added another server, you need to move half of your Redis instances from the first machine to the second machine.

38. What is Twemproxy?

Twemproxy is the (caching) proxy system maintained by Twitter, the ASCII protocol and Redis protocol of proxy Memcached. It is a single-threaded program, written in c language, and runs very fast. It is open source software using Apache 2.0 license. Twemproxy supports automatic partitioning and automatically excludes one of its proxy Redis nodes if it is not available (this will change the mapping of the original keys-instances, so you should only use Twemproxy when caching Redis). 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 a middle layer between Redis client and server, so it should not be complicated and reliable to deal with partition functions.

39. What clients support consistent hashing?

Redis-rb, Predis, etc.

40. How is Redis different from other key-value storage?

Redis has more complex data structures and provides atomic operations on them, which is an evolutionary path different from other databases. The data types of Redis are based on basic data structures while being transparent to programmers without additional abstraction. Redis runs in memory but can be persisted to disk, so memory needs to be weighed when reading and writing high-speed data sets, because the amount of data should not be larger than hardware memory. Another advantage of in-memory databases is that compared to the same complex data structures on disk, it is very easy to operate in memory, so Redis can do a lot of things with strong internal complexity. At the same time, they are compact and appended in terms of disk format, because they do not need random access.

41. How is the memory footprint of Redis?

Let me give you an example: 1 million key-value pairs (the key is 0 to 999999 is the string "hello world") use 100MB on my 32-bit Mac notebook. Only 16MB is needed to put the same data into a key, because the key value has a lot of overhead. Execution on Memcached has a similar result, but is a little less expensive than Redis, because Redis records type information, reference counts, and so on. Of course, the ratio of the two is much better when the key values are matched. 64-bit systems require more memory overhead than 32-bit ones, especially when key-value pairs are small, because pointers take up 8 bytes in 64-bit systems. But, of course, 64-bit systems support more memory, so 64-bit systems are more or less needed to run large Redis servers.

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

If you are using 32-bit Redis instances, you can make good use of collection type data such as Hash,list,sorted set,set, because usually many small Key-Value can be stored together in a more compact way.

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

Info

44. What happens when Redis runs out of memory?

If the upper limit is reached, Redis's write command returns an error message (but the read command returns normally. Or you can use Redis as a cache to use the configuration elimination mechanism, which will wash out the old content when the Redis reaches the memory upper 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 to use multiple CPU, you can consider shard.

46. How many keys can be stored in a Redis instance?

List, Set, Sorted Set, how many elements can they store at most? Redis can handle up to 232 keys in theory and has been tested in practice, with at least 250 million keys stored in each instance. We are testing some larger values. Any list, set, and sorted set can put 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 enables AOF to back up data, and the policy is set to synchronize once per second.

(3) for the speed of master-slave replication and the stability of connection, Master and Slave should be in the same local area network.

(4) try to avoid adding slave libraries to the stressed master libraries.

(5) Master-slave replication does not use graphic structure, but one-way linked list structure is more stable, that is, Master

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

Database

Wechat

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

12
Report