Sub topic 13
Event retention and delivery limits
This chapter assumes you can already open a Subscribe stream, decode Avro payloads, and store replay IDs. It covers how long Salesforce keeps events and which limits should shape your subscriber design.
How long events are kept
Salesforce stores published events in the event bus for a fixed window. For platform events and Change Data Capture events, that window is 72 hours (3 days). A replay ID is only useful while the event it points to is still inside the window.
This has a practical consequence. If your subscriber is down for longer than the retention window, the events it missed are gone. Reconnecting with an old replay ID cannot bring them back.
Recovery decisions after an outage
The length of the outage decides which recovery path applies.
To make this check possible, store a timestamp next to every replay ID you save.
import time
RETENTION_SECONDS = 72 * 60 * 60
SAFETY_MARGIN_SECONDS = 6 * 60 * 60
def save_checkpoint(store, replay_id):
store["replay_id"] = replay_id
store["saved_at"] = time.time()
def can_resume(store, now=None):
now = time.time() if now is None else now
if "replay_id" not in store:
return False
age = now - store["saved_at"]
return age < RETENTION_SECONDS - SAFETY_MARGIN_SECONDS
checkpoint = {}
save_checkpoint(checkpoint, b"\x00\x01")
print(can_resume(checkpoint))The safety margin keeps you from trusting a replay ID that expires while you are reconnecting. The saved time is when you processed the event, so it is only an approximation of the event's publish time. That is why the margin is there.
Delivery limits that affect design
Retention is one limit. Several others shape how you build a subscriber.
| Limit | What it affects | Design response |
|---|---|---|
| Retention window (72 hours) | How long you can replay | Checkpoint often, alert on long outages |
| Events per response | Batch size per FetchResponse | Keep `num_requested` modest and process each batch fully before asking for more |
| Daily event delivery allocation | Total events delivered to subscribers in 24 hours | Subscribe only to topics you need and filter on the consumer side |
| Concurrent subscribers | How many clients can listen at once | Share one stream per topic instead of opening many |
Exact numbers for the last three rows depend on your Salesforce edition and any purchased add-ons, and Salesforce changes them over time. Look up the current values in the Salesforce documentation for your org rather than hard-coding them.
Reading the delivery allocation
The delivery allocation counts events sent to subscribers, not events published. Two subscribers on the same topic each consume allocation. A replay also counts again, because the events are delivered a second time.
This affects testing. A loop that reconnects and replays from an old position can use up a large share of a daily allocation in a sandbox.
Design checklist
- Save the replay ID and a timestamp after each processed batch.
- On startup, check the checkpoint age before choosing
CUSTOMorLATEST. - Alert if the subscriber has been disconnected for more than half the retention window.
- Keep a reconciliation path, such as a query on the changed records, for gaps that exceed retention.
- Use one stream per topic and fan out inside your own application.
Exercise
Your subscriber was offline for five days. Its stored replay ID is from before the outage. Can you resume with the CUSTOM preset and recover every missed event? What should you do instead?
Show answer
No. Five days exceeds the 72-hour retention window, so the events from the first two days of the outage are no longer in the event bus and the stored replay ID cannot be relied on. Start with LATEST, then reconcile the gap another way, for example by querying the affected records, and alert on outages that approach the retention window in future.