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 redis application scenarios?

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

What are the redis application scenarios? I believe that many novice rookies are ignorant about redis. Through the summary of this article, I hope you can gain more. The following information is about? The content of.

Redis is an open source API that is written in ANSI C language, supports the network, can be memory-based and persistent, and provides API in multiple languages.

Redis has many application scenarios. This simple list starts with 7 application scenarios:

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

Don't forget persistence, after all, redis only saves 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.

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.

Seven: ranking

Who gets the highest score and who ranks high. Command: ZADD

The above is the knowledge summary of redis application scenarios, the content is more comprehensive, the editor believes that there may be some knowledge points that we may see or use in our daily work. I hope you can learn more from this article.

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