In this lesson, you’ll learn how to use idempotency keys to ensure that retrying a failed network request doesn't accidentally trigger the same database action twice.
The Problem of Partial Failure
In a distributed system, a client might send a request to a database service to "Deduct 50, leaving the user with a duplicate state change.
The Idempotency Key Solution
An idempotency key is a unique identifier (usually a UUID) generated by the client and sent alongside the request. The database uses this key to check if it has already processed that specific operation before executing it again.
Think of it as a "processed-request log." Before performing any state change, the database performs a quick check:
-- 1. Check if the key exists in an 'idempotency_log' table
-- 2. If it exists, return the previously cached response
-- 3. If it does not exist, perform the business logic and insert the key
INSERT INTO idempotency_log (request_id, response_data)
VALUES ('unique-uuid-123', '{"status": "success", "balance": 950}');
-- If this INSERT fails due to a Primary Key constraint violation,
-- we know the request is a duplicate and we skip the state change.
By enforcing a UNIQUE constraint on the request_id column, the database ensures that any subsequent attempts with the same ID will result in a constraint violation rather than a second debit. The database then simply returns the result of the original, successful operation from its log.
This pattern is essential when dealing with distributed systems where network instability is a fact of life. It turns "exactly-once" delivery—which is physically impossible over unreliable networks—into "effectively-once" outcomes. By tagging requests with a unique identifier, you allow the system to be resilient to retries, ensuring that your state remains consistent even when communication is interrupted.
Key Takeaway: Idempotency keys prevent duplicate state changes by using unique request identifiers to gate database operations, ensuring that retried requests result in no additional side effects.