Concepts
Data batching and offline behavior
How the SDK queues events on disk, when it sends them, how it retries, what it drops, and how it behaves without a network.
The SDK never sends an event the moment you record it. It writes each event to a queue on disk and sends the queue in batches, so data survives app restarts, poor networks and airplane mode.
When the SDK sends
| Trigger | Default | Setting |
|---|---|---|
| Timer, while the app is in the foreground | Every 10 seconds | iOS flushInterval (seconds), Android flushIntervalSeconds |
| Queue size | 20 queued events | iOS and Android flushAt |
| App moves to the background | Always | None |
| Your app asks | When you call flush() | None |
On iOS, the SDK asks the system for background time when the app leaves the foreground, and holds it until the upload finishes.
Herald.configure(appKey: "YOUR_APP_KEY", endpoint: URL(string: "https://YOUR_HERALD_HOST")!) { options in
options.flushInterval = 30
options.flushAt = 50
}Herald.configure(
this,
appKey = "YOUR_APP_KEY",
endpoint = "https://YOUR_HERALD_HOST",
options = HeraldOptions(flushIntervalSeconds = 30, flushAt = 50),
)Confirm delivery before turning the SDK off
flush() starts a send and returns at once. When a last change must reach Herald before you call setEnabled(false), for example clearing the email at sign-out, use the iOS SDK's (0.5.0+) flush(timeout:) and wait for it:
Herald.shared.setAttributes(["email": .null])
let delivered = await Herald.shared.flush(timeout: .seconds(10))
Herald.shared.setEnabled(false)true means everything recorded before the call is on the server, so turning the SDK off afterwards can't lose it. On false, the events still queued stay on the device unsent while the SDK is off, and go out after setEnabled(true); wipeData() deletes them.
How batches are built
- A batch holds up to 100 events and stays under the server's 512 KB request limit. When events do not fit, the SDK halves the batch until they do; the rest wait for the next batch.
- A batch belongs to one user, or to no user. The SDK starts a new batch whenever the user changes, and
identifygoes out in the new user's batch. Events recorded before sign-in go out as an anonymous batch. When the user then signs in on that device, Herald links that device's recent guest events to them: see Guests and signed-in users. - Events go out in the order you recorded them. One exception: a push token moves ahead of a push subscription in the same batch, so Herald has the token when it applies the opt-in.
- Each batch carries the current device data. See Device data collected.
Persistence and the queue cap
| Rule | Behavior |
|---|---|
| Storage | iOS: a file in Application Support, excluded from backups. Android: a file in the app's no-backup files directory, excluded from backups (the files directory in SDK 0.4.0 and earlier). |
| Survives | App restarts, crashes, updates and reboots |
| Cap | 1,000 events |
| When full | The oldest events are dropped to make room |
| Max age | SDK 0.6.0 and later drop queued events more than 30 days old before building a batch, since Herald would reject them as too_old (maxQueuedEventAge on iOS, maxQueuedEventAgeDays on Android) |
| Dropped count | Saved, and sent as dropped on the next batch; cleared once a batch that reported it is accepted. Counts events dropped for the cap, for size and for age. |
Cards and in-app messages are cached on disk too, so they show without a network after a restart.
Retries and backoff
| Response | What the SDK does |
|---|---|
| 2xx | Removes the batch from the queue and sends the next one. Events Herald couldn't accept are listed in the response's rejected and logged at debug level; they are never retried, and the rest of the batch was kept. See Rejected events. |
| 408, 429, 5xx, or no network | Keeps the batch and retries later, with exponential backoff and jitter |
401 with the error Invalid app key | Keeps every queued event, logs Herald rejected the app key: check the publishable key in configure, and sends nothing more until the app launches again (with the key fixed). The queue limit still applies. |
| 401 for a signed-in user's batch (missing or wrong user token) | Keeps the batch, logs Herald needs a user token: call setUserToken once, and holds that user's events until the app calls setUserToken, which sends them. Anonymous events keep flowing. The queue limit still applies, so the oldest events are dropped if the token never comes. |
| Any other 4xx (400, 403, 404, 413) | Drops the batch and logs an error; retrying would not help |
Backoff starts at about 1 second, doubles after each failure, and stops growing at 5 minutes. Each wait is a random time between half and all of the computed delay, so many devices do not retry at the same moment. A Retry-After header from the server takes precedence, in seconds or as an HTTP date, and so does a retryAfterSeconds field in the response body. Either is capped at one hour. SDK calls still return at once during a backoff, and the events they record join the queue.
Idempotency
Every event has a unique ID that the SDK creates. Herald stores each event ID once per connection and reports repeats as duplicates. The SDK removes events from its queue only after a response, so if the app is killed mid-upload, the next attempt re-sends the same IDs and nothing is counted twice.
Clock correction
Phone clocks are sometimes wrong by minutes or hours. Each batch carries sentAt, the device's time when it was sent, from the same clock as the event timestamps. Herald notes when the batch arrived and moves every event by the difference: the stored event time is arrival time minus (sentAt minus the event's timestamp), and never later than arrival. An event recorded 10 minutes before the send lands 10 minutes before arrival, whatever the phone's clock said, and so do events that waited offline for days.
Activity, the preferred send time, goal attribution and every other use of event time read the corrected time. The device's own timestamp is kept too. Events whose corrected time is more than 30 days old are rejected as too_old. Server events (POST /api/v1/events) are not corrected and have no age limit: Herald trusts your server's clock, so historical imports are kept, and rejects only events more than 5 minutes ahead of arrival as in_future.
Offline behavior
- Recording keeps working: events go to the disk queue, up to the cap.
- Uploads wait: each attempt fails and backs off until the network returns.
- Events queued for more than 30 days are dropped on the device (SDK 0.6.0 and later) and counted in
dropped; from an earlier SDK, Herald rejects them astoo_oldwhen they finally arrive. - Cards and in-app messages come from the last successful fetch. In-app triggers are evaluated on the device, so they fire offline.
- Interactions with cached messages are queued like any other event and credited once they reach Herald.