Task: how to test a transfer system with a queue
Testing a transfer system with a queue starts with risks, not a list of tools. The risks are double debit, a negative balance, a transfer without a final status and a duplicate request. Each risk is tied to the layer where it is cheapest to check, and the weight is on negative scenarios: a duplicate event, a consumer crash, parallel debits, the DLQ. E2E waits for the final status, not a sleep.
System design · Microservices
A transfer is stuck in PENDING and the client pressed Send again. How do you test that?
An interview task: a user creates a transfer through the API. The Transfer Service stores it with status PENDING and publishes an event to a queue. The Ledger Service debits and credits the funds and publishes an event, after which the status becomes COMPLETED. The Notification Service sends a push. The question: how to test it. What is assessed is not a list of tests but the order of reasoning: risks, layers, negative scenarios, metrics.
Risks first
Money must not be debited twice or lost. A balance must not go negative. Every transfer reaches a final status, COMPLETED or FAILED. A repeated client request does not create a second transfer. Each risk is then tied to the layer where it is cheapest to check.
The path of a transfer
Test layers
- Unit: fee calculation, amount validation, status transitions.
- Component Transfer: API plus a real database, the queue a stub or a container. We check that the event is published in the right format.
- Component Ledger: feed an event to the queue and check the records. The same event twice gives one debit.
- Contract: the event format between Transfer and Ledger.
- E2E: create a transfer through the API, wait for COMPLETED, check both balances.
Example: e2e with waiting and a duplicate
The test does not check the status right after POST because the response returns PENDING. It waits for the final state, then repeats the same request with the same key and checks there is no new transfer.
const key = crypto.randomUUID();
const body = { from: 'acc-1', to: 'acc-2', amount: 100 };
const post = () =>
request.post('/transfers', { data: body, headers: { 'Idempotency-Key': key } });
const { id } = await (await post()).json();
await expect
.poll(async () => (await (await request.get('/transfers/' + id)).json()).status, {
timeout: 15_000, // the processing SLA
})
.toBe('COMPLETED');
const again = await (await post()).json();
expect(again.id).toBe(id); // the same transfer
expect(await balance('acc-1')).toBe(900); // one debit
Assumptions: the starting balance of acc-1 is 1000 and the idempotency key is accepted in the Idempotency-Key header. If the balance is 800 after the repeat, idempotency does not work: a double debit is found, which only a repeat test reveals.
Negative scenarios
- Insufficient funds: status FAILED, the balance unchanged.
- Ledger crashed mid-processing: after a restart the transfer completes exactly once.
- An invalid event lands in the DLQ (dead-letter queue) and an alert fires.
- 20 parallel transfers from one account for more than the balance: none ends negative.
- Notification is down: the transfer still completes, the push is retried later.
What to observe
The number of transfers stuck in PENDING longer than N minutes, DLQ size, consumer lag, mismatches in balance reconciliation. These metrics show a problem before a customer complaint.
Limits and alternatives
One e2e run does not prove duplicates are impossible: redelivery and consumer crashes are more reliably reproduced in component tests where the test controls the queue. Even if the broker offers exactly-once within its own bounds, the consumer still has to be idempotent at the boundary with the database.
Common mistakes in the answer
- Listing tools straight away without risks.
- Only the happy path: duplicates, consumer crashes and concurrency are forgotten.
- Checking status with
sleep(5000)instead of waiting for the final state.
Check yourself
Why can an e2e test not check COMPLETED right after POST?
Answer
Processing is asynchronous: POST returns PENDING and Ledger finishes the transfer later. You need to wait for the final state with a timeout based on the SLA.
Which queue behaviour is the most dangerous for money and which test catches it?
Answer
Redelivery: a queue with an at-least-once guarantee may deliver the same event twice. A Ledger component test feeds the event twice and expects one debit.