search
HomeDatabaseRedisHow to implement Redis distributed cache and flash sales

1. Problems with single-point Redis

1. Data loss problem

Redis data persistence.

2. Concurrency issue

Our master-slave cluster realizes the separation of reading and writing.

3. Failure recovery issues

Use Redis Sentinel to implement health detection and automatic recovery.

4. Storage capacity issue

Build a sharded cluster and use the slot mechanism to achieve dynamic expansion.

2. RDB

RDB's full name is Redis Database Backup file (Redis data backup file), also called Redis data snapshot. To put it simply, all the data in the memory is recorded to the disk. When the Redis instance fails and restarts, the snapshot file is read from the disk and the data is restored.
The snapshot file is called an RDB file and is saved in the current running directory by default.

There is a mechanism to trigger RDB inside Redis, which can be found in the redis.conf file. The format is as follows:

How to implement Redis distributed cache and flash sales

When the bgsave command is executed, fork will be passed. The system call creates a child process that shares memory data with the main process. After completing the fork, read the memory data and write it to the RDB file.

Fork uses copy-on-write technology:

  • When the main process performs a read operation, it accesses the shared memory;

  • When the main process performs a write operation, it will copy a copy of the data and perform the write operation;

The basic process of RDB mode bgsave?

  1. Fork the main process to get a child process and share the memory space;

  2. The child process reads the memory data and writes a new RDB file;

  3. Replace the old RDB file with the new RDB file;

How to implement Redis distributed cache and flash sales

When will RDB be executed? What does save 60 1000 mean?

  • The default is when the service is stopped;

  • means that RDB will be triggered if at least 1000 modifications are performed within 60 seconds;

Disadvantages of RDB?

  • The RDB execution interval is long, and there is a risk of data loss between two RDB writes;

  • fork sub-process, compression, write It is time-consuming to export RDB files;

The frequency of AOF command recording can also be configured through the redis.conf file:

3. AOF

AOF stands for Append Only File. Every write command processed by Redis will be recorded in the AOF file, which can be regarded as a command log file.

AOF is turned off by default. You need to modify the redis.conf configuration file to enable AOF:

How to implement Redis distributed cache and flash sales

The frequency of AOF command recording can also be passed through redis.conf File to match:

How to implement Redis distributed cache and flash sales

Configuration items Flush timing Advantages Disadvantages
Always Synchronous disk flush High reliability, almost no data loss Great impact on performance
everysec Flash disk per second Moderate performance Lost data for up to one minute
no Operating system control Best performance Poor reliability, may lose a lot of data

Because it is a recording command, the AOF file will be much larger than the RDB file. Although AOF will record multiple write operations to the same key, only the last write operation among them is meaningful. You can use the bgrewriteaof command to complete the AOF file rewriting function with the minimum number of commands.

set id 1
set name nezha
set id 2

bgrewriteaof

mset name nezha id 2

Redis will also automatically rewrite the AOF file when the threshold is triggered. The threshold can also be configured in redis.conf:

# If the AOF file grows by more than the last file, the rewrite will be triggered. auto-aof-rewrite-percentage 100# What is the minimum size of the AOF file? Trigger rewrite auto-aof-rewrite-min-size 64mb

RDB and AOF each have their own advantages and disadvantages. If the data security requirements are high, the two are often combined in actual development to use.

##RDBAOFPersistence methodRegular snapshots of the entire memoryRecord every executed commandData integrityIncomplete, between two backups Will loseRelatively complete, depends on the brushing strategyFile sizeThere will be compression, the file size is smallrecords command, the file size is very largeDowntime recovery speedQuicklySlowData Recovery priorityLow, because data integrity is not lowHigh, because data integrity is higherSystem resource usageHigh, a lot of CPU and memory consumptionLow, mainly disk IO resources, but AOF rewriting will occupy a lot of CPU and memory resourcesUsage scenariosCan tolerate data loss for several minutes and pursue faster startup speedCommon with higher requirements for data security

4. Redis optimization flash sale process

1. Flash sale steps:

  1. Query coupons;

  2. Judge flash sale product inventory;

  3. Query order

  4. Verify one order per person;

  5. Reduce inventory;

  6. Create an order;

How to implement Redis distributed cache and flash sales

2. Redis optimization flash sale steps:

  1. Add flash sale coupons and save the coupon information to Redis;

  2. Based on Lua script, determine the flash sale product inventory, one person per order, determine whether the user's flash sale is successful;

  3. If the flash sale is successful, encapsulate the coupon id, user id, and product id into the blocking queue;

  4. Start the asynchronous task and continuously remove the items from the blocking queue Read information and implement asynchronous ordering function;

How to implement Redis distributed cache and flash sales

3. Lua script for flash sale

How to implement Redis distributed cache and flash sales

4. Call the flash kill lua script

public Result seckillVoucher(Long voucherId) {
     Long userId = UserHolder.getUser().getId();
     long orderId = redisIdWorker.nextId("order");
     // 1.执行lua脚本
     Long result = stringRedisTemplate.execute(
             SECKILL_SCRIPT,
             Collections.emptyList(),
             voucherId.toString(), userId.toString(), String.valueOf(orderId)
     );
     int r = result.intValue();
     // 2.判断结果是否为0
     if (r != 0) {
         // 2.1.不为0 ,代表没有购买资格
         return Result.fail(r == 1 ? "库存不足" : "不能重复下单");
     }
     // 3.返回订单id
     return Result.ok(orderId);
 }

5. Through the thread pool, operate the blocking queue

// 线程池
private static final ExecutorService SECKILL_ORDER_EXECUTOR = Executors.newSingleThreadExecutor();

/**
* 在类初始化完成后执行
*/
@PostConstruct
private void init() {
    SECKILL_ORDER_EXECUTOR.submit(new VoucherOrderHandler());
}

// 阻塞队列
private BlockingQueue<VoucherOrder> orderTasks = new ArrayBlockingQueue<>(1024 * 1024);
private class OrderHandler implements Runnable{

    @Override
    public void run() {
        while (true){
            try {
                doSomething();
            } catch (Exception e) {
                log.error("处理订单异常", e);
            }
        }
    }
}

5. Implement shared session login based on Redis

Login based on session

How to implement Redis distributed cache and flash sales

Implementing shared session login based on Redis

public class RefreshTokenInterceptor implements HandlerInterceptor {

    private StringRedisTemplate stringRedisTemplate;

    public RefreshTokenInterceptor(StringRedisTemplate stringRedisTemplate) {
        this.stringRedisTemplate = stringRedisTemplate;
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 1、获取请求头中的token
        String token = request.getHeader("authorization");
        if (StrUtil.isBlank(token)) {
            return true;
        }
        // 2、基于TOKEN获取redis中的用户
        String key  = LOGIN_USER_KEY + token;
        Map<Object, Object> userMap = stringRedisTemplate.opsForHash().entries(key);
        // 3、判断用户是否存在
        if (userMap.isEmpty()) {
            return true;
        }
        // 5、将查询到的hash数据转为UserDTO
        UserDTO userDTO = BeanUtil.fillBeanWithMap(userMap, new UserDTO(), false);
        // 6、存在,保存用户信息到 ThreadLocal
        UserHolder.saveUser(userDTO);
        // 7、刷新token有效期
        stringRedisTemplate.expire(key, LOGIN_USER_TTL, TimeUnit.MINUTES);
        // 8、放行
        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        // 移除用户
        UserHolder.removeUser();
    }
}

How to implement Redis distributed cache and flash sales

The above is the detailed content of How to implement Redis distributed cache and flash sales. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete
Redis vs databases: performance comparisonsRedis vs databases: performance comparisonsMay 14, 2025 am 12:11 AM

Redisoutperformstraditionaldatabasesinspeedforread/writeoperationsduetoitsin-memorynature,whiletraditionaldatabasesexcelincomplexqueriesanddataintegrity.1)Redisisidealforreal-timeanalyticsandcaching,offeringphenomenalperformance.2)Traditionaldatabase

When Should I Use Redis Instead of a Traditional Database?When Should I Use Redis Instead of a Traditional Database?May 13, 2025 pm 04:01 PM

UseRedisinsteadofatraditionaldatabasewhenyourapplicationrequiresspeedandreal-timedataprocessing,suchasforcaching,sessionmanagement,orreal-timeanalytics.Redisexcelsin:1)Caching,reducingloadonprimarydatabases;2)Sessionmanagement,simplifyingdatahandling

Redis: Beyond SQL - The NoSQL PerspectiveRedis: Beyond SQL - The NoSQL PerspectiveMay 08, 2025 am 12:25 AM

Redis goes beyond SQL databases because of its high performance and flexibility. 1) Redis achieves extremely fast read and write speed through memory storage. 2) It supports a variety of data structures, such as lists and collections, suitable for complex data processing. 3) Single-threaded model simplifies development, but high concurrency may become a bottleneck.

Redis: A Comparison to Traditional Database ServersRedis: A Comparison to Traditional Database ServersMay 07, 2025 am 12:09 AM

Redis is superior to traditional databases in high concurrency and low latency scenarios, but is not suitable for complex queries and transaction processing. 1.Redis uses memory storage, fast read and write speed, suitable for high concurrency and low latency requirements. 2. Traditional databases are based on disk, support complex queries and transaction processing, and have strong data consistency and persistence. 3. Redis is suitable as a supplement or substitute for traditional databases, but it needs to be selected according to specific business needs.

Redis: Introduction to a Powerful In-Memory Data StoreRedis: Introduction to a Powerful In-Memory Data StoreMay 06, 2025 am 12:08 AM

Redisisahigh-performancein-memorydatastructurestorethatexcelsinspeedandversatility.1)Itsupportsvariousdatastructureslikestrings,lists,andsets.2)Redisisanin-memorydatabasewithpersistenceoptions,ensuringfastperformanceanddatasafety.3)Itoffersatomicoper

Is Redis Primarily a Database?Is Redis Primarily a Database?May 05, 2025 am 12:07 AM

Redis is primarily a database, but it is more than just a database. 1. As a database, Redis supports persistence and is suitable for high-performance needs. 2. As a cache, Redis improves application response speed. 3. As a message broker, Redis supports publish-subscribe mode, suitable for real-time communication.

Redis: Database, Server, or Something Else?Redis: Database, Server, or Something Else?May 04, 2025 am 12:08 AM

Redisisamultifacetedtoolthatservesasadatabase,server,andmore.Itfunctionsasanin-memorydatastructurestore,supportsvariousdatastructures,andcanbeusedasacache,messagebroker,sessionstorage,andfordistributedlocking.

Redis: Unveiling Its Purpose and Key ApplicationsRedis: Unveiling Its Purpose and Key ApplicationsMay 03, 2025 am 12:11 AM

Redisisanopen-source,in-memorydatastructurestoreusedasadatabase,cache,andmessagebroker,excellinginspeedandversatility.Itiswidelyusedforcaching,real-timeanalytics,sessionmanagement,andleaderboardsduetoitssupportforvariousdatastructuresandfastdataacces

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.