Chapter 93: AWS Lambda and Serverless for DevOps
93.6 Duplicates and idempotence
Story (fictional teaching scenario). At a fictional Nagpur course shop, a receipt event arrives twice. Alex from Wellington wants both deliveries to produce one receipt, not charge a learner twice.
Why
Delivery can repeat. A handler must tolerate the same business request more than once.
What
- Duplicate is another delivery of the same business work.
- Idempotence means repeat processing does not repeat the business side effect.
- Idempotency key identifies the business operation, not merely one invocation.
- Atomic conditional write changes storage only if a condition holds, avoiding races between two workers.
How
-
Choose a stable operation key such as a fictional order ID.
-
Claim it atomically in durable storage before side effects.
-
Save a result/status and define expiry and crash recovery.
-
Never use a local Python set as production duplicate protection.
Example 1: Pure repeatable calculation
- Read the input and the code.
from price import lambda_handler
e = {'qty':3, 'unit_paise':4000}
print(lambda_handler(e,None) == lambda_handler(e,None))
- Read the result.
Output (tested locally; not on AWS)
True
- What each part means: The local calculation has no side effects, so repeating it gives the same result. This test does not prove exactly-once charging or durable duplicate suppression.
Example 2: A durable claim state machine
- Read the input and the code.
ABSENT -> IN_PROGRESS -> COMPLETED(result)
Repeat COMPLETED -> return saved result
Expired IN_PROGRESS -> follow a tested recovery policy
- Read the result.
Output (illustrative; not executed on AWS)
Two workers racing for the same key: only one conditional claim succeeds.
- What each part means: ABSENT means no record yet. IN_PROGRESS means a worker owns the operation. COMPLETED stores the reusable result. A process can crash after a side effect but before completion; production design must reconcile that gap, often using an idempotent downstream API. This is a plan, not executed database code.
Ravindra Bagale's Tip
Retry-safe calculation and exactly-once payment are different problems.
Calculation पुन्हा करणं safe असणं आणि payment एकदाच होणं या वेगळ्या गोष्टी आहेत.
Calculation दोबारा safe होना और payment सिर्फ एक बार होना अलग बातें हैं.
Did you understand? Let us try it once more.