Why do we need Redis to store the idempotency key? Why can’t we store it in a local cache?
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.
Also asked at Коммит (Аутстафф на проект "Ростелекома")
- How to protect the system from creating multiple payments if the user presses the pay button many times?
- How is the N+1 query problem solved in ORM?
- What types of indexes are available in PostgreSQL?
- There are several services (2-3), and there is an end-to-end business scenario passing through all services. If an operation on one of the services in the middle or at the end fails, the system ends up in an inconsistent state. What methods can be used to achieve inter-service transactionality (either the entire scenario is applied, or none of it is), if each service has its own database?
- What databases have you worked with the most?
Similar questions
- Where do we store the idempotency key, and how do we process it?
- What does it mean to use idempotency, and at which level (client or backend) is the idempotency key created?
- Can we ensure data durability in Redis if the service crashes, so that we don't lose shopping carts?
- How was Redis set up in your project (one instance or several)?
- What cache read and write patterns are there (Cache-Aside, Write-Through, etc.)?