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 frequently asked questions about Redis

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

Today, I will talk to you about the common problems of Redis, which may not be well understood by many people. in order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.

How does 1.Redis optimize memory?

Use a hash table (hashes) as much as possible. Hash tables (that is, a small amount of memory stored in a hash table) use 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.

What is the use of pipes in 2.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.

What is the relationship between 3.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.

What are the suitable scenarios for 4.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 their 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 big 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 helps 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 backend that uses Redis as the 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 very easy for us to perform these operations, and Redis just happens to provide these two kinds of 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 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!

There are 2000w data in 5.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.

In fact, in addition to examining Redis, many companies attach great importance to highly concurrent and highly available technologies, especially front-line Internet companies, distributed,

JVM, spring source code analysis, micro-service and other knowledge points have become the required questions for the interview.

Under what circumstances will the 6.Redis cluster scheme make the entire cluster 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.

What should the 7.Redis cluster scheme do? What are the plans?

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.

Redis cluster

The characteristic of its own cluster is that its distributed algorithm is not consistent hash, but the concept of hash slot, as well as its own support node setting slave node. Take a look at the official document introduction. 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, considering partial package, alternative algorithm after node failure, automatic script recovery after data concussion, instance monitoring, and so on.

What are the internal codes of 8.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?

It can be implemented using Zset. Member is the task description, score is the execution time, and then the timer is used to scan. As soon as there is a task whose execution time is less than or equal to the current time, it will be executed immediately.

When 10.Redis looks for key in the cluster, how does it locate the specific node?

Use the crc16 algorithm to hash the key to hash the hash value to 16384, and get the specific slot according to the mapping information between the node and the slot (after connecting with the cluster, the client can get 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 instruction, plus a new node address to the client, and at the same time The client will refresh the local node slot mapping relationship. If the slot is being migrated, the redis cluster will return the asking instruction to the client. This is a temporary correction, and the client will not refresh the local node slot mapping relationship.

Do you know anything about the persistence of 11.Redis?

There are two ways of Redis persistence: RDB and AOF.

RDB: save database snapshots to disk in binary format.

AOF: record the state of the database by recording all commands and parameters that have been written to the database in the form of protocol text into an AOF file.

Under what circumstances will 12.Redis trigger the recycling of key?

Two situations: 1, scheduled (sampling) cleaning; 2, when executing the command, determine whether the memory exceeds the maxmemory.

What are the elimination strategies for 13.Redis key?

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

Do you know the 14.Redis transaction mechanism?

The concept of Redis transactions:

The essence of a Redis transaction is a collection of commands. Transactions support the execution of multiple commands at a time, and all commands in a transaction are serialized. During transaction execution, commands in the queue will be serialized sequentially, and commands submitted by other clients will not be inserted into the transaction execution command sequence.

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

Redis transactions have no concept of isolation levels:

Batch operations are cached before sending EXEC commands and are not actually executed, so there are no intra-transaction queries to see updates in the transaction, while out-of-transaction queries cannot be seen.

Redis does not guarantee atomicity:

In Redis, a single command is executed atomically, but the transaction is not guaranteed atomicity and there is no rollback. Any command execution line in the transaction fails, and the rest of the commands are still executed.

The three phases of a Redis transaction:

Start a transaction

Order to join the team

Execute a transaction

Redis transaction related commands:

Watch key1 key2.: monitor one or more key, and if the monitored key is changed by other commands before the transaction is executed, the transaction is interrupted (similar to optimistic locks)

Multi: marks the beginning of a transaction block (queued)

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

Discard: cancels the transaction and discards all commands in the transaction block

Unwatch: cancel watch's monitoring of all key

15. What should I do if I use the UV of the Redis statistics website?

UV is different from PV in that UV needs to be deduplicated. There are generally two options:

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

2. Use Bloom filter. Put the user uid for each visit into the Bloom filter. The advantage is provincial memory, while the disadvantage is that accurate UV can not be obtained. But it's a good choice for scenarios where you don't need to know the specific UV exactly, but only about the order of magnitude.

What about the big key in 16.Redis?

A big key refers to a key with a very large value. For example, a long string, or a very large set, and so on. Large key can cause two problems:

1. Data skew, such as excessive memory consumption of some nodes.

2. When deleting a large key or a large key expires automatically, it will cause a sudden drop in the QPS, because the Redis is a single thread.

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

What about the hot key in 17.Redis?

1. Disperse the hot key. For example: add different prefixes and suffixes to key, cache multiple key, so that each key is scattered to different nodes.

2. Adopt multi-level cache.

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

Cache invalidation means that a large number of caches expire at the same time, when the instantaneous pressure of DB soars. The reason for this is that the expiration time of key is set to the same. The solution is to introduce random factors into the expiration time of key, such as 5 minutes + random seconds.

Cache penetration means that if you query a database or a piece of data that is not available in the cache, the database will be queried all the time, and the access pressure to the database will increase. There are two solutions for cache penetration: caching empty objects: code maintenance is simple, but the effect is not good. Bloom filter: the code is complex to maintain and works well.

Cache avalanche means that the cache set expires in a certain period of time. At the moment, numerous requests directly bypass the cache and go directly to the request database. There are two reasons for cache avalanches: reids downtime. Most of the data is invalid.

There are two solutions for caching avalanches:

Build a high-availability cluster to prevent stand-alone redis from downtime.

Set different expiration times to prevent a large number of key invalidation between agreements.

Cache concurrency sometimes if the concurrent access to the website is high, if a cache fails, multiple processes may query DB at the same time and set the cache at the same time. If the concurrency is really high, this may also cause excessive pressure on DB and the problem of frequent cache updates. The general solution is to lock the DB. If the KEY does not exist, add the lock, then check the DB into the cache, and then unlock it. Other processes wait if they find a lock, and then check the cache or enter the DB query after unlocking.

How does a 19.Redis cluster select a database?

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

How does 20.Redis set and verify passwords?

Set password: config set requirepass 123456

Authorization password: auth 123456

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

Therefore, redis has the characteristics of high speed and data persistence. If you don't put the data in memory, the disk speed will be as fast as the performance of redis.

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

Why doesn't 22.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.

Is 23.Redis single threaded or multithreaded?

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

Prior to Redis6.0, both IO and execution threads were single-threaded.

Why is 24.Redis so fast?

1. Memory operation

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

3. Non-blocking IO model, epoll.

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

512M

What is the full name of 26.Redis?

Remote Dictionary Server .

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

Memory.

What are the data structures of 28.Redis?

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

These five kinds of knowledge are the most basic and important parts of Redis related knowledge.

What are the advantages of 29.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

thirty。 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 that it supports the preservation of multiple data structures, in addition, the maximum limit of a single value is 1GB, unlike memcached, which can only save 1MB data, so Redis can be used to achieve many useful functions.

For example, using his List to do FIFO two-way linked list, to achieve a lightweight high-performance message queue service, using his Set to 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.

After reading the above, do you have any further understanding of Redis FAQs? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

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