The most useful retry case to assert would be a crash after the database commits but before the client gets its response. The retry should return the original result for that idempotency key, with exactly one durable payment record.
I Killed Redis With SIGKILL to Test Payment Idempotency — Here Is What Broke
12 Comments
@[WAQAS AHMAD] Correct! And it is one of the main motives of our "Dual-Layer Idempotency" concept. If a retry comes in right after Postgres commits and before Redis gets updated, it falls straight through to Postgres(graceful degradation as mentioned in the diagram), hits the IntegrityError on idempotency_key, rolls back cleanly, and returns the original PaymentIntent (201)—leaving strictly one row in the ledger.
Please log in to add a comment.
The dead-socket and nested-return bugs are exactly what a real kill finds and a mock never would. I have a question on the everysec row, since a container SIGKILL leaves the host page cache intact and I'd expect the ~1 second loss only on real power or VM loss. Did you try killing the whole VM to watch a retry land on the UNIQUE path?
@[nik-13] Fair and sharp point on OS page-cache semantics. I didn't kill a full VM in this run, so with docker kill -s SIGKILL the host kernel stayed alive and flushed everysec's dirty buffers cleanly. To force a retry onto the Postgres UNIQUE fallback path without a hard VM reset, I wiped the Redis key mid-flight right after Postgres committed. A proper hard VM reset (virsh destroy) to trigger that ~1s page-cache loss organically is definitely next on my list.
Please log in to add a comment.
@[inevolin] Thanks and Great point. I set ex=300 (5 minutes) on the PROCESSING lock so that retries see a 409 for up to that window. I didn't query Postgres during an active PROCESSING hit in this version. But yes, tightening that TTL and checking Postgres on conflict will be a much cleaner refinement. so if the row is already committed, we can backfill Redis and return 201 immediately instead of making the client wait out the TTL. Definitely adding that next.
Please log in to add a comment.
Solid teardown, and the Redis-then-Postgres fallback is the right shape. One thing worth adding from the payments side: an idempotency key is only safe if it's bound to the request it first committed. The key is client-supplied, so if you store and compare it alone, a replay that reuses the key with a different amount or payee gets served the cached 201 for the original charge, and a client bug that recycles a key across distinct payments silently suppresses the second one. Storing a hash of the canonical fields (amount, currency, payee) next to the key and returning 422 on a key-reuse-with-different-body closes that gap — the UNIQUE constraint stops a double-commit, but only the request fingerprint stops a wrong-commit.
@[DunimeTech] That is an eye-opener—I hadn't looked at it from that angle, to be honest. We really can't just be at the mercy of a client-supplied idempotency key. Storing a canonical SHA-256 hash of (amount, currency, payee) alongside the key and rejecting body mismatches with 422 Unprocessable Content should close that gap cleanly. Definitely adding that validation layer in the next pass!
@[bhargavdas99] Glad it's useful, Bhargav. Two things to nail down when you add it: write the fingerprint hash in the same insert that reserves the key, so a concurrent duplicate can't win the key row while the hash write lags behind; and on a legitimate retry (same key, same body) replay the stored original response rather than recomputing it, so a downstream change can never make the "same" request return something different. That keeps the key a true idempotency anchor and not just a lock.
Please log in to add a comment.
This is a great example of why payment reliability needs more than a simple Redis check. I especially liked the dual layer approach with PostgreSQL as the source of truth. One question: how would you handle a worker crash after the database commit but before Redis gets updated? That edge case seems important for preventing inconsistent retry behavior.
@[SuMiTa] Thanks for the feedback! And yes it is one of the main motives of our "Dual-Layer Idempotency" concept. If a retry comes in right after Postgres commits and before Redis gets updated, it falls straight through to Postgres (graceful degradation as mentioned in the diagram), hits the IntegrityError on idempotency_key, rolls back cleanly, and returns the original PaymentIntent (201)—leaving strictly one row in the ledger.
Please log in to add a comment.
Please log in to comment on this post.
More Posts
- © 2026 Coder Legion
- Feedback / Bug
- Privacy
- About Us
- Contacts
- You Tube
- Tiktok
- Premium Subscription
- Terms of Service
- Early Builders
More From bhargavdas99
Related Jobs
- Hardware & Software Test EngineerOn Time Hire · Full time · Egypt
- Frontend Developer with Payments & Foreign ExchangeKaizen Technologies · Full time · Tampa, FL
- Work From Home - Paid Online Casino TesterLittle Wheel · Full time · Egypt
Commenters (This Week)
Contribute meaningful comments to climb the leaderboard and earn badges!