Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the design idea of redis flash sale activity?

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "what is the design idea of redis flash sale activity". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Redis solves highly concurrent transaction activities such as killing / grabbing red packets.

Synchronize the second kill inventory from the database to Redis Sorted Set 30 minutes before the start of the second kill

The user's second kill inventory is put into the Sorted Set of the limited number of second kill.

After the second kill reaches the specified number of second kill, the Sorted Set no longer accepts the second kill request and displays the return flag.

After the complete completion of the flash sale activity, the scheduled task synchronizes the Redis data to the database, and the flash sale activity ends

A brief introduction to redis:

1,Redis

Rich data structures (Data Structures)

Redis ordered collections, like Redis collections, are collections that do not contain the same strings.

Each member of an ordered set is associated with a score, which is used to rank the members of an ordered set from the lowest to the highest (ranking application, take TOP N operation)

With ordered collections, you can add, delete and update elements very quickly (O (log (N).

Elements are sorted at insertion time, so a range of elements is quickly obtained through score or position (applications that require precise expiration times)

Easy access to anything you need: ordered elements, fast existence testing, quick access to the middle elements of the collection

Create a ranking in a giant online game and use ZADD to update it whenever a new record is created. You can easily use ZRANGE to get the top user, or you can provide a user name and then use ZRANK to get his ranking in the rankings. Using ZRANK and ZRANGE at the same time, you can get a list of users with the same score as the specified user. All these operations are very fast.

Ordered collections are often used to index data stored in Redis. For example, if you have a lot of hash to represent users, you can use an ordered collection whose age field is used as a score and user ID as a value. Using ZRANGEBYSCORE, you can easily and quickly retrieve all users of a given age group.

Redis Hashes is a mapping between string fields and string values

Although Hashes is mainly used to represent objects, they can also store many elements

The Redis collection is an unordered set of strings that do not allow the existence of the same member (Uniq operation, which gets all the data weight values for a certain period of time)

Some server commands are supported to perform set operations from existing sets, such as merging (union: union), intersection (intersection: intersection), subtraction, finding out different elements (common friends, second friends).

Track a unique thing with a collection. Want to know all the independent IP that visit a blog post? Just use SADD to handle one page visit at a time. Then you can be sure that the duplicate IP will not be inserted (using uniqueness, you can count all the individual IP visits to the site)

Redis collections represent relationships very well. You can create a tagging system and then use collections to represent a single tag. You can then use the SADD command to add all the ID of all objects that own tag to the collection to represent this particular tag. If you want all the ID of all objects with 3 different tag at the same time, then you need to use SINTER

Use the SPOP or SRANDMEMBER command to get elements randomly

A Redis list is a simple list of strings sorted in the order in which they are inserted

You can add an element to the head (left: LPUSH) or tail (right: RPUSH) of the list.

A list can contain up to 232-1 elements (4294967295, with more than 4 billion elements per table)

Build a timeline model in the social network, use LPUSH to add new elements to the user's timeline, and use LRANGE to retrieve some recently inserted entries

You can use both LPUSH and LTRIM to create a list that will never exceed a specified number of elements and remember the last N elements at the same time

Lists can be used as primitive for messaging, for example, the well-known Resque Ruby library used to create background tasks

Redis strings can contain any type of data

A value of a string type can store up to 512m bytes of content

Use INCR command clusters (INCR, DECR, INCRBY) to use strings as atomic counters

Use the APPEND command to add content after a string

String (String)

List (List)

Collection (Set)

Hash (Hashes)

Ordered set (Sorted Sets)

Replication (Replication, Redis replication is easy to use, it is configured to allow replicas of slave Redis Servers or Master Servers)

A Master can have multiple Slaves

Slaves can accept links from other slave through the interface. Besides accepting links from slaves under the same master, it can also accept links from other slaves in the same structure diagram.

Redis replication is non-blocking in master segments, which means that master can also accept queries when performing synchronization on the same or more slave sides

Replication is also non-blocking on the slave side. Suppose you configure redis in redis.conf. When slave is performing a new synchronization, it can still use the old data information to provide queries. Otherwise, you can configure that when redis slaves goes to master and loses contact, slave will send a client error.

In order to have multiple slaves to do read-only queries, replication can be repeated 2 or even more times, with scalability (for example, slaves conversations and repeated sorting operations, it is relatively simple to have multiple data redundancy)

He can use replication to avoid saving data on the master side, as long as the master side redis.conf is configured, it can avoid saving (all save operations), and then save it on the slave side in real time through the slave link.

LRU expiration processing (Eviction)

Redis allows you to set a different expiration time for each key, which will be automatically deleted from the server when they expire (EXPIRE)

The EVAL and EVALSHA commands start with Redis version 2.6.0, and you can evaluate Lua scripts using the built-in Lua interpreter.

Redis uses a single Lua interpreter to run all scripts, and Redis ensures that scripts are executed in an atomic manner: when a script is running, no other scripts or Redis commands are executed. This is similar to a transaction surrounded by MULTI / EXEC. To other clients, the effect of the script is either invisible (not visible) or completed (already completed)

LRU expiration processing (Eviction)

Business

MULTI, EXEC, DISCARD, and WATCH are the basis of Redis transactions

A transaction is a separate isolation operation: all commands in the transaction are serialized and executed sequentially. During the execution of a transaction, it will not be interrupted by command requests sent by other clients.

All or none of the commands in the transaction are executed, and the EXEC command is responsible for triggering and executing all commands in the transaction.

Redis's Transactions does not provide strict ACID transactions.

Transactions still provides the basic function of packaging and execution of commands: it ensures that a series of commands are executed sequentially, with other client commands plugged in.

Redis also provides a Watch function. You can Watch a key and then execute Transactions. In the process, if the value of the Watched is modified, the Transactions will discover and refuse to execute.

Data persistence

Use both persistence features at the same time

Characteristics

Advantages

Shortcoming

AOF persistence method records every operation written to the server

When redis restarts, the AOF file will be loaded first to recover the original data, because under normal circumstances, the dataset saved by the AOF file is more complete than that saved by the RDB file.

Using AOF will make your Redis more durable: you can use different fsync strategies: no fsync, fsync per second, fsync every time you write

The AOF file is a log file that is appended only, so there is no need to write to seek

Redis can automatically rewrite AOF in the background when the AOF file size becomes too large.

The AOF file saves all writes to the database in an orderly manner, which are saved in the format of the Redis protocol, so the contents of the AOF file are easy to read and parse. Exporting (export) AOF files is also very simple

For the same dataset, the volume of the AOF file is usually larger than that of the RDB file.

Depending on the fsync strategy used, AOF may be slower than RDB

Characteristics

Advantages

Shortcoming

RDB persistence enables snapshot storage of your data at specified time intervals.

RDB is a very compact file, which saves the dataset at a certain point in time, so it is very suitable for the backup of dataset.

RDB is a compact, single file that is ideal for disaster recovery

When RDB saves the RDB file, the only thing the parent process needs to do is to fork out a child process, all the subsequent work is done by the child process, and the parent process does not need to do other IO operations, so RDB persistence can maximize the performance of redis.

Compared with AOF, RDB is faster when recovering large data sets.

If you want to lose the least data when redis stops working unexpectedly (such as a power outage), then RDB is not suitable. It is a heavy task for Redis to keep the entire dataset intact.

RDB needs frequent fork subprocesses to save the dataset to the hard disk. When the dataset is large, the fork process is very time-consuming, which may cause Redis to be unable to respond to client requests in milliseconds. If the dataset is large and the CPU performance is not very good, this situation will last for 1 second, and AOF also requires fork, but you can adjust the frequency of rewriting log files to improve the durability of the dataset.

RDB

AOF

Choice

Distributed system

Fast, lightweight, reduce the number of back-end Cache Server connections, easy to configure, support ketama, modula, random, common hash slicing algorithm

As far as the client is concerned, the redis cluster is transparent, the client is simple, and it can be expanded dynamically.

When Proxy is a single point and deals with consistent hash, there is no brain fissure problem in cluster node availability testing.

High-performance and CPU-intensive, while redis node clusters have multiple CPU resources that are redundant and can be deployed on redis node clusters without additional equipment

When Master is dead, VIP drifts to Slave;Slave. Keepalived informs redis to execute: slaveof no one, and starts to provide business.

When the Master is up, the VIP address remains unchanged, and the keepalived of Master instructs redis to execute slaveof slave IP host, starting as slave synchronization data.

And so on.

Redis Cluster (Redis 3 version)

Keepalived

Twemproxy

High availability (HA)

The single Mmurs structure is suitable for the business model in which different user data are associated, but the application can achieve the separation of read and write. Master mainly provides write operations, while Slave mainly provides read operations, making full use of hardware resources.

The double (multi) Mater Redis structure is suitable for business models where there is no or less data association between users. The read and write efficiency is two (more) times that of the single Mmurs, but it requires that a single server can bear the resource requirements of two Mmurs in case of failure.

The dual Mmurs structure is characterized by configuring a Master Redis on each server and deploying a Slave Redis at the same time. Four Redis Sentinel are monitored by two Redis simultaneously. Two Master Redis can provide read and write services to applications at the same time. Even if one server fails, the other server can run two Master Redis at the same time to provide read and write services.

The disadvantage is that the data can not be shared between the two Master redis, so it is not suitable for applications with a large number of user data associations.

The single Mmurs structure is characterized by configuring Master Redis (Redis-1M) and Master Sentinel (Sentinel-1M) in the Master server.

Configure Slave Redis (Redis-1S) and Slave Sentinel (Sentinel-1S) in the Slave server

Master Redis can provide read-write service, but Slave Redis can only provide read-only service. Therefore, in the case of heavy business pressure, we can choose to put the read-only business in Slave Redis.

Monitoring (Monitoring): Redis Sentinel monitors the running status of master and slave servers in real time

Reminder (Notification): when there is a problem with a monitored Redis server, Redis Sentinel can send a notification to the system administrator or send notification to other programs through API

Automatic failover (Automatic failover): when a master server does not work properly, Redis Sentinel can upgrade one slave server to the master server and configure the other slave servers to use the new master server. When the application connects to the Redis server, Redis Sentinel tells you the new primary server address and port

Redis Sentinel (a cluster management tool that comes with redis)

Single Mmurs structure

Double Mmurs structure

Comparison of single Mmurs structure and double Mmurs structure

Publish / subscribe (Pub/Sub)

Monitoring: Redis-Monitor

Historical redis runs queries: CPU, memory, hit rate, number of requests, master-slave switching, etc.

Real-time monitoring curve

2. Data type Redis usage scenario

String

Counter application

List

The operation of taking the latest N pieces of data

Message queue

Delete and filter

Real-time analysis of what is happening for data statistics and prevention of spam (combined with Set)

Set

Uniqe operation to obtain all data weight values for a certain period of time

Real-time system, anti-garbage system

Common friend, second-degree friend

Using uniqueness, you can count all the independent IP that visit the website

When recommending a friend, you can recommend it if it is greater than a certain threshold according to the intersection of tag.

Hashes

Store, read, and modify user attributes

Sorted Set

Ranking application, take TOP N operation

Applications that require precise expiration time (timestamp as Score)

An element with weight, such as a game's ranking of user scores

Processing of overdue items, sorted by time

This is the end of the content of "what is the design idea of redis flash sale activity". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report