Redis high-concurrency project scenario design actual combat analysis (Taobao shopping cart, WeChat likes, Weibo relationship design, etc.)

Write in front

The video explanation about Redis high concurrency scenario design recommendation station B is very easy to understand!

The most complete Redis high-concurrency project scenario design in 2020 (Taobao shopping cart, WeChat likes, Weibo relationship design, etc.)

text

Distributed cache is an important component in a distributed system. It mainly solves the performance problems of hot data access in high concurrency and big data scenarios, and provides high-performance fast data access.

Common scenarios for using caching are: some data in the project is accessed frequently, causing service pressure on downstream DBs (such as MySQL). At this time, caching can be used to improve efficiency. Let's talk about the core design of various application scenarios of Redis in first-line enterprises such as BAT!

1. Common commands

Next, let’s take a look at the commonly used commands for each data structure. Let’s use a table to show them clearly:

img

Two, scene analysis

1.1string storage

img

1.2 String type usage scenarios

Scenario 1: Commodity inventory

From a business perspective, commodity inventory data is hot data, and transaction behavior will directly affect inventory. And Redis's own String type provides:

  1. set goods_id 10; Set the initial value of inventory of goods with id as good_id to 10;
  2. decr goods_id; When the goods are purchased, the inventory data is reduced by 1.

Scenarios by analogy : the number of times the product is viewed, the number of likes for questions or replies, etc. You can consider using Redis to implement this counting scenario.

Scenario 2: Time-sensitive information storage

Redis's data storage has the ability to automatically fail. That is, the stored key-value can be set to expire time: set(key, value, expireTime).

For example, a user needs to obtain a login verification code to log in to an App, and the verification code is valid within 30 seconds. Then we can use the String type to store the verification code and set an expiration time of 30 seconds.

[External link image transfer failed. The source site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-YVYYAIf9-1589895386136)(data:image/svg+xml;utf8,)]

2.1hash storage data

[External link image transfer failed. The source site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-EJFSijlP-1589895386138)(data:image/svg+xml;utf8,)]

2.2 Hash type usage scenarios

Redis needs to serialize and convert the objects when storing objects (for example: user information) and then store them.

Another form is to convert the object data into JSON structure data, and then store the JSON string in Redis.

For some object types, there is a more convenient type, which is to store according to the Hash type of Redis.

For example, we store some basic information of website users, we can use:

In this way, a user’s basic information is stored. The stored information is: {name: Xiaoming, phone: "123456", sex: "Male"}

Of course, there are many similar scenarios, such as storing order data, product data, and basic business information. Mainly Taobao shopping cart

[External link image transfer failed. The origin site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-rKfgbmuK-1589895386139)(data:image/svg+xml;utf8,)]

2.3 The advantages and disadvantages of implementing information storage

1. Native:

  • set user: 1:name james;
  • set user:1:age 23;
  • set user:1:sex boy;

Advantages: simple and intuitive, each key corresponds to a value

Disadvantages: too many keys, a lot of memory, user information is too scattered, not used in a production environment

2. Serialize and save the object

redis set user:1 serial ize (userInfo);

Advantages: simple programming, high memory usage if serialization is used

Disadvantages: serialization and deserialization have a certain overhead. When updating attributes, you need to take out all userInfo for deserialization, and then serialize to redis after updating

3.hash storage:

hmset user:1 name james age 23 sex boy

Advantages: simple and intuitive, reasonable use can reduce memory space consumption

Disadvantages: To control the conversion between ziplist and hashtable, Mhashtable consumes more memory.

3.1 List type usage scenarios

list is a linked list of strings sorted in the order of insertion. You can insert new elements at the head and tail (doubly linked list implementation, the time complexity of adding elements at both ends is O(1)).

[External link image transfer failed. The source site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-Zvx5IVDF-1589895386139)(data:image/svg+xml;utf8,)]

Scenario 1: Message queue implementation

At present, there are many professional message queue components Kafka, RabbitMQ, etc. We are here only using the characteristics of the list to achieve the message queue requirements. In the actual technology selection process, everyone can think carefully.

List storage is the storage form of a queue:

  1. lpush key value; Add a string element to the head of the key corresponding to the list;
  2. rpop key; remove the last element of the list, the return value is the removed element.

Scenario 2: The latest products

On the homepage of the trading website, there are often modules recommended for new products on the shelves. This module stores the top 100 latest products on the shelves.

At this time, use the list data structure of Redis to store the TOP 100 new products.

The Redis ltrim command trims a list so that the list will only contain the specified elements in the specified range.

Both start and stop count from 0, where 0 is the first element (header) in the list, and 1 is the second element.

4.1 Set type usage scenarios

set also stores a collection list function. Unlike list, set has the function of deduplication. When you need to store a list of information and require that the elements in the list cannot be repeated, it is more appropriate to use set at this time. At the same time, set also provides intersection, union, and difference.

For example, on a trading website, we will store information about products that users are interested in. When analyzing similar users, we can provide some basis by calculating the number of products of interest between two different users.

Get two users' similar products, and then determine the category of similar products to conduct user analysis.

Similar application scenarios also include support for scenarios such as following friends together in social scenarios, and similar interest tags.

[External link image transfer failed. The source site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-Ig9Ydf02-1589895386140)(data:image/svg+xml;utf8,)]

4.2 Set set special operation commands

setA={A,B,C} setB={B, C}

1) Intersection between set and set

sinter setA setB-->get set {B,C}

  1. Set and set union

sunion setA setB -->get set {A,B,C}

3) Difference between set and set

sdiff setA setB-->get set {A}

4.3 Set collection special operation command application scenarios

How to realize the micro-relationship design of Weibo? (Watching the video is more enjoyable)

www.bilibili.com/video/av921…

img

5.1 Zset ordered set

Commonly used in rankings. For example, a video website needs to list the videos uploaded by users, or the number of likes is related to the collection, and there should be no duplicate members.

img

5.2 Zset type usage scenarios

img

Guess you like

Origin blog.csdn.net/EnjoyEDU/article/details/109203223