Explain idempotency in APIs.
Skip to solutionKEEP THE
mediumSystem Design
What is idempotency and why does it matter?
1.1k views
01
Understand the problem
idempotency
02
Attempt it yourself
Sketch your approach before reading the solution — that's what interviews test.
Nudge consolestandby
Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.
03
Study the solution
What Is Idempotency, and Why Does It Matter?
Target Audience: Junior & Senior Software Engineers preparing for System Design Interviews — no prior system design knowledge assumed. Difficulty: Easy to Medium
How to read this doc: Every concept is explained in plain language first. Right after, you'll s
Solution ready — 2 min read
Classified // press E to declassify
04
Read the code
Idempotency key: safe payment retry
Run Playgroundclass PaymentProcessor {
constructor() {
this.seenKeys = new Map();
this.nextId = 1;
this.chargesProcessed = 0;
}
charge(idempotencyKey, amount) {
if (this.seenKeys.has(idempotencyKey)) {
return this.seenKeys.get(idempotencyKey);
}
const txnId = `TXN${this.nextId}`;
this.nextId += 1;
this.chargesProcessed += 1;
this.seenKeys.set(idempotencyKey, txnId);
return txnId;
}
}
const p = new PaymentProcessor();
console.log('key-1 first call:', p.charge('key-1', 100));
console.log('key-1 retry:', p.charge('key-1', 100));
console.log('key-2 first call:', p.charge('key-2', 50));
console.log('total charges actually processed:', p.chargesProcessed);05
Join the discussion
Discussion (0)
Sign in to join the discussion.
No responses yet. Be the first to share what you think.
Transmission complete // awaiting log
KEEP THE
STREAK ALIVE.
Dossier 9 of 99 decoded in the System Design track. One more won't hurt.