fix(telemetry): dedupe retries by uuid and bound the send queue (v1.55.3) - #190
Merged
Conversation
…5.3) PostHog dedupes on the event uuid, not on $insert_id, and a CLI process aborts in-flight sends at exit after the body has usually already left. So every quick command re-sent the whole backlog, PostHog ingested each copy as a new event, and nothing ever pruned the queue: one user's queue re-sent every rollup ~230 times, and two users alone were 93% of all CLI Usage Rollup events (~1M/week). - every queued event carries a uuid derived from its $insert_id and sends it, so a re-sent copy collapses on ingest; lines written by an older CLI get one when drained, so an inherited backlog dedupes too - drainQueue counts dispatches per event (persisted before the send) and drops an event after 3 attempts or 24h; never dispatches the same event twice in one run - flush() lets in-flight sends finish for up to 300ms before aborting, and postAction drains the queue so a rollup emitted at exit usually lands in the same run - rollups are sent with $process_person_profile: false (anonymous rate) and without $set; the person already exists from the signup events Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Part of the PostHog optimization plan (Cleanup → volume reduction).
CLI Usage Rollupis 86% of all PostHog events (~1M/week), and 93% of that comes from two users' Macs — not from heavy usage, but from the CLI re-sending the same rollups over and over:uuid, not on$insert_id, which the CLI relied on.command_countby those duplicates.Changes (
src/lib/analytics.ts,src/cli.ts)uuidderived deterministically from its$insert_id(uuidFromInsertId, v5-shaped) and sends it in the POST body. Queue lines written by an older CLI get one when drained, so an inherited backlog dedupes on upgrade.drainQueue()counts dispatches per event (persisted before the send so a crash can't reset it), drops an event afterSEND_MAX_ATTEMPTS = 3orQUEUE_MAX_AGE_MS = 24h, and never dispatches the same event twice in one run.flush()(now async) waits up toEXIT_GRACE_MS = 300for in-flight sends only when there are any, then aborts the rest.postActionalso drains the queue so a rollup emitted at exit usually lands in the same run instead of on the next one. Worst case added exit latency: 300 ms, only on runs that had something to send.$process_person_profile: falseand no$set(sheet row: "rollups do not need person profiles"). Note: person-property filters (e.g. the project's internal-user filter by email) won't apply to rollup events; they still key ondistinct_id.Expected effect once users update: CLI events drop from ~1M/week to roughly 70K/week.
Test plan
src/lib/analytics.test.ts: 27 tests — the 17 existing rollup tests plus 10 new delivery tests using a stubbedfetch(uuid derivation + legacy backfill, uuid in the wire body, delivered events pruned, grace period lets a slow send land, attempts counted and capped, age cap, no double dispatch per run, failed send retried).npm run typecheckclean;npm run build(tsup) succeeds.resolveConfigfailures insrc/lib/config.test.tsfail identically on a cleanmain(verified by stashing this change) and are unrelated.CLI Usage Rollupvolume per user in PostHog (HogQLcount() / uniq($insert_id)per distinct_id) drops to ~1.🤖 Generated with Claude Code