Home > Database > Redis > body text

What are the usage scenarios of redis?

WJ
Release: 2020-06-05 16:23:03
Original
2339 people have browsed it

What are the usage scenarios of redis?

1: Cache - hot data

Hot data (data that is often queried but not often modified or deleted), The first choice is to use redis cache. After all, the powerful bubbling QPS and extremely strong stability are not available in all similar tools. Compared with memcached, it also provides rich data types that can be used. In addition, the data in the memory is also provided. You can choose persistence mechanisms such as AOF and RDB, whether they are cold, hot or hot and cold.

It should be noted based on specific applications: Many people use spring's AOP to build automatic production and clearing of redis cache. The process may be as follows:

Select Query redis before the database, and use redis if necessary Data, give up the select database, if not, select the database, and then insert the data into redis

update or delete database, query redis to see if the data exists, if it exists, delete the data in redis first, and then update or delete the database The data in

The above operation is basically no problem if the amount of concurrency is very small, but in the case of high concurrency, please pay attention to the following scenario:

Delete the redis in order to update At this time, another thread executed a query and found that it was not in redis. It immediately executed the query SQL and inserted a piece of data into redis. Returning to the update statement just now, this miserable thread did not know the damn select just now. Thread made a huge mistake! So the erroneous data in redis will exist forever until the next update or delete.

2: Counter

Applications such as counting clicks. Due to the single thread, concurrency issues can be avoided, error-free guaranteed, and 100% millisecond performance! Cool.

Command: INCRBY

Of course you are done, don’t forget persistence, after all, redis only stores memory!

3: Queue

is equivalent to the messaging system, ActiveMQ, RocketMQ and other tools are similar, but I personally think it is okay to use it simply, if the data consistency requirements are high. Or use professional systems such as RocketMQ.

Since redis adds data to the queue by returning the number of the added element in the queue, it can be used to determine the number of users accessing this business.

The queue can not only change concurrent requests into into serial, and can also be used as queue or stack

Four: Bit operation (big data processing)

Used in scenarios with hundreds of millions of data, for example System check-ins for hundreds of millions of users, statistics on the number of duplicate logins, whether a user is online, etc.

Think about Tencent’s 1 billion users. You need to check whether a user is online within a few milliseconds. What can you do? Don’t say create a key for each user and then record them one by one (you can calculate the memory required, which will be terrifying, and there are many similar needs, how much Tencent will spend just for this...) Okay. Here you need to use in-place operations - use setbit, getbit, bitcount commands.

The principle is:

Construct a long enough array in redis. Each array element can only have two values ​​​​0 and 1, and then the subscript index of this array is used to represent what we have above The user ID in the example (must be a number), then it is obvious that this large array with a length of hundreds of millions can build a memory system through subscripts and element values ​​(0 and 1). The scenarios I mentioned above are also It can be achieved. The commands used are: setbit, getbit, bitcount

5: Distributed lock and single-thread mechanism

Verify repeated requests from the front end (similar situations can be freely expanded) , can be filtered through redis: each request uses the request IP, parameters, interfaces and other hashes as keys to store in redis (idempotent request), set the validity period, and then search in redis for the next request. This key is used to verify whether it is a repeated submission within a certain period of time

The flash kill system is based on the single-threaded feature of redis to prevent database "explosion"

Global incremental ID generation, similar to " Flash Sale"

6: Latest List

For example, the latest news list on the news list page. If the total number is large, try not to use select a from A limit. 10. For this kind of low-quality goods, try the LPUSH command of redis to build a List, and just insert them one by one in order. But what if the memory is cleared? It’s also simple. If you can’t query the storage key, just use mysql to query and initialize a List into redis.

The above is the entire introduction to redis usage scenarios.

Related references:Redis Tutorial

The above is the detailed content of What are the usage scenarios of redis?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:51dev.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template