In this lesson · topic 4 of 7
Topic 4
Loss, resends and the order they create
6 min read
Loss reorders
The server sends message 5. The network drops it. The server sends message 6, which arrives. Three ticks later the resend of 5 arrives. On a screen that shows messages as they land, 5 appears below 6.
At 10 percent loss, 5.7 percentmeasured of messages appeared out of order. With no loss it was 0.0 percentmeasured. Loss and resends together cause it. Neither alone does.
Duplicates
A second failure comes from lost acknowledgements. The message arrived, but the ack did not, so the server sends it again. With no check, the client showed 541 duplicatesmeasured in the run. With the check on, it showed 0 duplicatesmeasured.
This is at-least-once delivery, as in delivery guarantees. The server cannot promise exactly once, so the client makes the result look like once. The sequence number is the key. A message with a number the client already holds is dropped.
If the client may resend its own messages, as after a dropped connection, give each one a client-made message id. The server then drops a repeat of the same id. See idempotency and the outbox.
Hold for order
The fix for order is for the client to wait. It keeps a message until all lower numbers have been shown, then shows the run. Out of order fell to 0.0 percentmeasured.
The wait is not free. The 99th percentile delay from send to display was 22 ticksmeasured when showing on arrival and 24 ticksmeasured when holding for order. The median was 0 ticksmeasured either way. Holding costs the tail, not the typical message.
Why a counter, and not a clock
The server assigns the number when it accepts the message, in one place per conversation. That is a single writer, so the order is simple. Clocks on phones disagree. Lamport’s paper on clocks explains why ordering by time across machines needs care, and why a logical counter is the safe tool when one place can assign it.
The cost is that one conversation must be handled by one sequencer at a time. Partition conversations across servers by conversation id. A failover of that server needs a lease, so two servers do not hand out the same number. See leases and fencing.