Why should a meditation app's analytics events go to a persisted on-device queue and upload in batches, when sleep sessions often play offline?
answer
- offline is the normal case
- a kill loses memory, not disk
- not in a purgeable cache folder
- send when big enough or old enough
- delete only after the server says yes
basics
~20 sSleep sessions run offline and the OS may end the app at any moment, so events sent at once or held in memory are lost. A disk queue uploaded in batches keeps them and wakes the radio once per batch.
solid answer
~50 sAt night the phone is often in airplane mode, so a send-now design has nowhere to send, and an in-memory buffer dies when the OS ends the backgrounded app or the battery runs out. So `track()` appends each event to a small on-device store, a table or an append-only file in the app's own data area, not a cache folder the system may purge. An uploader drains it in batches: when the queue reaches a size threshold, when the oldest event reaches an age limit, and when the app returns to the foreground with a network. A batch leaves the queue only after the server acknowledges it, so a dropped connection just means the same batch goes again later. One upload per batch also means one radio wake-up instead of dozens, and far fewer requests for the ingest service.
code
pseudocode · 18 linesfunction track(event):
if not consent.allows(event.purpose):
return
event.id = new_random_uuid()
event.recorded_at = device_clock.now()
queue.append(scrub(event)) # on disk before returning
if queue.count() >= 50 or queue.oldest_age() >= 30 minutes:
uploader.request_flush()
function flush():
if not network.available():
return # wait for the next trigger
batch = queue.oldest(50)
response = post("/v1/events", batch)
if response.status == 200:
queue.delete(batch.ids) # only after the server stored them
else:
retry_later_with_backoff() # the batch stays in the queuego deeper
Recall the four steps: record through one entry point, write to disk, upload in batches, and delete only after the server confirms.
Explain the upload triggers, why the queue lives outside folders the system may purge, and why deletion waits for the server's acknowledgement.
Weigh batch size against freshness and radio cost, and say why an analytics queue may drop events under a cap while the user's outbox may not.
Decide which shared queue and uploader every team must use, so no feature or SDK ships its own send-now path with its own timers.
## Why the phone, not the server, holds events first A **product-analytics event** is a small record that something happened in the app: `session_started`, `paywall_viewed`, `reminder_opened`. The server wants all of them, but in a meditation and sleep app the moment an event happens is often the worst moment to send it: - **The network is often absent.** Many users start a sleep story, switch on airplane mode and put the phone down. A whole night of events happens with no connection at all. - **The process may not survive.** Both operating systems end backgrounded apps to reclaim memory, and a phone can run out of battery overnight. Anything held only in memory is gone, and no "on exit" hook is guaranteed to run first. - **Each send has a cost.** Powering the cellular radio up for one tiny request costs far more energy than the bytes themselves, because the radio stays in a high-power state for a while after every transfer. The radio's cost model is a topic of its own; the conclusion here is simply fewer, larger uploads. ## The shape: record, persist, batch, acknowledge 1. **Record.** Feature code calls one entry point, for example `track(event)`. It checks consent, stamps the event with an id and a timestamp, and removes any property the event's schema does not allow. 2. **Persist.** The event is appended to an on-device queue before `track` returns: a small database table or an append-only file in the app's own data directory. 3. **Batch.** An uploader reads the oldest events, up to a batch size, when a trigger fires. 4. **Acknowledge.** The uploader deletes those events only after the server answers that it has stored the batch. Where the queue lives matters. One platform periodically purges its temporary and cache directories, and both platforms leave cache folders out of device backups, so a queue written there can disappear before it is sent. The app's ordinary data area is the right home. ## When to upload | Trigger | Why it exists | |---|---| | The queue reaches a size, such as 50 events (the scenario's assumption) | each request is worth the radio wake-up it causes | | The oldest event reaches an age, such as 30 minutes while online (assumption) | a quiet user's few events do not wait forever | | The app returns to the foreground with a network | the user is present and the radio is probably already awake | | A deferred background job, if the team uses one | the OS runs it when it chooses, never on an exact clock | While offline the uploader does not try at all; it waits for the next trigger that comes with a connection. A failed request leaves the batch where it is, and the next attempt waits a little longer than the last. ## Why deletion waits for the acknowledgement If the uploader removed events at the moment it *sent* them, a connection that drops mid-request would lose the batch for good. Deleting only after the server confirms means the worst outcome of a failure is sending the same batch twice. That is why every event also carries its own unique id: the server can recognise a batch it has already stored and skip the copies. Losing nothing and counting nothing twice are two separate guarantees, and the queue alone provides only the first. ## What this queue is not - **It is not the outbox** that sends the user's own changes, such as a journal entry or a new reminder time. That outbox must never drop a write and must keep order. An analytics queue is best-effort: it has a size cap and may discard old, low-value events when a phone stays offline for weeks. - **It is not a place to store everything first and decide later.** Each event passes the consent and personal-data checks *before* it is written, because whatever sits on disk is one upload away from leaving the phone. - **It is not one per SDK.** If every analytics library keeps its own queue and timers, the app pays for several radio wake-ups and several sets of rules. One shared queue and uploader keep the behaviour predictable. ## A short checklist - One entry point that every event goes through. - Written to disk before `track` returns. - Uploaded in batches on size, age or return to the foreground. - Removed only after the server confirms. - Kept out of folders the system may purge.
- Why not send each event the moment it happens whenever the phone is online?Each request wakes the radio and keeps it at high power for a while afterwards, so dozens of tiny sends cost far more battery than one batch, and the ingest service takes many more requests. A send-now path still needs a fallback for offline and failed sends, which is the queue; once the queue exists, sending everything through it keeps one code path.
- Should the app flush the queue when it moves to the background?It can try, as one more trigger, but the design must not depend on it: a backgrounded app may get little time and can be ended without running more code. Because the events are already on disk, a flush that never finishes costs nothing; the batch simply goes on the next trigger.
A field researcher writes each observation in ink in a notebook as it happens, posts the filled pages in one envelope from the next post office, and tears pages out only when the office confirms they arrived.
- Keep events in memory and send them all when the app closes.
- Send each event at once; the network is almost always there.
- Delete a batch from the queue as soon as the request is sent.
- Put the event queue in the cache folder because it is temporary data.
- An analytics queue must never drop an event, exactly like the outbox.