A meditation app's analytics show more trial starts than the stores report; how do retried event uploads double-count, and how should the pipeline prevent it?
answer
- stored, but the reply was lost
- an id per event, minted when recorded
- the server skips ids it has seen
- accepted, duplicate or rejected
- a sequence number also reveals gaps
basics
~20 sIf the server stores a batch but its reply is lost, the client resends it and each event counts twice. Give each event an id when recorded, keep it in the queue, and let the server skip ids it has stored.
solid answer
~50 sThe queue deletes a batch only after the server acknowledges it. That is right for loss, but every lost acknowledgement then causes a resend: a timeout after the server stored the batch, the app ended between the response and the delete, two uploaders sending the same rows, or an unsent queue restored from a backup onto a new phone. The fix is an id per event: a random UUID generated when `track()` records it, stored with the event and never regenerated at upload. The ingest service remembers ids for as long as a client may retry and answers a repeat as already accepted, so the client deletes it. A single uploader avoids self-inflicted copies, and invalid events are rejected for good rather than retried. Revenue itself should come from verified purchase records; analytics events describe the funnel.
code
http · 18 linesPOST /v1/events HTTP/1.1
Content-Type: application/json
{"install_id": "b2e5f1c4-7a3d-4e9b-8f21-6c0d9a4e3b17",
"events": [
{"id": "6f1c2a9e-8d41-4b7a-9c0e-5a1d2b7e4f90", "seq": 4182, "name": "trial_started", "props": {"plan": "annual"}},
{"id": "a93e0b12-55c7-4f0e-a1d4-2c7b9e8f6a31", "seq": 4183, "name": "session_started", "props": {"kind": "sleep_story"}},
{"id": "0d4b7c6e-2f19-4a8b-b3e5-91c2d7f0a6e4", "seq": 4184, "name": "plan_selected", "props": {}}
]}
HTTP/1.1 200 OK
Content-Type: application/json
{"results": [
{"id": "6f1c2a9e-8d41-4b7a-9c0e-5a1d2b7e4f90", "status": "duplicate"},
{"id": "a93e0b12-55c7-4f0e-a1d4-2c7b9e8f6a31", "status": "accepted"},
{"id": "0d4b7c6e-2f19-4a8b-b3e5-91c2d7f0a6e4", "status": "rejected", "reason": "missing_property:plan"}
]}go deeper
Recall that a retried upload can resend events the server already stored, and that an id on each event lets the server skip the copy.
Explain when the id is minted, how the server answers a known id, and why a rejected event is dropped with a counter rather than retried.
Name every duplicate source on a phone, including two uploaders and restored queues, and add sequence numbers so event loss is measured rather than guessed.
Decide which numbers come from analytics and which from verified purchase records, and require every team's events to carry ids the shared ingest can deduplicate.
## Where the extra trial starts come from A well-built analytics queue keeps a batch until the server says it was stored. That rule never loses an acknowledged event, but it turns every lost acknowledgement into a **duplicate**. On a phone the common sources are: | Cause | What happened | Without event ids | |---|---|---| | Lost response | the server stored the batch; the reply timed out on a weak signal | the whole batch is sent again | | Process ended | the reply arrived, but the app was ended before it deleted the rows | the batch is sent again on next launch | | Two uploaders | a foreground flush and a background job read the same oldest rows | both send them | | Restored queue | the unsent queue was in a device backup and was restored onto a new phone | old events arrive again from a second device | | Double tracking | the trial screen calls `track` twice when it redraws | two events with two different ids | The last row is different in kind. Two `track` calls are two events, and nothing in the transport can merge them. That one is fixed in the code and the event definition: `trial_started` fires once, where the purchase result is confirmed, not in a view that can be drawn twice. ## Ids minted when the event is recorded Give each event a **unique id** at the moment it is recorded: - generate a random UUID (version 4) inside `track()`; - store it in the queue together with the event; - send it unchanged on every attempt, from every uploader. A version 4 UUID carries 122 random bits, so two phones minting the same id is not a practical concern, and no coordination with the server is needed to create one offline. An id generated at upload time would be new on each retry and would defeat the purpose. A time-ordered UUID is just as unique, but its time part comes from the phone's clock, which may be wrong, so it must not be read as the event's time. ## What the ingest service does with the ids The ingest service keeps a store of the ids it has accepted, for at least as long as a client could still retry; the scenario assumes 30 days, the queue's own maximum age. 1. For each event in the batch, look its id up. 2. A new id: store the event and remember the id. 3. A known id: skip the event, but report it as accepted, because it is. 4. An invalid event, such as an unknown name or a missing required property: reject that event, with a reason. The response reports a result per event, so the client deletes everything accepted or duplicate and drops permanently rejected events with a counter instead of resending them forever. A `429` or `503` with `Retry-After` leaves the batch where it is; how retries back off is the request pipeline's business. ## Removing duplicates at the source - **One uploader at a time.** A lock or a single serial worker owns the head of the queue, so a foreground flush and a background job cannot send the same rows together. - **Mark rows in flight.** Rows being sent are marked; another flush takes only unmarked rows, and the mark is cleared if the attempt fails. - **Keep the queue out of backups where possible**, or accept that ids will absorb the rare restored copy. - **Key the seen-id store by install as well as id.** Lookups stay small, and ids can expire per install once its oldest possible retry has passed. - **Never re-mint ids on migration.** When a new app version changes the queue's storage format, existing rows keep their ids, or a migration becomes a mass duplication. ## Gaps as well as doubles Adding a per-install **sequence number**, incremented and saved with each recorded event, lets the server see both problems. A repeated number is a duplicate; a missing range is loss, from a dropped batch, a cleared queue or an uninstall. The analytics team can then say "this cohort lost about 2 % of its events" from evidence instead of guessing. ## Which number to trust Analytics is best-effort: a capped queue drops events, some users decline consent, some phones never come back online. The funnel from paywall viewed to plan selected to trial started is the analytics pipeline's job; revenue and active subscriptions should come from verified purchase records on the server. A small shortfall in analytics is expected. Analytics showing *more* trials than purchases makes duplicates the first suspect.
- Why not deduplicate by comparing event contents instead of ids?Two genuine events can look identical: a user who starts the same eight-minute session twice in a row produces the same name, the same properties and nearly the same time. Content matching would merge real events, and a time window would still miss a retry that arrives days later. An id says exactly which recording an event is.
- How long must the server remember the ids it has seen?At least as long as a client may still resend a batch, which the queue's maximum age bounds; the team picks that figure, and the scenario uses 30 days. Ids older than that can expire, because the client will already have dropped those events itself.
- Does this make the pipeline exactly-once?It makes every event that arrives count once per id. It cannot recover events that were never recorded, or that the queue dropped under its cap; the sequence number exposes those gaps instead of hiding them.
- Generate each event's id when its batch is uploaded.
- TCP retransmission already guarantees the server sees each event once.
- Answer a repeated event id with an error so the client stops resending.
- Keep retrying a rejected event until the server finally accepts it.
- Analytics trial counts should match the stores' purchase reports exactly.