Ravindra BagaleCourses & study guides मराठी

Chapter 93: AWS Lambda and Serverless for DevOps

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.

Duplicates and idempotence
Duplicates and idempotence

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

  1. Choose a stable operation key such as a fictional order ID.

  2. Claim it atomically in durable storage before side effects.

  3. Save a result/status and define expiry and crash recovery.

  4. Never use a local Python set as production duplicate protection.

Example 1: Pure repeatable calculation

  1. 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))
  1. Read the result.

Output (tested locally; not on AWS)

True
  1. 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

  1. Read the input and the code.
ABSENT -> IN_PROGRESS -> COMPLETED(result)
Repeat COMPLETED -> return saved result
Expired IN_PROGRESS -> follow a tested recovery policy
  1. Read the result.

Output (illustrative; not executed on AWS)

Two workers racing for the same key: only one conditional claim succeeds.
  1. 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.

Did you understand? Let us try it once more.