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

Idempotent Response Caching

Cache complete responses alongside idempotency keys to return identical results without re-executing business logic.

Idempotent Response Caching

What You'll Learn

You'll learn how to cache complete responses alongside idempotency keys to return identical results without re-executing business logic.

The Problem: Wasted Work on Retries

When a client retries a request with the same idempotency key, you've already done the work once. Simply checking "this key exists" prevents duplicate side effects, but you still need to construct a response. What if that response generation is expensive—database queries, calculations, external API calls?

The Solution: Cache the Entire Response

Instead of just tracking that a request completed, store the complete HTTP response (status code, headers, body) alongside the idempotency key. When a retry arrives:

  1. Look up the idempotency key
  2. If found and completed, return the cached response immediately
  3. No database queries, no business logic re-execution—just an instant replay

How It Works

Think of it like a video recording of your first answer. When someone asks the same question again (same idempotency key), you don't re-think the answer—you just replay the recording.

Example flow:

  • First request with key abc-123: Process payment, store result in idempotency table with the full JSON response {"status": "success", "charge_id": "ch_xyz"}
  • Retry with key abc-123: Look up key, find cached response, return {"status": "success", "charge_id": "ch_xyz"} instantly

Storage Considerations

Store the response in the same record as your idempotency key metadata. Include the HTTP status code—a failed operation (400, 409) should also be cached to prevent retry storms against validation errors.

Key Takeaway: Idempotent response caching means storing the complete response with the idempotency key, enabling instant, identical replies to retries without re-executing any business logic.