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
AOFThere 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.
Key-ValueThe 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 the
stringType 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.
listThe 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 pass
lpushPut the message into the list, and the consumer can go through the
rpopTake out the message and ensure the order of the message. If you need to implement message queuing with priority, you can also choose
sorted set。 and
pub/subFunctions 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.
listAs 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 pass
lpushStore it in a key of
listAfter that, we can go through
lrangeTake out the latest microblog.
Circular linked list
listIt can also be used as a circular linked list
RPOPLPUSH source destination
RPOPLPUSHIn one atomic time, perform the following two actions:
sourceThe last element in the (tail element) pops up and is returned to the client.
sourcePop up elements are inserted into the list
destinationThe header element of the list.
destinationIf 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.
sorted setAnd an algorithm to calculate the heat can easily create a heat ranking,
zrevrangebyscoreWe can get a sequence in reverse order of fractions,
zrankYou 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 use
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,
sorted setAll provided
incrMethods 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
stringAs a counter, set a
REGISTERED_COUNT_TODAYAnd set an expiration time to 0:00 a.m. when initializing, it will be used whenever the user successfully registers
incrThe 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
hashIt’s better to count. Set the key of this counter to
hashThe field of
view_numberAfter the corresponding operation
In hashThe field increases automatically.
- If the app has a post leaderboard function, select
sorted setLet’s set the key of the set to
POST_RANK。 After the user posts, use the
zincrbyIncrease the score of the user ID by 1.
sorted setIt will be reordered and the user’s position on the leaderboard will be updated in real time.
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 the
setThe 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 a
sinterAccording to the order
A:followerThe intersection of a and a get users who pay attention to each other. When a enters the home page of another user B,
B:followThe intersection of a and B is the common focus of a and B,
B:followerThe intersection of “a” and “B” is that the people a pays attention to also pay attention to B.
Starting with redis 2.6.12,
setThe command adds three parameters:
EX: sets the expiration time of the key (in seconds)
PX: sets the expiration time of the key (in milliseconds)
XX: when set to
XXWhen the key does not exist
Since 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 returns
false, indicating that the key was not added successfully, that is, someone is occupying the lock. And if you go back
true, indicates that if the lock is obtained, the operation can be continued, and the operation can be passed after the operation
delCommand 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 is the most common way to construct search functions. In redis, you can also use the
setTo establish an inverted index, here we use simple Pinyin + prefix to search for cities
Suppose a city
BeijingThrough the Pinyin Thesaurus
beijingThese two words are divided into several prefix indexes by prefix segmentation
beijing。 Use these indexes as
Index: North）And store
BeijingThe inverted index is established. Next, we only need to retrieve the corresponding keywords in the search
setAnd 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.