Home >Database >Redis >Where is redis used?

Where is redis used?

藏色散人
藏色散人Original
2019-06-19 11:10:212516browse

Where is redis used?

Where is redis used?

redis application scenario

● Token generation

● SMS verification code

● Ranking list

● Message Queue

The data structure implementation of list in Redis is a doubly linked list, so it can be very conveniently applied to message queue (producer/consumer model). The producer of the message only needs to put the message into the list through lpush, and the consumer can take out 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 functionality can also be used as a publisher/subscriber model for messages. No matter which method is used, because Redis has a persistence function, there is no need to worry about message loss due to server failure. (Recommended: "Redis Video Tutorial")

If you have high requirements for data consistency, you should 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 in queues or stacks

Distributed lock

Verify repeated requests from the front end, which can be filtered through redis

The flash kill system, based on the single-threaded feature of redis, prevents database "explosion"

Global incremental ID generation, similar to "second kill"

Counter

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

The counting function should be one of the most suitable usage scenarios for Redis, because its high-frequency reading and writing characteristics can fully utilize the efficiency of Redis as an in-memory database. In the Redis data structure, string, hash and sorted set all provide the incr method for atomic increment operations.

Example:

If the application needs to display the number of registered users every day, you can use string as the counter, set a key named REGISTERED_COUNT_TODAY, and during initialization Set an expiration time to 0 am. Every time a user successfully registers, use the incr command to increase the key by 1. At the same time, after 0 am every day, the counter will be cleared to zero due to the expiration of the key.

Each Weibo has four attributes: number of likes, number of comments, number of retweets and number of views. In this case, it would be better to use hash for counting. Set the key of the counter to weibo:weibo_id, hash The fields are like_number, comment_number, forward_number and view_number. After the corresponding operation, the field in the hash is incremented through hincrby.

If the application has a post ranking function, select sorted set and set the key of the set to POST_RANK. When a user posts, use zincrby to increase the score of the user id by 1. The sorted set will be re-sorted, and the user's ranking position will be updated in real time.

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

Statement:
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