Redis: 11. Simple social networking

Time:2021-2-20

brief introduction

The implementation of ad targeting is introduced in the front. It is a query intensive program, so every request sent to it will cause a lot of calculation. In this paper, we will implement a simple social networking site, which will reduce the work that users need to do when viewing the page.P184

Users and statusP185

User object stores the basic identity information of users, the number of followers of users, the number of status messages published by users and other information. It is the starting point of building other available and interesting data.P185

Status messages record what different users have said and what they communicate with each other. These status messages created by users are the real content of social networking sites.P185

User informationP185

User information usageHASHUser information includes: user name, the number of followers owned by the user, the number of people the user is following, the number of status messages published by the user, the registration date of the user, and some other meta information. Example:"user:139960061": {"login": "dr_josiah", "id": "139960061", "name": "Josiah Carlson", "followers": "176", "following": "79", "posts": "386", "signup": "1272948506"} P185

Create user P185

When creating a user, you need to create an object with the following number, followers number, and published status messages number set to 0 according to the user name specified by the user and the time stamp at that time.P185

In addition to the initialization of user information, the user name needs to be locked to prevent multiple requests from using the same user name to create a new user at the same time (you can use 08. To achieve automatic completion, distributed lock and distributed lock implemented in counting semaphore to lock).

Status messageP186

Status message usageHASHThe status message includes the message itself, the time of message publishing, the ID of the message publisher, and the user name (redundant user name is to avoid user information when displaying)HASHBecause the user name will not change) and other additional information about the status message. Example:"status:223499221154799616": {"message": "My pleasure. I was amazed that...", "posted": "1342908431", "id": "223499221154799616", "uid": "139960061", "login": "dr_josiah"} P186

When creating a message, you need to get the user name first, and then combine the relevant information to store it in theHASHIn the middle.P187

Home timelineP187

The home timeline is a list of status messages posted by users and the people they are following. The personal timeline is similar to the home timeline in that it contains only the status messages published by users themselves. Because the home page timeline is the main entry point for users to visit the website, the data must be as easy to obtain as possible.P187

The home page timeline is stored in an ordered collection. The member is the ID of the status message, and the score of the member is the timestamp of the status message. Example:"home:139960061": {..., "227138379358277633": "1342988984", ...} P188

There are two steps to get the home page timelineP188

  1. useZREVRANGEGet the ID list of the latest status message by paging
  2. Using assembly lines andHGETALLGet all the status messages in the status message ID list

Followers list and following listP189

The user’s following list and followers list are also stored in an ordered set. The member is the user’s ID, and the score of the member is the time stamp when the user begins to follow or be followed by someone. Example:"followers:139960061": {..., "558960079": "1342915440", "14502701": "1342917840", ...} "following:139960061": {..., "18697326": "1339286400", "558960079": "1342742400"} P188

When a user begins to pay attention to or stops paying attention to another user, it is necessary to update the following ordered set and followers ordered set of the two users(ZADD, ZREM)And modify the number of followers and the number of followers in their user information(HINCRBY)At the same time, it also needs the operator’s home page timeline to add or delete another user’s status message(ZUNIONSTORE)。 P189

The deletion operation requires two commands. Because there is no difference set command for ordered sets, there are two ways to implement it

  1. useZREVRANGEGet the member list of the ordered collection corresponding to another user’s personal home page, and then use theZREMRemove these members from the ordered set corresponding to the operation user’s home page pipeline
  2. Use firstZUNIONSTOREThe weight of the ordered set corresponding to the operation user’s home page pipeline is 1, and the weight of the ordered set corresponding to another user’s personal pipeline is 0MINAnd then use it againZREMRANGEBYSCORE key 0 0Remove all members with a score of 0 (pipeline merging can be used)

person one is in love with

The exercises support the operations of batch attention and cancellation of attention on a given basis. In fact, in most cases, single and batch operations are similar in business, and batch operations are likely to be supported in the future. In order to reduce the amount of back-end development, avoid changing interface signature or explosive growth of similar function interfaces, I often implement batch operation interfaces in advance. We usually can not only deal with the current requirements, but also understand the overall function and think from the perspective of users, and pay attention to possible changes as soon as possible, so as to develop an extensible interface.

Publishing and deleting status messagesP191

One of the most basic operations that users can perform is to publish status messages. We have implemented the logic of creating status messages, and then we will add new status messages to each follower’s home page timeline.P191

In order to make the publishing operation return as soon as possible, we will only add new status messages to the home page timeline of the first 1000 followers after creation. If the current publisher has more than 1000 followers, we will use 09. To implement the task queue in task queue, message pull and file distribution, and process the subsequent followers asynchronously.P192

When deleting a message, the same operation can be done. First, delete the status message itself, then process the statistical information of the publisher, then delete the message ID from the personal timeline, and finally delete the message ID from the homepage timeline of the former 1000 followers. If the number of followers exceeds 1000, then use the task queue to process the subsequent followers asynchronously.P193

Streaming APIP194

Some functions are constructed to broadcast simple events, and then the event listener responsible for data analysis receives and processes these events. The stream API request can continuously return data for a long time.P194

The function of stream API is to generate a time sequence with the passage of time, so that clients and other services on the whole network can know what is happening on the website in time.P195

Various decisions need to be made in the process of building flow API. These decisions are mainly related to the following three problems:P195

  • What events need to be exposed by stream API?

    • We need to disclose four kinds of user operations: publish message, delete message, pay attention to user and cancel paying attention to user. This section will take creating publish message event and deleting message event as examples
  • Do you need access restrictions? If necessary, how to achieve it?

    • At present, access restrictions are only considered when user privacy or system resources are involved
  • What filtering options should stream API provide?

    • We can not only get the filtered messages through the following filter (filtering based on users), detection filter (filtering based on keywords) and location filter, but also get some random messages through streams like Twitter’s firehouse (all public messages can be obtained) and sample (only a small number of public messages can be obtained).
Identify clientP198

In order to distinguish different clients, we need to identify them. In most cases, we only consider the logged in users. Then we use the user ID for identification. In order to verify the user ID at the same time, we can use JWT.

Filtering messagesP200

We will use redis’sPUBLISHOrders andSUBSCRIBEWhen a user publishes a message, the program will use thePUBLISHIt is sent to a channel, and each filter is sent through theSUBSCRIBETo subscribe to and receive messages of that channel, and when the messages matching the filter are found, return the messages to the web server (return the entity if the message is published, return the ID and current status if the message is deleted), and then the server sends these messages to the client.P200

This article starts with the official account: full Fu machine (click to view the original), open source in GitHub:reading-notes/redis-in-action
Redis: 11. Simple social networking