Have you ever wondered why, when you're transferring money via mobile banking, your signal suddenly drops, you hit "Send" again... but your balance isn't deducted twice?
That’s not luck. There’s a mechanism behind the scenes ensuring a single transaction is executed exactly once, even if the request is sent multiple times.
While building my Finance Ledger Core project, I realized that the ultimate challenge in financial systems isn't just about processing speed—it’s about how safely the system handles duplicate requests and retries. In the real world, a "retry" isn't an edge case; it’s an absolute certainty.
In this article, I'll dive into Idempotency: a fundamental principle in backend systems that keeps data consistent and secure, even when the same request arrives more than once. This concept may seem simple, but it is crucial for building fault-tolerant systems, especially when money is on the line.
What is Idempotency?
If you hear the word “Idempotency,” it might sound like heavy mathematical jargon. However, the concept is quite simple and appears in your daily life more often than you realize.
Simple Analogy: The Elevator Button vs. The Light Toggle
Imagine you’re waiting for an elevator. You press the "Up" button once. Being impatient, you press it ten more times. Does the elevator arrive ten times? No, right? It still arrives once at your floor. The elevator button is idempotent.
Contrast this with a toggle light switch. You press it once; the light turns on. You press it again; the light turns off. The result changes depending on how many times you press it. The light switch is NOT idempotent.
In the world of finance, we want our systems to behave like elevator buttons. Regardless of how many times the same request is received (due to a panicked user or an automated system retry), the final state must remain the same: The transaction is processed exactly once.
Technical Definition
Technically, Idempotency is a property of an operation or API where you can call the operation multiple times with the same input, yet the result remains identical to the first call without changing the server state more than once.
In the context of HTTP APIs:
- Safe Methods: Methods like
GETare naturally idempotent because they only read data without modifying the database. - Unsafe Methods: Methods like
POSTare NOT idempotent by default. Every time youPOST, the server assumes you want to create new data. Our job as engineers is to make thesePOSTrequests idempotent to handle financial transactions safely.
Idempotency is about guaranteeing consistency. The system might still return a "Success" response for retries, but behind the scenes, it won't deduct the user's balance a second time.
Why is it Important? (The “Why”)
You might think, "I can just disable the button on the frontend after it's clicked." Let me tell you: Never trust the client side. The real issue isn't just a "happy-click" user; it’s the infrastructure between your client and server.
There are two main reasons why idempotency is vital:
1. Network Issues
This is the classic scenario. In server-to-server communication, there are three possible outcomes:
- Success: Request arrives, gets processed, response is sent back. Safe.
- Fail: Request never arrives. Safe (just try again).
- Timeout / Unknown: The request arrives, the database is updated, but the connection drops right as the server tries to notify the client.
In scenario #3, the client thinks, "It failed," and triggers a retry. If your system isn't idempotent, you'll end up with duplicate data (and duplicate charges).
2. Load Distribution & Auto-Retries
If you use a message broker (like RabbitMQ), there’s usually an automatic retry feature. This means the system will re-send the same message to a different worker if the first one fails. Without idempotency, one transaction could be processed multiple times by different workers.
When to Implement Idempotency?
Not every API needs this extra layer of protection. Implementing idempotency everywhere makes your system bloated and slow. The principle is simple: Use it for operations with "Side Effects."
| HTTP Method | Idempotent? | Recommendation |
|---|---|---|
| GET | Yes | No extra implementation needed. |
| POST | No | Highly Recommended for transactions/payments. |
| PUT | Yes | Usually naturally idempotent (replaces data). |
| DELETE | Yes | Usually naturally idempotent (deleting non-existent data is fine). |
Pro Tip: If you're unsure, ask yourself: "If this request is called twice due to laggy internet, will someone complain because their money disappeared?" If the answer is YES, idempotency is mandatory.
Implementation Strategy: “The Idempotency Key”
How does the server know if a request is "old stock" in a "new box"? The answer is the Idempotency Key.
This is a unique identifier (usually a UUID) generated by the client and sent via an HTTP Header (e.g., X-Idempotency-Key).
Mechanism Flow (Sequence Diagram)

Simple Logic (Node.js/TypeScript Style)
The logic isn't just about checking for existence; you must ensure the process is Atomic. Using Redis is ideal because it's fast and supports Expiration (TTL).
async function processTransaction(idempotencyKey: string, payload: any) {
// 1. Check Cache/Redis if this key has already succeeded
const cachedResponse = await redis.get(`idemp:${idempotencyKey}`);
if (cachedResponse) {
console.log("Duplicate request detected. Returning cached response.");
return JSON.parse(cachedResponse);
}
// 2. Use a Distributed Lock (Optional but recommended for Race Conditions)
try {
// 3. Execute Ledger Business Logic within a Database Transaction
const result = await db.transaction(async (tx) => {
// Your balance mutation logic goes here
return await ledgerService.createEntry(tx, payload);
});
// 4. Store the result in Redis with a TTL (e.g., 24 hours)
// This ensures the key is cleaned up eventually.
await redis.set(
`idemp:${idempotencyKey}`,
JSON.stringify(result),
'EX', 86400
);
return result;
} catch (error) {
// Do NOT cache internal system errors (500), so the client can actually retry.
throw error;
}
}
Conclusion
Building a finance ledger core isn't just about writing SQL queries or performance optimization. It’s about Trust. Your users trust that when they click "Pay," the system will execute that command honestly—no more, no less.
Idempotency is a contract between the client and server that says: "I will handle all network uncertainties, so you don't have to worry about data duplication."
Our system is now smart enough to reject duplicates arriving seconds apart. But consider this: What if two requests with the same Idempotency Key hit your server at the exact same millisecond?
Both check Redis, both see the key doesn't exist yet, and both proceed to deduct the balance. Our current strategy will fail if we don't handle Concurrency. This is called a Race Condition, and in the next article, I’ll discuss how to handle it.
Share this article
Found this useful? Share it with your network.