A meditation app's analytics queue grows on phones offline for weeks while frame-time samples flood it; how should the queue be capped, and which events may be sampled?
answer
- best-effort, so it needs a ceiling
- count, bytes and maximum age
- evict the cheapest class first
- count what you dropped
- sample by install, record the rate
basics
~20 sCap the queue by count, bytes and age; when it is full, evict the lowest-value class first and count every drop. Sample only high-volume diagnostics, per install, with the rate on each event; never sample the paywall funnel, consent or exposures.
solid answer
~50 sAn analytics queue is best-effort, so it gets limits: a maximum event count, a byte size and an age, all the scenario's own assumptions such as 10,000 events, 5 MB and 30 days. Events fall into classes. When the queue is full, diagnostic samples such as frame times are evicted first, then ordinary product events, and purchase, consent and experiment-exposure events last, oldest first within a class. Every eviction increments a persisted counter per class and reason, sent with the next batch, so dashboards know what is missing. Volume is better cut before the queue: sample the high-volume diagnostics by a deterministic choice per install, so sampled users keep complete histories, and stamp the rate on every event so counts are re-weighted even when builds use different rates. Funnel, consent and exposure events are never sampled, because they count users and money.
code
pseudocode · 18 lineslimits = { max_events: 10000, max_bytes: 5 MB, max_age: 30 days } # scenario assumptions
rank = { diagnostic: 0, product: 1, critical: 2 }
function enqueue(event):
rate = sampling_rate_for(event.name) # 1.0 for never-sampled events
if hash(install_id + sampling_salt) mod 10000 >= rate * 10000:
return # this install is not sampled
event.sample_rate = rate
queue.delete_older_than(now() - limits.max_age, count_into = dropped["age"])
while queue.would_exceed(limits, event):
victim = queue.oldest_of_lowest_rank()
if victim == none or rank[victim.class] > rank[event.class]:
dropped["cap_" + event.class] += 1 # the new event is the cheapest
return
queue.delete(victim)
dropped["cap_" + victim.class] += 1
queue.append(event)
# dropped counters are persisted and sent as a queue_dropped event with the next batchgo deeper
Recall that the analytics queue has limits and that only high-volume diagnostic events are sampled.
Explain the three limits, the eviction order by class, and why the sampling rate is recorded on every event.
Justify per-install sampling, drop counters plus sequence gaps, and why funnel, consent and exposure events are never sampled or evicted early.
Set the classes, caps and sampling rules every team's events follow, and who may change a rate remotely without breaking long-running series.
## Why an analytics queue needs a ceiling Some phones stay offline for weeks: a second phone used only for sleep sounds, a traveller with data roaming off. Meanwhile the app records a frame-time sample on every scroll and a launch-duration sample on every start. Without limits the queue grows without end, takes disk space the user notices, and finally tries to upload a huge backlog in one go. Unlike the outbox of the user's own changes, an analytics queue **may drop events**; the design question is which, and how to know. ## The limits Three limits apply together. The figures are this scenario's assumptions, not a standard: - **Count**: at most 10,000 events. - **Size**: at most 5 MB on disk. - **Age**: events older than 30 days are discarded, which also bounds how long the server must remember event ids for deduplication. Count and bytes are both needed: a few events with large properties can fill the disk long before the count limit, and many tiny ones can make a single upload slow long before the byte limit. ## What is evicted first Every event belongs to a class recorded in its schema: | Class | Examples | When the queue is full | |---|---|---| | Diagnostic | frame-time samples, launch durations | evicted first, oldest first | | Product | `session_started`, `reminder_opened`, `streak_extended` | evicted next, oldest first | | Critical | `trial_started`, `consent_changed`, experiment exposures | evicted last, and only by newer critical events | A new event that is cheaper than everything in a full queue is itself dropped. Evicting by class keeps the numbers that matter for revenue and experiments alive longest; evicting blindly by age would throw away a purchase to keep a frame sample. ## Counting the drops A dropped event that leaves no trace makes the data look complete when it is not. So: 1. Keep persisted counters per class and reason: over the cap, over the age limit, rejected by the server. 2. Send them as a `queue_dropped` event with the next batch, then reset them. 3. Combine them with per-install sequence numbers, whose gaps show loss the counters could not see, such as an uninstall. Analysts can then exclude or correct cohorts with heavy loss instead of reporting their numbers as truth. ## Sampling before the queue Capping deals with overflow; **sampling** decides what volume is recorded in the first place. The meaning of a frame-time or launch-time metric belongs to the performance-budget design; the pipeline's job is to ship enough of them, cheaply. - **What may be sampled**: high-volume diagnostics whose value is in their distribution, such as frame times and launch durations. - **What is never sampled**: the paywall funnel and purchases, consent changes, experiment exposures, and any event used to count users or money. Re-weighting a rare event from a sample turns an exact count into a noisy estimate, and a funnel needs each user's complete sequence. - **Decide per install, not per event.** Hash the install id with a salt and keep the install when the result falls below the rate. Sampled installs send all their diagnostics, so per-user and per-device figures stay whole. Per-event coin flips give everyone a thin random slice and break anything computed per user or per session. - **Stamp the rate on every event.** The rate comes from remote configuration and changes; old builds that never fetched the new value keep the old one. With `sample_rate` on each event, every count is divided by its own rate and series stay continuous across changes and builds. - **Decide before queuing.** An unsampled event never touches the disk or the radio, which is where the saving comes from. ## Why both, and in this order Sampling cuts the steady volume; the cap protects against the long offline stretch that sampling cannot predict. Counting drops keeps both honest. Together they keep the queue's disk use bounded, the eventual upload modest, and the data's gaps visible. When a phone that was offline for weeks finally reconnects, the uploader still drains the backlog in ordinary batch sizes rather than one huge request, sending the critical class first, so a connection that drops halfway has already delivered the events that matter most.
- Why sample per install instead of flipping a coin for each frame-time sample?Per-event coin flips give every user a thin random slice, so per-user or per-device figures, such as one user's slowest launches, become noisy and skewed toward heavy users. Per-install sampling keeps the complete stream for a fixed subset of installs, and their counts re-weight cleanly by the recorded rate.
- The team raises the frame-time sampling rate from 5 % to 20 % remotely; what breaks if events do not carry their rate?Totals jump fourfold overnight with no real change, while old builds that never fetched the new value still sample at 5 %. With the rate on each event, each count is divided by its own rate and the series stays continuous across the change and across builds.
- An analytics queue should grow without limit so no event is lost.
- Per-event random sampling keeps per-user metrics accurate.
- Sampling purchases at 10 % is as good as counting them all.
- Dropped analytics events need no record; they were best-effort anyway.
- When a full queue evicts by age alone, the important events survive.