Course contentsShow
← System Design
Lesson 1004 of 1,91926. Idempotency and RetriesFree lesson

Server-Side State for Idempotency

Understand how servers must store request history or results to detect and handle duplicate requests.

Server-Side State for Idempotency

What You'll Learn

You'll learn how servers must track request information to recognize and handle duplicate requests correctly.

The Core Problem

Remember idempotency keys from the last lesson? The client sends them, but the server must do the real work: storing enough information to detect when a request is a duplicate and return the appropriate response without repeating side effects.

Think of it like a receipt book at a store. When a customer wants to return an item, the cashier checks the receipt book to see if that transaction already happened. Without the book, they can't tell if someone is trying to return the same item twice.

What State to Store

The server needs to persist:

  1. The idempotency key itself — your unique identifier
  2. Request metadata — enough details to verify it's the same request (method, endpoint, user ID)
  3. Operation status — is it processing, complete, or failed?
  4. The result — the original response to return for duplicates

Storage Duration

This state must survive:

  • Server restarts — store in a database or distributed cache, not just memory
  • Long enough for realistic retries — typically 24 hours to several days
  • Cleanup after expiration — old keys can be purged to save space

Detection Flow

When a request arrives with an idempotency key:

  1. Check if the key exists in storage
  2. If new: process normally and store the result
  3. If exists and complete: return the stored result
  4. If exists and processing: wait or return "still processing" status

Key Takeaway: Idempotency requires server-side storage of request keys, status, and results so that duplicate requests can be detected and handled without repeating operations or creating inconsistent state.