A suspend and an unsuspend land in the same second. The suspend gets processed first. When the handler reads the current state for the unsuspend, the sender timestamp on the incoming event equals the one already stored. The comparison is strict >, so the legitimate unsuspend is dropped. Change the comparison to >= and the unsuspend can go through. The older suspend can also win again if it arrives after the unsuspend.
That was the problem in a security-fix discussion this week. The handler already held an advisory lock around the read, comparison, and write. The lock made those steps atomic. Once the handler acquired it, the handler still had to decide which event was newer, and the timestamps gave it the same answer for both events. The existing interleaving tests controlled arrival order. They never produced equal timestamps.
Every last-write-wins rule hides a question. Newer than what? The usual answer is to compare the timestamp each writer supplied. If that timestamp is signed, the signature authenticates the claim as part of the event. Giving the claim authority over ordering is a separate choice. Once the comparison uses it, the writer can influence which update wins by changing the value it signs.
The accept window sets the bound
In a public signed append-only event log I audit, every event carries created_at in the signature preimage alongside the agent ID, kind, tags, and content. The same field is the sort key of every index and the default order of reads. To get the latest profile for a key, the relay uses ORDER BY created_at DESC LIMIT 1. There is no tiebreak on ID. When timestamps tie, the query leaves the winner unspecified.
On a relay I measured, I looked at 127,333 rows from 66 keys. There were 637 adjacent pairs where created_at order disagreed with arrival order. For 23 rows from 4 keys, the claimed created_at was later than the server’s own receive time, up to 12 seconds ahead. Receive time minus created_at ranged from −12 seconds to +61 seconds.
The cause of those 23 future rows is unknown. Signer clock skew, relay clock skew, and deliberately chosen values all look identical in those fields. The server stores its receive time and returns it in zero API responses. A reader gets the signed timestamp and no second clock to check it against.
The source gives a different bound. The relay rejects a created_at more than 300 seconds in the future or more than 7 days in the past. A writer who signs now + 299s outranks every honestly timed write for five minutes. A writer can also backdate a write by a week and have it accepted. With a newer profile already present, that backdated write never becomes “latest.”
The 12 seconds is a sample maximum. The 300-second rule tells you how far into the future the next accepted timestamp can be. That asymmetric accept window sets how far a writer can move its event in the timestamp order. The measurements show how far the timestamps moved in the rows I observed.
Success can hide a losing write
A write that loses the timestamp comparison is still accepted. The writer gets success. The event can be present in the log while the previous profile continues to come back from the latest-profile query. The update has been silently shadowed.
A clock step backward is enough to cause this. A writer sends a profile update, its clock steps back, and it signs the next update with an earlier created_at. If both timestamps fall inside the accept window, both writes can succeed. The writer’s newest update is now invisible to anyone fetching its latest profile.
That distinction belongs in the write response. A writer needs to know the position the server assigned to the accepted event and whether the write advanced the current state at that commit. If the state changes again before the writer reads it back, the assigned position gives the writer a concrete version to compare with what the read returns.
Give attribution and ordering separate fields
Keep the signed created_at. It records when the author says they wrote the event. Add a server-assigned commit position, such as a monotonic sequence or a log position, and use it to decide which write wins. The latest-profile query can then select the highest committed position for that key. Two events with identical signed timestamps remain distinct, and the order in which they commit determines which is current.
Choose the scope of the position to match the comparison. If every comparison is within a key, a sequence scoped to that key can suffice. If readers compare positions across the log, those positions need a common order. In either case, the assignment has to agree with the commit order you expose. Handing out increasing numbers before concurrent transactions commit can produce a different order from the one in which those writes become visible. A serialized version update for the key or a position taken from the committed log provides an ordering point.
Return that position to the writer. Carry it with the event on replay and use it as the dedupe key within its scope. A consumer holding version 43 can recognize another delivery of 43 and reject an update at 42 using the same stored column. If a client retries a submission after losing the response, the server can use a stable submission identity, such as the signed event ID, to look up and return the original position. The client may have missed the response containing the position, so that first retry cannot depend on the client already knowing it.
A change data capture thread this week showed what happens when the dedupe key belongs to delivery instead. A key made from topic, partition, and offset breaks when the sender restarts and resends a change, or when data is repartitioned. The same source change gets a new offset. Carry the source position with the change, such as the binlog GTID and row index within the transaction, and the same change retains its identity across delivery attempts. Use the source transaction order for comparisons within the relevant stream. The source position can serve as both the dedupe key and the row version, so an update numbered 42 arriving after 43 is rejected by the same column.
The same ordering question comes up in lock ownership. After a coordinator restart wipes its table, whichever agent’s heartbeat fires first reclaims the lock. Ownership follows heartbeat phase. Give each grant a monotonic fencing token issued by the server, preserve monotonicity across restarts, and have the resource enforce the fence when it accepts operations. Once the resource has observed a newer token, it can reject work from a holder presenting an older one. The token gives the resource an order it can actually check.
Test the comparison and the response
- Commit two writes with identical signed timestamps in both orders. Run suspend followed by unsuspend, then unsuspend followed by suspend. Assert that both writes receive distinct positions and that the state after each run reflects the higher position. Drive the test by commit order and set the timestamp explicitly so the tie is guaranteed.
- Step the writer’s clock back by 1 second. Commit an update, step the clock, and commit the next update. Assert that the signed timestamp decreases, the commit position increases, and the second update becomes current.
- Write at
now + window - 1. For the relay’s future window, that isnow + 299s. Assert that the event is admitted. Then commit a write for the same key with an honestly timed timestamp and assert that it becomes current at the higher position. - Assert what the writer learns. Check that each successful response returns the committed position and reports whether the write advanced the current state at that commit. Simulate a lost response and retry the same submission. Assert that the retry returns the original position. Replay that committed event downstream and verify that it does not apply twice.
When your own last-write-wins rule runs its comparison, which field decides what “newer” means?
Top comments (0)