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

Example Analysis of instantaneous High concurrency second kill in Redis

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article is to share with you the content of the example analysis of Redis instantaneous high concurrency second kill. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

1.Redis

Rich data structures (Data Structures)

String (String)

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 the string.

List (List)

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.

Collection (Set)

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.

Hash (Hashes)

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.

Ordered set (Sorted Sets)

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.

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 links through the interface of other slave, in addition to accepting links to slaves under the same master, it can also accept links to other slaves in the same structure diagram. Redis replication is non-blocking in the master segment. This means that master can also accept query replication when performing synchronization on the same or more slave. Suppose you configure redis in redis.conf, when slave is performing a new synchronization, it can still provide queries with old data information. Otherwise, you can configure that when redis slaves loses contact with master, slave will send a client error in order to have multiple slaves to do read-only query. Replication can be repeated 2 or even more times, and it is scalable (for example: slaves conversations and repeated sorting operations, it is relatively easy to have multiple data redundancy). He can use replication to avoid saving data on the master side, as long as the redis.conf on the masterside is configured to avoid saving (all save operations), and then through the slave link To save real-time LRU expiration processing (Eviction) EVAL and EVALSHA commands on Redis 2.6.0, using the built-in Lua interpreter, you can evaluate Lua scripts Redis uses a single Lua interpreter to run all scripts, and Redis also ensures that the script will be executed in an atomic way: when a script is running, no other scripts or Redis commands will be 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).

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

Transactions MULTI, EXEC, DISCARD, and WATCH are the basis of a Redis transaction. The transaction is a separate isolated operation: all commands in the transaction are serialized and executed sequentially. During the execution of a transaction, the commands in the transaction will not be interrupted by command requests sent by other clients. Either all or none of the commands in the transaction are executed. The EXEC command is responsible for triggering and executing all the commands in the transaction. Redis's Transactions provides not strict ACID transaction Transactions or basic command package execution function: a series of commands can be guaranteed to be executed in sequence. There will be other client commands plugged in to execute Redis also provides a Watch function, you can Watch a key, and then execute Transactions, in the process, if the value of the Watched has been modified, then the Transactions will find and refuse to execute the data persistence RDB.

Features:

RDB persistence allows snapshot storage of your data at specified intervals.

Advantages:

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, the RDB approach is faster when recovering large datasets.

Disadvantages:

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

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

AOF

Characteristics

AOF persistence method records every operation written to the server

When redis restarts, priority is given to loading the AOF file to recover the original data, because in general, the dataset saved by the AOF file is more complete than the dataset saved by the RDB file.

Advantages

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

Shortcoming

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.

Choice

Use both persistence features at the same time.

Distributed Redis Cluster (Redis 3 version) Keepalived

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.

Twemproxy

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, CPU-intensive, while redis node clusters with multiple CPU resources are redundant and can be deployed on redis node clusters without additional equipment.

High availability (HA) Redis Sentinel (redis's own cluster management tool)

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.

Single Mmurs structure

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.

Double Mmurs structure

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.

Comparison of single Mmurs structure and double Mmurs structure

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.

Publish / subscribe (Pub/Sub) monitoring: Redis-Monitor history redis runs queries: real-time monitoring curves such as CPU, memory, hit rate, number of requests, master-slave switching, etc.

two。 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, the real-time system of obtaining all data weight values in a certain period of time, and the uniqueness of common friends and second-degree friends in anti-spam systems. When all independent IP friends who visit the site are recommended, Hashes can be recommended if it is greater than a certain threshold according to the intersection of tag.

Store, read, and modify user attributes

For Sorted Set ranking applications, the TOP N operation needs to accurately set the expiration time of the application (timestamp as Score) with weighted elements, such as the processing of expired items in a game's ranking of user scores, sorted by time.

3.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, synchronize the Redis data to the database, and the second kill is officially over.

Thank you for reading! This is the end of this article on "example analysis of Redis instantaneous high concurrency second kill". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!

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

Database

Wechat

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

12
Report