NewWork mode: your Claude Code or Codex on work calls, answering from your project's codeSkip to content
Middle+0 views

Why do we need Redis to store the idempotency key? Why can’t we store it in a local cache?

Practice

AI answer

A local cache belongs to a single process or application instance: another instance cannot see a key that was written. With multiple replicas, a request with the same key may reach a different replica, and the entry disappears when the process restarts. Therefore, a local cache does not provide a shared, sufficiently durable idempotency check.

Redis provides a fast store shared across instances, with an atomic key-creation operation such as SET key value NX EX ttl. If the key already exists, the repeated request is recognized; the TTL limits how long the key is kept. For correctness, choose a TTL that covers the retry window, handle Redis unavailability, and plan for races and failures. If a strict guarantee against duplicate execution is needed even when Redis data is lost or a failure occurs between writing the result and the key, writing to Redis alone may not be sufficient: use a transactional store or a pattern such as the transactional outbox.