Home  >  Article  >  Database  >  A summary of 30 frequently asked questions about Redis!

A summary of 30 frequently asked questions about Redis!

藏色散人
藏色散人forward
2021-09-25 16:22:353498browse

1.How does Redis perform memory optimization?

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, you should store all the user's information in a hash table. .

2. What is the use 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 the 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 feature, which greatly speeds up the process of downloading new emails from the server.

3. 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).

4.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 is easy to find 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 plug-in 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) Queue

One of the great advantages of Redis in the field of memory storage engines is that it provides list and set operations, which allows Redis to be used as 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 the broker. You can view it from here.

(4) Ranking/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, we want to get the top 10 users from the sorted set - we call them "user_scores", we just need to execute like the following: Of course, this is assuming that you are Sort in ascending order based on your users' scores. If you want to return the user and the user's score, you need to execute it like this:

ZRANGE user_scores 0 10 WITHSCORES

Agora Games is a good example, implemented in Ruby, with its ranking list It uses Redis to store data, you can see it here.

(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!

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

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

In fact, in addition to examining Redis during interviews, many companies attach great importance to high-concurrency and high-availability technologies, especially first-line Internet companies, distributed,

JVM, spring source code analysis, microservices and other knowledge Point is already a must-answer question in interviews.

6. 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 and will be unavailable.

7. What should be done with the Redis cluster solution? What are the plans?

codis

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

redis cluster

The cluster that comes with 3.0 is characterized by the fact that its distributed algorithm is not consistent hashing, but the concept of hash slots, and it supports node setting slave nodes. See the official documentation for details. Implemented at the business code layer, several unrelated redis instances are created. At the code layer, the hash calculation is performed on the key, and then the corresponding redis instance is used to operate the data. This method has relatively high requirements for hash layer code. Considerations include alternative algorithm solutions after node failure, automatic script recovery after data shock, instance monitoring, etc.

8.What are the internal encodings of Redis String?

int, embstr, raw

Integers below 10000 will use the int constant in the cache.

Length less than or equal to 44 bytes: embstr encoding

Length greater than 44 bytes: raw encoding

9. How to use Redis as a delay queue? accomplish?

Can be implemented using Zset. Member is the task description, score is the execution time, and then use a timer to scan regularly. Once there is a task whose execution time is less than or equal to the current time, it will be executed immediately.

10. How does Redis locate the specific node when searching for keys in the cluster?

Use the crc16 algorithm to hash the key, take the hash value modulo 16384, and obtain the specific slot based on the mapping information of the node and the slot (after establishing a connection with the cluster, the client can obtain the slot Mapping information), find the specific node address and go to the specific node to find the key. If the key is not on this node, the redis cluster will return the moved command and add the new node address to the client. At the same time, the client will refresh the local node slot. Bit mapping relationship If the slot is being migrated, the redis cluster will return the asking command to the client. This is a temporary correction. The client will not refresh the local node slot mapping relationship

11.Redis Have you ever understood persistence?

Redis persistence has two methods: RDB and AOF.

RDB: Save the database snapshot to disk in binary form.

AOF: Record all commands and parameters written to the database to the AOF file in the form of protocol text, thereby recording the database status.

12. Under what circumstances will Redis trigger key recycling?

2 situations: 1. Regular (sampling) cleaning; 2. When executing the command, determine whether the memory exceeds maxmemory.

13. What are the elimination strategies for Redis key?

8 species: noeviction, volatile-lru, volatile-lfu, volatile-ttl, volatile-random, allkeylru, allkeys-lfu, allkeys-random

14. Have you understood the Redis transaction mechanism?

The concept of Redis transaction:

The essence of Redis transaction is a set of commands. Transactions support executing multiple commands at one time, and all commands in a transaction will be serialized. During the transaction execution process, the commands in the queue will be executed serially in order, and command requests submitted by other clients will not be inserted into the transaction execution command sequence.

Redis transaction is a one-time, sequential and exclusive execution of a series of commands in a queue.

Redis transactions have no concept of isolation level:

Batch operations are put into the queue cache before sending the EXEC command and will not be actually executed, so there are no queries within the transaction to see. Updates in the transaction cannot be seen by queries outside the transaction.

Redis does not guarantee atomicity:

In Redis, a single command is executed atomically, but transactions do not guarantee atomicity and there is no rollback. If any command in the transaction fails to execute, the remaining commands will still be executed.

Three stages of Redis transaction:

Start transaction

Command enqueue

Execute transaction

Redis transaction related commands:

watch key1 key2 ...: Monitor one or more keys. If the monitored key is changed by other commands before the transaction is executed, the transaction will be interrupted (similar to optimistic locking)

multi: Mark the start of a transaction block (queued)

exec: Execute the commands of all transaction blocks (once exec is executed, the previously added monitoring locks will be canceled)

discard: Cancel Transaction, abandon all commands in the transaction block

unwatch: Cancel watch’s monitoring of all keys

15. How to use Redis to count the UV of the website?

UV is different from PV, UV needs to be deduplicated. There are generally two solutions:

1. Use BitMap. What is stored is the user's uid. When calculating UV, just do the bitcount.

2. Use Bloom filter. Put the user uid of each visit into the bloom filter. The advantage is that it saves memory, but the disadvantage is that accurate UV cannot be obtained. But it is a good choice for scenes that do not need to know the specific UV accurately, but only need a rough order of magnitude.

16. How to deal with large keys in Redis?

Big key refers to a key with a particularly large value. For example, a very long string, or a large set, etc. Large keys will cause two problems:

1. Data skew, for example, the memory usage of some nodes is too high.

2. When a large key is deleted or a large key automatically expires, it will cause a sudden drop in QPS because Redis is single-threaded.

Solution: A large key can be fragmented, for example, a large set can be divided into multiple small sets.

17. How to deal with hot keys in Redis?

1. Distribute hot keys. For example: add different suffixes to the key, cache multiple keys, so that each key is distributed to different nodes.

2. Use multi-level cache.

18. Cache invalidation? Cache penetration? Cache avalanche? Cache concurrency?

Cache invalidation Cache invalidation means that a large number of caches fail at the same time, and the instantaneous pressure on the DB soars. The reason for this phenomenon is that the expiration time of the keys is set to the same value. The solution is to introduce random factors into the key's expiration time, such as 5 minutes and random seconds.

Cache penetration Cache penetration refers to querying a piece of data that is neither in the database nor in the cache. The database will always be queried, and the access pressure on the database will increase. The solutions for cache penetration are as follows Type 2: Cache empty objects: Code maintenance is simpler, but the effect is not good. Bloom filter: The code is complex to maintain, but the effect is very good.

Cache avalanche Cache avalanche refers to the centralized expiration of caches in a certain period of time. At this moment, countless requests directly bypass the cache and directly request the database. There are two reasons for cache avalanche: reids downtime. Most of the data is invalid.

There are two solutions to cache avalanche:

Build a highly available cluster to prevent stand-alone redis from going down.

Set different expiration times to prevent a large number of keys from invalidating within the agreement period.

Cache concurrency Sometimes if the website has high concurrent access, if a cache fails, multiple processes may query the DB at the same time and set up the cache at the same time. If the concurrency is really large, this may also cause excessive pressure on the DB. , and there is also the problem of frequent cache updates. The general solution is to lock when checking the DB. If the KEY does not exist, lock it, then check the DB into the cache, and then unlock it; if other processes find that there is a lock, wait, and then wait until it is unlocked before checking the cache or entering the DB. Inquire.

19. How to choose a database for Redis cluster?

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

20.How to set password and verify password in Redis?

Set password: config set requirepass 123456

Authorization password: auth 123456

21. 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 the memory, the disk I/O speed will seriously affect the performance of redis.

As memory becomes cheaper and cheaper today, redis will become more and more popular. If the maximum memory usage is set, new values ​​cannot be inserted after the number of existing data records reaches the memory limit.

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

23. Is Redis single-threaded or multi-threaded?

Redis6.0 uses multi-threaded IO, but the execution of commands is still single-threaded.

Before Redis 6.0, the IO thread and execution thread were both single-threaded.

24. Why is Redis so fast?

1. Memory operation;

2. Single thread, eliminating the overhead of thread switching and lock competition;

3. Non-blocking IO model, epoll.

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

512M

26. What is the full name of Redis?

Remote Dictionary Server.

27.What physical resources does Redis mainly consume?

Memory.

28.What data structures does Redis have?

Redis has 5 basic data structures, which are: string (string), list (list), hash (dictionary), set (set) and zset (ordered set).

These 5 types are the most basic and important parts of Redis-related knowledge.

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

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

Redis is essentially a Key-Value type in-memory database, much like memcached. The entire database is loaded and operated in memory, and the database data is flushed to the hard disk through asynchronous operations at regular intervals for storage.

Because it is a pure memory operation, Redis's performance is very good. It can handle more than 100,000 read and write operations per second, and it is the fastest Key-Value DB with known performance.

The excellence of Redis is not just 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 be used to achieve Lots of useful features.

For example, use his List to make a FIFO doubly linked list to implement a lightweight high-performance message queue service, and use his 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.

Recommended learning: "redis video tutorial"

The above is the detailed content of A summary of 30 frequently asked questions about Redis!. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:码农有道. If there is any infringement, please contact admin@php.cn delete