DEV Community

Josip Musa
Josip Musa

Posted on

Idempotency4j - Java/Spring Boot idempotency library

The last couple of months, I ended up implementing idempotency in 2 different Spring Boot projects back to back.

As I was implementing it in the second project, I decided to look up any existing solutions/libraries for Java/Spring Boot, but I honestly couldn't find one that felt clean and flexible enough for what I needed (and what most people probably need).

So I decided to build my own and open source it.

I released it about a month ago:

Repository : https://github.com/josipmusa/idempotency4j

Maven spring boot starter : https://central.sonatype.com/artifact/io.github.josipmusa/idempotency-spring-boot-starter

The goal was to make idempotency implementations feel straightforward and easy, but also to not scope it only to spring boot or a certain storage implementation. The library has a core which can be used on any method with pluggable storage backends. It also has an integration with spring web (servlet-based for now) and a spring boot starter to simplify usage.

Usage example for a spring boot project:

@PostMapping("/payments")
@Idempotent
public ResponseEntity<Payment> createPayment(@RequestBody PaymentRequest request) {
 // Runs exactly once per unique Idempotency-Key value.
 // Subsequent identical requests get the stored response replayed.
 return ResponseEntity.ok(paymentService.charge(request));
}
Enter fullscreen mode Exit fullscreen mode

Right now it supports:

  • Spring MVC (Servlet-based apps)
  • JDBC storage (so it works out of the box with MySQL / PostgreSQL setups most people already have)
  • In-memory storage
  • duplicate request detection
  • replaying previous responses
  • concurrent request protection
  • request fingerprinting
  • configurable TTLs
  • pluggable storage backends

Curious whether others have run into this same problem and whether this library helps solve it for them.

Open to any feedback, suggestions, or reviews.

Top comments (0)