Redis application scenarios
As a non relational database, redis not only has significant advantages in access speed, but also supports a variety of data types, which can cover many application scenarios in system development. Some of the scenarios listed below are seen from other people’s blogs on the Internet, and some of them have tried some solutions during their own development, hoping to bring inspiration to future development.
Before talking about the application scenarios, let’s talk about some suggestions on using redis
Suggestions for use
- The fast speed of redis is based on the memory database, but the memory of a server is much more expensive than the disk, so don’t put everything into redis at the beginning of the project. In this way, when the amount of data comes up, the memory will not be enough, and the gain will not be worth the loss. Reasonable use of limited memory, read (write) frequent hot data in redis to better feel its performance improvement.
- Although redis provides
RDB
andAOF
There are two persistence methods, but it is generally believed that the persistence of redis is not very reliable. Do not rely on redis for development of very important data, or at least not only persist in redis - The performance of MySQL has been very good after continuous optimization, so when the data structure and access efficiency provided by MySQL can meet the requirements, do not introduce redis. If one component is introduced, there will be one more possible failure node. Especially in the scenario of maintaining data consistency, the data (such as user balance) should only be placed in the database, unless you know how to solve the distribution of the test system Type of transaction.
cache
AsKey-Value
The first application scenario that redis will think of is as a data cache. However, using redis to cache data is very simple. You only need to use thestring
Type can save the serialized object, but there are some points to note:
- It is necessary to ensure that the key of different objects will not be repeated, and the key should be as short as possible. Generally, it is composed of class name (table name) and primary key.
- It is also important to choose an excellent serialization method to improve the efficiency of serialization and reduce the memory consumption.
-
There are generally two methods for consistency between cache content and database
- Only after the database query, the object is put into the cache. If the object is modified or deleted, the corresponding cache is cleared directly (or set as expired).
- After the database is added and queried, the object is put into the cache, the cache is updated after modification, and the corresponding cache is cleared (or set to expiration) after deletion.
Message queuing
Redislist
The implementation of the data structure of is bidirectional linked list, so it can be applied to message queue (Producer / consumer model) very conveniently. The producer of the message only needs to passlpush
Put the message into the list, and the consumer can go through therpop
Take out the message and ensure the order of the message. If you need to implement message queuing with priority, you can also choosesorted set
。 andpub/sub
Functions can also be used as messages for the publisher / subscriber model. No matter what method is used, because redis has the persistence function, there is no need to worry about the loss of messages due to server failure.
Timeline
list
As a bidirectional linked list, it can not only be used as a queue. If you use it as a stack, it becomes a common timeline. When the user sends the microblog, they all passlpush
Store it in a key ofLATEST_WEIBO
Oflist
After that, we can go throughlrange
Take out the latest microblog.
Circular linked list
list
It can also be used as a circular linked listRPOPLPUSH source destination
commandRPOPLPUSH
In one atomic time, perform the following two actions:
- List
source
The last element in the (tail element) pops up and is returned to the client. - take
source
Pop up elements are inserted into the listdestination
, asdestination
The header element of the list.
Ifsource
anddestination
If it is the same, the tail element in the list is moved to the header and the element is returned, which can be regarded as the rotation operation of the list.
For example, if there is a process to complete the dispatch task, the application sent by the user needs to be distributed to the staff in turn. Then, the identity of the staff member can be maintained in the circular list. After reading the ID from the end of the list, the corresponding ID will be placed in the column header, so as to cycle.
Ranking List
usesorted set
And an algorithm to calculate the heat can easily create a heat ranking,zrevrangebyscore
We can get a sequence in reverse order of fractions,zrank
You can get the position of a member in the ranking list (it is the position when the scores are arranged in positive order. If you want to get the position of a member in reverse order, you need to usezcard
–zrank
)。
Counter
The counting function should be one of the most suitable scenarios for redis, because its high-frequency read-write feature can fully exert the efficiency of redis as a memory database. In the data structure of redis,string
、hash
andsorted set
All providedincr
Methods are used for atomic auto increment operations. The following examples illustrate their respective usage scenarios:
- If the application needs to display the number of registered users per day, it can be used
string
As a counter, set aREGISTERED_COUNT_TODAY
And set an expiration time to 0:00 a.m. when initializing, it will be used whenever the user successfully registersincr
The command increases the key by 1. At the same time, after 0:00 a.m. every day, the counter will reset the value because the key has expired. - Each microblog has four attributes: like number, comment number, forwarding number and browsing number
hash
It’s better to count. Set the key of this counter toweibo:weibo_id
,hash
The field oflike_number
、comment_number
、forward_number
andview_number
After the corresponding operationhincrby
sendIn hash
The field increases automatically. - If the app has a post leaderboard function, select
sorted set
Let’s set the key of the set toPOST_RANK
。 After the user posts, use thezincrby
Increase the score of the user ID by 1.sorted set
It will be reordered and the user’s position on the leaderboard will be updated in real time.
Good friends
This scene was first seen in a PPT about the application of redis on Weibo. It was mentioned that redis in microblog was mainly used in counting and friend relationship. At that time, I didn’t know much about the usage of friend relationship. Later, it was introduced in the design and implementation of redis that the author first used redis in the hope that it could be used through theset
The traditional database can not quickly calculate the intersection in the set. Later, I think that the current business scenario of microblog can be realized in this way, so let’s guess:
For a user a, store its attention and fans’ user ID in two sets
-
A:follow
: store all user IDs that a follows -
A:follower
: store the user ID of all fans of aSo through
sinter
According to the orderA:follow
andA:follower
The intersection of a and a get users who pay attention to each other. When a enters the home page of another user B,A:follow
andB:follow
The intersection of a and B is the common focus of a and B,A:follow
andB:follower
The intersection of “a” and “B” is that the people a pays attention to also pay attention to B.
Distributed lock
Starting with redis 2.6.12,string
Ofset
The command adds three parameters:
-
EX
: sets the expiration time of the key (in seconds) -
PX
: sets the expiration time of the key (in milliseconds) -
NX
|XX
: when set toNX
Set toXX
When the key does not existSince this operation is atomic, you can simply implement a distributed lock based on it. For example:
set key "lock" EX 1 XX
If this operation returnsfalse
, indicating that the key was not added successfully, that is, someone is occupying the lock. And if you go backtrue
, indicates that if the lock is obtained, the operation can be continued, and the operation can be passed after the operationdel
Command to release the lock. And even if the program does not release the lock for some reasons, the lock will be released automatically after 1 second due to the expiration time, which will not affect the operation of other programs.
Inverted index
Inverted index is the most common way to construct search functions. In redis, you can also use theset
To establish an inverted index, here we use simple Pinyin + prefix to search for cities
Suppose a cityBeijing
Through the Pinyin ThesaurusBeijing
Tobeijing
These two words are divided into several prefix indexes by prefix segmentationnorth
、Beijing
、b
、be
…beijin
andbeijing
。 Use these indexes asset
For example:Index: North
)And storeBeijing
The inverted index is established. Next, we only need to retrieve the corresponding keywords in the searchset
And get the ID.
At present, I only know the application scenarios of these data types. If you have application experience of other scenarios, you are welcome to exchange and supplement. In addition, when you are asked why you use redis, don’t simply say it is fast, If only cache is used in the system, you can at least provide some QPS data of MySQL and redis, or the average response time of the program before and after using redis to confirm your point.