Backend Development 6 min read

Implementing Redis Key Expiration Notifications in Spring Boot to Auto‑Close Orders

This article explains how to use Redis key‑expiration events in a Spring Boot application to automatically close unpaid orders by configuring Redis, adding the necessary dependencies, and implementing a listener that reacts to expired keys.

Java Architect Essentials
Java Architect Essentials
Java Architect Essentials
Implementing Redis Key Expiration Notifications in Spring Boot to Auto‑Close Orders

Business scenario : When an order is created but not paid within a certain time, it should be automatically closed; polling tasks are inefficient because each order has a different creation time.

Implementation idea : Store a KV pair in Redis where the key is the order ID and the value can be arbitrary, set its TTL to 30 minutes, and listen for the key‑expiration event to trigger order cancellation.

Enable Redis key expiration notifications : Edit redis.conf and set notify-keyspace-events Ex (or append Ex to existing value). The letters represent event types, e.g., K for keyspace, E for keyevent, g for generic commands, x for expiration events, etc.

Dependency : Add the Redis starter to the Maven project.

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

Configuration : Define a RedisListenerConfig class that creates 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;
    }
}

Listener : Implement RedisKeyExpirationListener extending KeyExpirationEventMessageListener to receive expired‑key messages, extract the key, and perform order‑cancellation logic.

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;

/**
 * Listens to all DB expiration events "__keyevent@*__:expired"
 */
@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 handle order cancellation
        String expiredKey = message.toString();
        System.out.println(expiredKey);
        // TODO: locate order by key and update its status to closed
    }
}

With this setup, when the Redis key representing an unpaid order expires after 30 minutes, the listener receives the event, retrieves the order ID, and updates the order status to closed, eliminating the need for frequent polling.

BackendJavaRedisSpring BootOrder ManagementKey Expiration
Java Architect Essentials
Written by

Java Architect Essentials

Committed to sharing quality articles and tutorials to help Java programmers progress from junior to mid-level to senior architect. We curate high-quality learning resources, interview questions, videos, and projects from across the internet to help you systematically improve your Java architecture skills. Follow and reply '1024' to get Java programming resources. Learn together, grow together.

0 followers
Reader feedback

How this landed with the community

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