Databases 6 min read

Auto‑Close Unpaid Orders with Redis Key Expiration Events in Spring Boot

This guide explains how to use Redis key expiration notifications in a Spring Boot application to automatically close unpaid orders by storing an order‑ID key with a TTL, configuring Redis to emit expiration events, and implementing a listener that updates order status when the key expires.

Programmer DD
Programmer DD
Programmer DD
Auto‑Close Unpaid Orders with Redis Key Expiration Events in Spring Boot

Business Scenario

We use an order function as an example: after an order is created, if it is not paid within a certain time it should be automatically closed. Using a scheduled task is inefficient because order creation times differ, so we propose listening to Redis key expiration events to close orders automatically.

Implementation Idea

When creating an order, store a KV pair in Redis where the key is the order number (so we can locate the order in the database) and the value can be anything. Set the key's TTL to 30 minutes. When the key expires, Redis sends a notification, allowing us to close the unpaid order.

We listen to Redis's expiration event queue; when a key expires, Redis publishes a message containing the key ( K) but not the value ( V). Using the key we locate the order and, if it is still unpaid, update its status to closed or cancelled.

Enable Redis Key Expiration Notifications

Modify redis.conf to set notify-keyspace-events to include the appropriate flags. Add Ex to enable key‑event expiration notifications. The flags meaning: K – keyspace events, E – keyevent events, g – generic commands, $ – string commands, l – list commands, s – set commands, h – hash commands, z – sorted‑set commands, x – expired events, e – evicted events, A – all events.

Add Dependency

Add the Spring Boot starter for Redis in pom.xml:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

Configuration

Create a RedisListenerConfig class to define a RedisMessageListenerContainer bean:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;

@Configuration
public class RedisListenerConfig {
    @Bean
    RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        return container;
    }
}

Implement a listener extending KeyExpirationEventMessageListener to handle expired keys:

import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;

@Component
public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener {
    public RedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer) {
        super(listenerContainer);
    }

    @Override
    public void onMessage(Message message, byte[] pattern) {
        // Get the expired key and process order cancellation
        String expiredKey = message.toString();
        System.out.println(expiredKey);
    }
}
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaredisSpring BootOrder ManagementKey Expiration
Programmer DD
Written by

Programmer DD

A tinkering programmer and author of "Spring Cloud Microservices in Action"

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.