java
33 lines · 7 steps
Idempotent webhooks with Redis in Spring
A Spring service uses an atomic Redis SETNX guard to process each webhook delivery exactly once.
Explained by
highlit
1@Service
2public class WebhookDedupService {
3
4 private static final Logger log = LoggerFactory.getLogger(WebhookDedupService.class);
5 private static final Duration GUARD_TTL = Duration.ofSeconds(30);
6
7 private final StringRedisTemplate redis;
8 private final ApplicationEventPublisher events;
9
10 public WebhookDedupService(StringRedisTemplate redis, ApplicationEventPublisher events) {
11 this.redis = redis;
12 this.events = events;
13 }
14
15 public void handle(String deliveryId, StripeEvent event) {
16 String key = "webhook:seen:" + deliveryId;
17 Boolean firstDelivery = redis.opsForValue()
18 .setIfAbsent(key, Instant.now().toString(), GUARD_TTL);
19
20 if (!Boolean.TRUE.equals(firstDelivery)) {
21 log.info("Ignoring duplicate webhook delivery {} for event {}", deliveryId, event.getId());
22 return;
23 }
24
25 try {
26 events.publishEvent(new WebhookReceivedEvent(event));
27 log.info("Accepted webhook delivery {} for event {}", deliveryId, event.getId());
28 } catch (RuntimeException ex) {
29 redis.delete(key);
30 throw ex;
31 }
32 }
33}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1An atomic set-if-absent with a TTL turns Redis into a lightweight, self-expiring dedup guard.
- 2Deleting the guard key on failure lets a retried delivery be reprocessed instead of silently dropped.
- 3Publishing an application event decouples receiving a webhook from the work that handles it.
Related explainers
java
@Component @Converter public class EncryptedStringConverter implements AttributeConverter<String, String> {
Transparent column encryption in Spring & JPA
encryption
aes-gcm
jpa-converter
Advanced
10 steps
java
package com.acme.billing.config; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.ConfigurationProperties;
Feature-flagged beans with Spring @ConditionalOnProperty
feature-flags
conditional-beans
strategy-pattern
Intermediate
5 steps
java
public static Map<String, String> parseCookieHeader(String header) { Map<String, String> cookies = new LinkedHashMap<>(); if (header == null || header.isBlank()) { return cookies;
Parsing an HTTP Cookie header in Java
string-parsing
http
url-decoding
Intermediate
6 steps
go
package logging import ( "context"
Deduplicating log attributes in Go's slog
decorator-pattern
structured-logging
immutability
Intermediate
8 steps
java
public class TimedSocketReader { private static final int READ_TIMEOUT_MS = 5_000; private static final int CONNECT_TIMEOUT_MS = 3_000;
Reading a socket with connect and read timeouts
sockets
timeouts
io
Intermediate
8 steps
java
public final class EncodingDetector { public enum Encoding { UTF_8, UTF_16LE, UTF_16BE, UTF_32LE, UTF_32BE, ASCII, UNKNOWN
Detecting text encoding from raw bytes in Java
byte-manipulation
encoding-detection
bitwise-operations
Intermediate
8 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/idempotent-webhooks-with-redis-in-spring-explained-java-dfb6/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.