# Idempotency Keys in Payment Gateways
Handling duplicate requests in a distributed environment using Redis.

##Problem Statement
In distributed payment systems, network timeouts and retries can lead to duplicate charges. Without a robust idempotency mechanism, customers might be charged multiple times for the same transaction.
##Solution
We implemented idempotency keys using Redis with TTL-based expiration. Each request carries a unique key that is checked before processing. If the key exists, we return the cached response. If not, we process the payment and store the result.
##Implementation
The key insight is using Redis SETNX with a reasonable TTL (24 hours) to handle the window of uncertainty. We also store the response body to ensure consistent behavior on retries.
##Results
- Zero duplicate charges in the last 6 months
- Average lookup time: <5ms
- Cache hit rate: 12% (mostly from mobile app retries)
Idempotency keys are now standard practice in all payment flows.