Summary of redis application scenarios

Time:2020-10-24

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 providesRDBandAOFThere 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-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 thestringType 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

    1. 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).
    2. 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

RedislistThe 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 passlpushPut the message into the list, and the consumer can go through therpopTake 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/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.

Timeline

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 passlpushStore it in a key ofLATEST_WEIBOOflistAfter that, we can go throughlrangeTake out the latest microblog.

Circular linked list

listIt can also be used as a circular linked listRPOPLPUSH source destination

commandRPOPLPUSHIn one atomic time, perform the following two actions:

  • ListsourceThe last element in the (tail element) pops up and is returned to the client.
  • takesourcePop up elements are inserted into the listdestination, asdestinationThe header element of the list.

IfsourceanddestinationIf 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 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 usezcardzrank)。

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,stringhashandsorted setAll providedincrMethods 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 usedstringAs a counter, set aREGISTERED_COUNT_TODAYAnd set an expiration time to 0:00 a.m. when initializing, it will be used whenever the user successfully registersincrThe 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 numberhashIt’s better to count. Set the key of this counter toweibo:weibo_idhashThe field oflike_numbercomment_numberforward_numberandview_numberAfter the corresponding operationhincrbysendIn hashThe field increases automatically.
  • If the app has a post leaderboard function, selectsorted setLet’s set the key of the set toPOST_RANK。 After the user posts, use thezincrbyIncrease 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.

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 thesetThe 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

    So throughsinterAccording to the orderA:followandA:followerThe intersection of a and a get users who pay attention to each other. When a enters the home page of another user B,A:followandB:followThe intersection of a and B is the common focus of a and B,A:followandB:followerThe 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,stringOfsetThe 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 toNXSet toXXWhen 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 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 operationdelCommand 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 thesetTo establish an inverted index, here we use simple Pinyin + prefix to search for cities

Suppose a cityBeijingThrough the Pinyin ThesaurusBeijingTobeijingThese two words are divided into several prefix indexes by prefix segmentationnorthBeijingbbebeijinandbeijing。 Use these indexes assetFor example:Index: North)And storeBeijingThe inverted index is established. Next, we only need to retrieve the corresponding keywords in the searchsetAnd 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.