Scrivio Interview library
ScenarioAdvanced6 min read · updated 13 November 2026

A payments service stored idempotency keys and per-card rate-limit counters only in Redis Cluster. A primary failed, the promoted replica was about 4 seconds behind, and 37 card charges were processed twice when clients retried.

You lead the follow-up across the three teams involved. What do you change in the design, and what rules do you set for Redis in payment flows?

The idempotency keys written in the last four seconds before the failure were acknowledged by the old primary but never reached the replica that was promoted, so the clients' retries looked new and were charged again; that is expected behaviour of asynchronous replication, not a Redis bug. I would move the idempotency record into the payment database, written in the same transaction as the payment with a unique constraint, keep Redis only as a fast pre-check that may be wrong, send the idempotency key on to the payment provider as a last line of defence, leave rate-limit counters in Redis by explicit decision, and set the rule that no money-flow correctness may depend on Redis keeping an acknowledged write.

Why this question is asked

Thirty-seven double charges is a small number with a large cost: refunds, support calls, possible regulatory reporting and lost trust. Three teams are involved, and each can easily believe the problem belongs to another, so the leader's job is to explain the mechanism once, clearly, and then change the design so it cannot happen again. Interviewers use this scenario to see whether we fix the class of problem rather than the instance. In this answer, we will explain what happened, redesign the idempotency check, add a defence at the provider, decide what may stay in Redis, and set the rules and tests that keep it fixed.

1A Quick Recap of Idempotency Keys

An idempotency key is a unique id the client sends with a request, such as a UUID per checkout attempt. If the client times out and retries with the same key, the server recognises it and returns the original result instead of charging again. The server must therefore remember every key it has processed, at least for a retry period such as 24 hours.

In this system, the memory of processed keys lived only in Redis Cluster. That works as long as Redis never forgets an acknowledged write. As we will see, it can.

2Explaining the Mechanism in the Review

The review should start with the mechanism, stated plainly so all three teams agree on it. Redis Cluster replicates asynchronously: the primary replies OK before its replica has the write. When the primary failed, its replica was about four seconds behind, so keys written in those last four seconds existed only on the failed primary. The replica was promoted, the clients retried their timed-out requests, the payment service looked up the keys, found nothing, and processed the charges again.

It is important to say that this is Redis doing what it is designed to do. Nobody misconfigured it, and a patch to Redis would not help. The design assumed a guarantee that Redis never offered, and that assumption is what we change.

Step throughHow a retry became a second charge
ClientPayment serviceRedis primaryReplica (4 s behind)Card network
charge, key K1
SET idem:K1 (OK)
charge card (succeeds)
fails; K1 never streamed
promoted to primary
retry after timeout, key K1
GET idem:K1 -> nil
charge card again

8 messages in order. Play them, or step through them one at a time.

The retry looked new because the only record of K1 died with the old primary.

3Moving the Idempotency Record to a Durable Store

The fix is to make the idempotency record as durable as the payment itself, by writing both in one database transaction. A unique constraint on the key means a second attempt cannot insert the same key, whatever any cache says.

SQL
CREATE TABLE payment (
id BIGSERIAL PRIMARY KEY,
idempotency_key TEXT NOT NULL UNIQUE,
card_ref TEXT NOT NULL,
amount_cents BIGINT NOT NULL,
status TEXT NOT NULL, -- PENDING, CHARGED, FAILED
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
-- first attempt
INSERT INTO payment (idempotency_key, card_ref, amount_cents, status)
VALUES ('K1', 'card_9', 4999, 'PENDING'); -- INSERT 0 1
-- retry with the same key
INSERT INTO payment (idempotency_key, card_ref, amount_cents, status)
VALUES ('K1', 'card_9', 4999, 'PENDING'); -- ERROR: duplicate key value violates unique constraint

The service inserts the PENDING row before calling the provider and updates it to CHARGED afterwards. On a duplicate key, it reads the existing row and returns its result. Redis can still sit in front as a pre-check, because most retries are then answered in a millisecond, but it is allowed to be wrong: a miss in Redis falls through to the database, and the unique constraint decides.

4A Last Line of Defence at the Provider

Our database protects our side, but a payment passes through several systems, and a bug or a crash between them could still produce two calls. Most card payment providers accept an idempotency key on their API and return the original result for a repeated key within their retention period. Passing our key, or a key derived from it, to the provider means that even if every internal check failed, the duplicate request would be rejected at the last step, where the money actually moves. Check the provider's documentation for how long keys are kept and what counts as the same request.

5Deciding What May Stay in Redis

Not everything has to leave Redis. Per-card rate-limit counters exist to stop abuse, and losing four seconds of counts after a failover means a card might get a few extra attempts in a rare window. That is tolerable. The point is to write the decision down, so the next engineer knows it was made on purpose.

Payment-flow data and where it lives
DataCost of losing recent writesDecision
Idempotency keysduplicate chargesdatabase, unique key; Redis pre-check only
Payment statuswrong answers to customersdatabase
Per-card rate-limit countersa few extra attemptsRedis, documented as lossy
Distributed locks on paymentstwo workers act at oncefencing tokens checked by the database
Cached card metadataa reloadRedis cache

6Rules and Tests for Redis in Payment Flows

The one rule I would set across the three teams is short: no correctness guarantee in a money flow may depend on Redis keeping an acknowledged write. Everything else follows from it. Locks, deduplication keys, counters and queues in existing flows get reviewed against the rule, and each one is either moved, given a durable backstop such as a database unique constraint or fencing token, or documented as tolerating loss.

Then we prove it. A failover test in staging runs under realistic load, kills a primary while clients retry, and counts charges per idempotency key afterwards; any count above one fails the test. Running it before every major change to the payment path is what turns the rule into something the teams can trust.

7Follow-up questions

How would you find which customers need refunds?

From records that do not depend on Redis. I would take the provider's settlement or transaction report for the incident window and group charges by card, amount and the client's idempotency key or order id where the provider stored it; pairs with the same order and amount seconds apart are the duplicates. I would cross-check them against our payment table and the application logs of the retries, and confirm the count matches the 37. Then refund proactively and tell the customers before they notice.

Is WAIT on the idempotency key write enough instead of moving it?

No. WAIT makes it less likely that the key is lost, but a failover can still promote a replica that missed it, and when WAIT times out there is no rollback. For money we need a guarantee, which the database's unique constraint gives.

Which team owns the fix?

The payment service team owns the idempotency design, the platform team owns the Redis guidance and the failover test environment, and the client team owns retry behaviour and key generation. I would give each a named action with a date, and keep one tracking document so the review closes cleanly.

8Conclusion

In this answer, we explained the double charges as acknowledged idempotency keys lost in an asynchronous failover, which is how Redis is designed to behave. We moved the idempotency record into the payment database with a unique constraint, kept Redis as a pre-check that may be wrong, passed the key to the payment provider as a last defence, and documented that rate-limit counters may stay in Redis. Finally we set one rule, that no money-flow correctness may depend on Redis keeping an acknowledged write, and backed it with a review of existing flows and failover tests under load.

What to say in the interviewAbout 90 seconds aloud

Open with

I would start the review by explaining the mechanism, so the three teams agree on it. Redis Cluster replicates asynchronously. The idempotency keys written in the last four seconds were acknowledged by the primary but never reached the replica, and when that replica was promoted, the clients' retries found no key and were charged again.

Then, if they want more

That is expected behaviour of asynchronous replication, not a Redis bug; the design assumed a guarantee Redis never gave. The main change is to move the idempotency record into the payment database, inserted in the same transaction as the payment, with a unique constraint on the key, so a retry can never insert twice.

Redis can stay in front as a fast pre-check, but it is allowed to be wrong. I would also send the idempotency key to the payment provider, so a duplicate is rejected at the last step even if every internal check fails.

Rate-limit counters can stay in Redis, because losing a few seconds of counts is tolerable, and I would write that classification down. Then I would set a rule: no correctness guarantee in a money flow may depend on Redis keeping an acknowledged write.

We review existing flows for locks, dedupe keys and counters that assume it, and we test failover under load in staging by counting charges per key.

About this answer

Last updated
13 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
Redis documentation
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "You lead the follow-up across the three teams involved. What do you change in the...": https://scrivio-interview-library.netlify.app/databases/redis-scaling/7041cb7fcb/