An Android app queues actions while offline. After the process is killed and restarted, some actions sync twice and the user sees duplicate orders. How do you design the fix?
The queue records that work was sent but cannot know whether the server received it, so any crash between sending and recording the acknowledgement replays the action. At-least-once delivery is the only thing a mobile client can guarantee, so the fix is to make the operation idempotent with a client-generated key persisted alongside the queued action, not to try harder to deliver exactly once.
Answer
Short answer
There is a window between the request leaving the device and the acknowledgement being durably written to the queue. If the process dies inside it, the action is still marked pending and gets replayed on restart — even though the server already applied it. No amount of care closes that window, because the client cannot distinguish "the server never received it" from "the server received it and the response was lost." The fix is to accept at-least-once delivery and make the operation idempotent.
Was this answer helpful?
Likely follow-ups
- Where exactly does the key get generated, and why not on the server?
- The user creates an order offline, then edits it, then goes online. What must your queue guarantee?
- How long should the server retain idempotency keys, and what breaks when it forgets?
- What happens if the response was a 500 - do you retry with the same key or a new one?
- How would you test this without being able to reproduce process death reliably?
Related questions
- You must ingest all of English Wikipedia every day, and the source does not want to be hammered. What runs, how often, and what do you do about the run that fails at hour six?hardAlso on idempotency6 min
- Your services talk through events and one consumer has been down for an hour. What has it missed, and how does it catch up?hardAlso on idempotency6 min
- The monthly bill run dies two thirds of the way through and the cycle closes tomorrow. What do you do?hardAlso on idempotency5 min
- How would you design a data pipeline you can safely re-run?hardAlso on idempotency6 min