Where do we store the idempotency key, and how do we process it?
AI answer
The client usually sends the idempotency key in a request header, such as Idempotency-Key, and the server stores it in durable storage—often a database or Redis with a TTL. The record associates the key with a user or scope, a hash of the request parameters, the processing status, and the result.
For the first request, the server atomically reserves the key, performs the operation, and saves the result. A retry with the same key and the same parameters receives the saved result rather than executing the operation again. If the key is reused with different parameters, the server should return an error. Concurrent requests must be synchronized using a unique constraint or an atomic operation; for processing that has not completed, the server can return an “in progress” status or safely retry after a timeout. Choose the TTL based on the retry window, and for financial operations, the record is often retained for longer.
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
- What does it mean to use idempotency, and at which level (client or backend) is the idempotency key created?
- Why do we need Redis to store the idempotency key? Why can’t we store it in a local cache?
- What is idempotency in HTTP, and which methods are idempotent?
- What pattern should be used for distributed, long-running transactions during checkout and payment to ensure nothing is lost?
- What if an external system doesn't respond while a message is being processed?