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-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

This article mainly introduces what redis application scenarios are, which can be used for reference. Friends who need it can refer to it. I hope you will learn a lot after reading this article. Next, let the editor take you to learn about it.

Where is redis used?

Redis application scenario

● token (Token) generation

● SMS verification code

● ranking

● message queuing

The data structure of list in Redis is a two-way linked list, so it can be easily applied to message queues (producer / consumer model). The producer of the message only needs to put the message into the list through lpush, and the consumer can retrieve the message through rpop, and the order of the message can be guaranteed. If you need to implement a message queue with priority, you can also choose sorted set. The pub/sub function can also be used as a message for the publisher / subscriber model. Either way, because Redis has persistence, there is no need to worry about message loss due to a server failure. (recommended: "Redis Video tutorial")

If there is a high demand for data consistency, then you should 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.

Distributed lock

Verify the duplicate requests at the front end, which can be filtered through redis

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

Global incremental ID generation, similar to "second kill"

Counter

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

The counting function should be one of the most suitable scenarios for Redis, because its high-frequency read and write characteristics can take full advantage of the efficiency of Redis as an in-memory database. In the data structure of Redis, string, hash and sorted set all provide incr methods for atomic self-increment operations.

Example:

If the application needs to display the number of registered users per day, it can use string as a counter, set a key called REGISTERED_COUNT_TODAY, and set it an expiration time up to 0: 00 a.m. during initialization, and use the incr command to increase the key by 1 every time the user registers successfully. At the same time, after 0: 00 a.m. every day, this counter will clear the value because the key expires.

Each Weibo has four attributes: number of likes, number of comments, number of retweets and number of views. It is better to count with hash. Set the key of this counter to the field of weibo:weibo_id,hash as like_number, comment_number, forward_number and view_number, and use hincrby to increase the field in the hash after the corresponding operation.

If the app has a post ranking feature, select sorted set and set the key of the collection to POST_RANK. When a user posts, use zincrby to increase the score of the user's id by 1. Sorted set will reorder, and the location of the user's ranking will be updated in real time.

Thank you for reading this article carefully. I hope the editor will share what is helpful to you in the redis application scenario. At the same time, I also hope that you will support us, pay attention to the industry information channel, and find out if you encounter problems. Detailed solutions are waiting for you to learn!

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