Идемпотентность платёжных операций
Идемпотентность гарантирует: повторный запрос с тем же ключом не создаёт дубликата операции.
Почему важна
Network failures: клиент не знает — прошёл ли запрос. Retry logic: без idempotency = duplicate charges. Real-world impact: 1000 pieces of same transaction в bad cases. Player trust: double-charge = massive fraud complaint. Financial reconciliation nightmare.
Реализация
Client generates idempotency_key (UUID) для каждого logical operation. Server stores mapping key → result. Repeat request с тем же key → return original result. TTL: 24 часа обычно. Storage: Redis с persistence или DB.
Обработка edge cases
Race condition: two concurrent requests с same key → distributed lock или optimistic locking. Different payload с same key → return error (409 Conflict) или original result. In-flight status: return 202 Accepted для polling. Timeout requests: preserve state, eventual consistency.
API design
Header: Idempotency-Key: