r/PinoyProgrammer • u/Boring-Breadfruit251 • 2h ago
Show Case Stand for demonstrating and exploring Idempotency Keys in Spring Boot
Ever wondered what happens when a rider taps "Book" on a train with weak signal? The request reaches the server, the ride is booked and the card is charged, but the answer is lost in a tunnel. The app does what any app does: it sends the request again. Without protection, the rider pays twice for one trip.
This is the retry problem, and it's one of those topics that separates "I know Spring Boot" from "I understand how payments actually work."
I built a small hands-on stand where you can break things on purpose and watch the pattern save the day:
Idempotency-Key: every booking carries a key. The first request books and charges. Every repeat under the same key gets the first answer back, status and body as they were, with no second ride and no second charge. The same key with a different body is refused with 422. A repeat that arrives while the first is still running gets 409.
Safe across the Stripe boundary: a charge can't be rolled back, so it runs outside the DB transaction. Stripe receives the same key, so neither side does its work twice. The receipt job goes through a transactional outbox and an inbox, so a redelivered Kafka message never sends a second receipt.
What you can play with:
- Two Spring Boot services (ride + receipt) built on idempify and oncebox
- PostgreSQL as the key store, Redis as the response cache, Kafka for receipts
- A simulator that books rides like a phone on a bad network: it hangs up before the answer and retries under the same key
- Failures told apart: a declined card is replayed as final, a Stripe outage frees the key for a retry
- RFC 9457 problem details, Prometheus metrics, Grafana dashboards
- A quick start with Docker
Run the simulator for a minute, then count: rides, charges and receipts match, however many attempts the log shows. Send the same request twice and watch the second one come back identical, with nothing new booked.
If you're learning Java and want to go beyond CRUD tutorials, this is a good next step. Feedback is very welcome!