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

Application scenarios of redis

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

Share

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

This article mainly discusses the application scenario of redis, which has certain reference value and can be used for reference by friends who need it. The following information is about the application scenario of redis.

One: cache-hot data

For hot data (data that is often queried but not often modified or deleted), the first choice is to use redis cache. After all, not all similar tools have QPS and strong stability, and compared with memcached, it also provides a wealth of data types to use. In addition, the data in memory also provides persistence mechanisms such as AOF and RDB to choose between cold, hot or hot and cold.

Please note that many people use spring's AOP to build automatic production and clearance of redis cache. The process may be as follows:

Query redis before Select database, if any, use redis data, abandon select database, if not, select database, and then insert the data into redis

Update or delete database money. Query whether the data exists in redis. If so, delete the data in redis first, and then the data in update or delete database.

The above operation is basically fine if the concurrency is very small, but in the case of high concurrency, please pay attention to the following scenarios:

For update to delete the data in redis first, at this time another thread executes the query and finds that there is no query in redis, instantly executes the query SQL, and inserts a piece of data into redis, back to that update statement just now, this miserable thread has no idea that that damn select thread has made a big mistake! So the error data in this redis will last forever until the next update or delete.

Two: counter

Applications such as counting clicks. Because of single thread, concurrency problems can be avoided, no errors can be guaranteed, and 100% millisecond performance! Yes.

Command: INCRBY

Of course, it's over, don't forget to persist, after all, redis just saved memory!

Three: queue

It is similar to messaging system, ActiveMQ,RocketMQ and other tools, but I think it is OK to use it simply. If it requires high data consistency, I still use professional systems such as RocketMQ.

Since redis returns the order of the added elements in the queue when adding data to the queue, it is possible to determine which user accesses this kind of business.

Queues can not only turn concurrent requests into serial, but also can be used as queues or stacks.

Four: bit operation (big data processing)

It is used in scenarios with hundreds of millions of data, such as check-in of hundreds of millions of user systems, statistics of re-login times, whether a user is online, and so on.

Think about Tencent 1 billion users. It takes a few milliseconds to find out whether a user is online. What can you do? Don't tell me to set up a key for each user and write it down one by one (you can calculate that the memory you need will be scary, and there are a lot of similar needs, how much more will Tencent Optical cost? no, no, no. (all right. Here you want to use the in place operation-- use the setbit, getbit, bitcount commands.

The principle is:

Build an array long enough in redis, each array element can only be 0 and 1, and then the subscript index of this array is used to represent the user id in our example above (must be numeric ha), then obviously, this multi-million-long array can build a memory system by subscript and element values (0 and 1), and the above scenarios can also be implemented. The commands used are: setbit, getbit, bitcount

Five: distributed lock and single thread mechanism

To verify the duplicate requests at the front end (similar cases can be freely expanded), you can filter through redis: each request stores request Ip, parameters, interfaces and other hash as key redis (idempotent request), sets how long the validity period is, and then retrieves the key in redis when the next request comes, and then verifies whether it has been repeatedly submitted within a certain period of time.

The second kill system, based on the single-thread feature of redis, prevents database "explosion".

Global incremental ID generation, similar to "second kill"

Six: the latest list

For example, the latest news list on the news list page, if the total number is very large, try not to use select a from A limit 10 as a low product, try redis's LPUSH command to build List, and stuff it one by one. But what if the memory is cleared? Also simple, if the query can not store key, use mysql query and initialize a List to redis.

This is the whole content of the usage scenario of redis.

After reading the above, do you have any further understanding of the application scenario of redis? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel. Thank you for reading.

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