The $99.99 Double Charge: How Stripe, Kafka, and Every Distributed System on Earth Fake 'Exactly-Once' Delivery
A phone loses signal for half a second, retries a payment, and a customer gets charged twice. The fix isn't a better network โ it's a mathematical surrender dressed up as an engineering pattern called idempotency.
The Elevator With No Signal
It's a Tuesday afternoon in 2016, and somewhere in a Stripe-powered checkout flow, a customer taps "Pay $99.99" inside an elevator. The request leaves their phone. The elevator doors close. The cellular radio drops to zero bars for exactly 400 milliseconds โ just long enough for the TCP connection to die before the response comes back.
The phone, dutifully following its retry logic, resends the request the moment signal returns.
Here's the problem: the first request did make it to Stripe's servers. It was processed. The card was charged. The response just never made it back to the phone. So when the retry lands, Stripe's API โ if it were naive โ has no way of knowing this is the same purchase. It looks like a brand new $99.99 charge. It processes it. Again.
Two charges. One purchase. A customer calling support, furious, holding a bank statement as evidence that the internet is broken.
It isn't broken. It's working exactly as distributed systems are mathematically guaranteed to work. And understanding why is the key to understanding one of the most quietly important ideas in all of software engineering: exactly-once delivery is a lie, and the entire industry has agreed to fake it.
The Proof That Ruined Everyone's Day
The uncomfortable truth goes back further than Stripe, further than Kafka, further than the modern internet. In 1975, three researchers โ E. A. Akkoyunlu, K. Ekanadham, and R. V. Huber โ formalized what's now known as the Two Generals Problem.
The setup: two armies, commanded by two generals, sit on opposite hills overlooking a city they want to attack. They will only win if they attack at exactly the same time. Their only way to coordinate is by sending messengers through enemy territory, where any messenger might be captured and the message lost.
General A sends a messenger: "Attack at dawn." But he can't be sure the messenger arrived. So General B sends a messenger back: "Confirmed, attacking at dawn." But now General B can't be sure his confirmation arrived. So General A needs to confirm the confirmation. And so on, forever.
There is no finite number of messages that creates perfect, guaranteed-certain agreement over a channel that can lose messages. It's not a hard engineering problem โ it's a proven mathematical impossibility. You cannot achieve exactly-once consensus over an unreliable network, full stop.
Every payment API, every message queue, every microservice calling another microservice over HTTP is running the Two Generals Problem in production, billions of times a day. The network can drop your request. It can drop the response after processing your request. It can time out when the operation actually succeeded. From the client's point of view, a timeout and a silent success are indistinguishable. So the client does the only rational thing: it retries.
Which means every system that claims "exactly-once delivery" is, underneath the marketing copy, actually running at-least-once delivery plus deduplication. The dedup is the trick. The dedup is everything.
Stripe's Fix: Make the Client Remember
Stripe published the canonical blueprint for solving this in a 2017 engineering blog post, and the pattern has since become a de facto industry standard: the idempotency key.
Here's how it works. Before making a request that mutates state โ like charging a card โ the client generates a unique identifier for that logical operation. Almost always a UUIDv4. Not per HTTP request, per intent. If the client retries, it reuses the exact same key.
POST /v1/charges
Idempotency-Key: a1b2c3d4-5e6f-7890-abcd-ef1234567890
{ "amount": 9999, "currency": "usd", "source": "tok_visa" }
On Stripe's side, when a request arrives with an idempotency key, the server first checks a fast dedup store โ Redis, in Stripe's architecture โ to see if that key has been seen before.
- Never seen it: process the charge normally, then store the key alongside the full response body and status code in Redis, with a TTL of 24 hours.
- Seen it, and it's still processing: return a 409-style "in progress" signal rather than double-processing. This is handled with a Redis
SETNX(set-if-not-exists) acting as a short-lived lock โ the server claims the key atomically the instant it starts working, so a retry arriving 50 milliseconds later doesn't race past the lock and double-charge the card. - Seen it, and it already finished: skip all business logic entirely and replay the exact same response that was returned the first time. The client thinks it just got charged. In reality, it's just reading a cached receipt.
One more detail that separates good engineers from engineers who ship security bugs: Stripe validates that the request body matches the original request that created the key. If you reuse an idempotency key but change the amount from $99.99 to $9,999.99, that's not a retry โ that's either a bug or an attack, and it gets rejected.
What happens when Redis itself is down? This is the part most tutorials skip. Stripe has to make a judgment call: hard-fail every request until dedup storage recovers (safe, but takes down payments entirely), or degrade gracefully and accept a small, bounded risk of duplicate charges rather than a total outage. In practice, payment infrastructure leans toward availability with monitoring and reconciliation jobs that catch the rare duplicate after the fact โ because a global payments outage is a worse headline than an occasional refund.
Kafka's Version of the Same Lie, With Better PR
In 2017, Kafka 0.11 shipped a feature with the boldest possible name: exactly-once semantics (EOS). It's one of the most misunderstood features in distributed systems, because the name oversells what's actually happening.
Here's what Kafka really does. Each producer is assigned a persistent Producer ID (PID), and every message it sends to a partition carries a monotonically increasing sequence number. If the producer's request times out and it retries, the broker sees the same PID and sequence number arrive twice and simply drops the duplicate at the partition level. That's idempotency, implemented inside the broker instead of an application-level Redis table โ same concept, different address.
For operations spanning multiple partitions โ read from topic A, write to topic B, commit the consumer offset โ Kafka adds a transaction coordinator that wraps all three actions into one atomic unit using a two-phase-commit-like protocol. Either all of it becomes visible to downstream consumers, or none of it does.
But here's the demystifying part nobody puts in the keynote slide: this guarantee only holds inside Kafka's walls. A Kafka-to-Kafka pipeline โ consume, transform, produce โ can genuinely get exactly-once-like behavior because every step is transactional within the same system. The moment your consumer writes to Postgres, calls a REST API, or sends an email, Kafka's transactional guarantee ends at the border. Kafka can promise the message was delivered to your consumer exactly once; it cannot promise your consumer's side effect โ the database write, the email send โ happened exactly once. That's a separate problem, and it needs a separate solution.
The Outbox Pattern: Solving the Border Problem
That separate solution is the transactional outbox pattern, used at scale by Shopify, GitHub, and most serious event-driven architectures.
The core insight: you can't atomically update a database and publish a message to a separate broker โ those are two different systems, and if the process crashes between the two, you either lose the event or duplicate the state change. So instead, you write the business state change and a row describing "an event needs to be published" into the same local database transaction:
BEGIN;
UPDATE orders SET status = 'paid' WHERE id = 42;
INSERT INTO outbox (event_type, payload) VALUES ('order.paid', '{...}');
COMMIT;
Both succeed or both roll back โ ordinary ACID guarantees, nothing exotic. A separate relay process (often using Kafka Connect's Debezium connector reading the database's write-ahead log) then polls or streams the outbox table and publishes each row to Kafka, marking it as sent. If the relay crashes mid-publish and resends, that's fine โ it's at-least-once again, dedupe'd downstream by consumers checking event IDs. The guarantee you actually get, precisely stated, is: the event is published if and only if the state change committed. That's a real, achievable guarantee โ just not the mythical exactly-once one.
Idempotency Was Already in the Spec
The HTTP protocol quietly solved half of this problem back in RFC 7231. PUT and DELETE are defined as idempotent by specification โ calling DELETE /users/42 five times should have the same effect as calling it once. GET is idempotent by nature. But POST โ the verb used to create a new charge, a new order, a new anything โ was never idempotent by design, because creating something twice usually means two somethings.
That gap is exactly what Stripe's Idempotency-Key header patches, and why it's since been echoed in Stripe-compatible APIs across the payments industry, in PayPal's API, in countless internal microservice contracts that nobody will ever publish a blog post about.
The Legacy
The Two Generals Problem was proven unsolvable in 1975, decades before most of today's engineers calling themselves "distributed systems architects" were born, and it still governs every retry button, every mobile payment, every message queue in production today. No amount of clever engineering repeals it โ you cannot achieve perfect certainty over an imperfect channel.
What Stripe, Kafka, and the outbox pattern all represent isn't a defeat of that theorem. It's a surrender to it, dressed up as an engineering pattern: stop trying to prevent duplicates from happening, and instead make duplicates harmless. At-least-once delivery, plus a server with a good memory, equals a system that merely looks like exactly-once from the outside.
So the next time an engineer tells you their system guarantees exactly-once delivery, ask them one question: where's the idempotency key, or where's the outbox table? If they can't point to either, they're not describing their architecture. They're describing a bug they haven't found yet.
Keep Reading
The Green Dashboard That Lied: How OpenTelemetry Killed the Logging Era and Taught Us to Ask Questions We Never Thought to Ask
Your dashboard says p99 latency is 800ms. Your customer says checkout took 45 seconds. Both are true. This is the story of how a messy 2019 merger of two rival projects quietly ended the vendor lock-in wars and rewired how the internet debugs itself.
5.26 Minutes a Year: The Brutal Math Behind 'Five Nines' โ And Why Almost Nobody Actually Hits It
In 1983, AT&T engineers built a phone switch that could fail for only 5 minutes a year โ and it worked, for decades. Here's the ruthless arithmetic of uptime, and why every extra 'nine' costs ten times more than the last.
The Herd That Broke Ticketmaster: How 14 Million Swifties Accidentally Explained Distributed Systems to Congress
On November 15, 2022, 14 million people clicked 'buy' on Ticketmaster at the same moment โ and the internet learned, in real time, what engineers call the Thundering Herd. Here's the decades-old problem behind the meltdown, and the five mitigation patterns that actually fix it.