The reconciliation sweep
The normative pattern for keeping your own copy of the feed complete — a cursor, a watermark, and an idempotent apply
If you keep your own copy of what the engine records — a replica, a warehouse table, a job queue — this page is the contract you build against. It is short, and it is normative: a consumer that follows it converges on the complete feed, and a consumer that skips part of it will lose activities in a way that is very hard to notice.
Every pass makes two reads:
- Advance an id cursor to pick up everything new.
- Separately re-read an overlap window anchored to a time watermark you persist, with no cursor on it at all.
Then deduplicate the union by act_ id and apply it idempotently.
The second read is not a belt-and-braces nicety. Without it an id cursor silently misses activities forever — see below.
Why an id cursor alone is not enough
Activity ids are time-ordered, which is what makes them usable as a cursor at
all. The id is minted before commit. created is stamped with database
clock_timestamp() when the activity is appended, and the row becomes readable
only when the transaction commits. Two pieces of work do not necessarily commit
in the order they started.
So this can happen:
14:07:41 work A begins → act_…c11, created 14:07:41
14:07:41 work B begins → act_…c12, created 14:07:41
14:07:42 work B commits → act_…c12 is now readable
14:07:42 your pass reads, applies act_…c12, stores it as its cursor
14:07:43 work A commits → act_…c11 is now readableYour cursor is at …c12. Every future read asks for activities after …c12.
…c11 is lower, so it is never returned again. It is not delayed; it is gone
from your consumer's point of view, and nothing in any response tells you.
The window is small — the length of one write — but on a busy on-sale you are making thousands of them, and the activity you lose is as likely to be a sale as anything else.
The fix is to re-read a slice of recent time with no cursor on it, because a late-committing activity is returned by a time filter once it lands.
First run — no cursor yet
A consumer with nothing stored has two jobs: start following the feed forward, and backfill what is already there. Do them in that order, because history is retained indefinitely and does not run away from you, while new activity does.
Read the newest page with no cursor at all. Note the time before you send it — that is your first watermark.
curl "https://tickets.lissen.com/api/v1/activities?limit=100" \
-H "Authorization: Bearer tk_test_..." \
-H "Ticketing-Organizer: org_0199c4e2-1f7a-7c31-8d44-2b9f6a1e05c7"Store data[0].id as your forward cursor before you do anything else. Every
list comes back newest-first, so that is the newest activity in existence for
you, and it is where normal passes will resume from.
Page backward through history with starting_after set to the oldest id
in the page you just read — the last element — repeating until has_more is
false. This walks away from the present, so it cannot race the forward cursor
you already stored.
Begin normal passes from the stored forward cursor and watermark. From here on, every pass is the two reads below.
Persist bootstrap as incomplete. Atomically apply the initial page and store
data[0].id as the forward cursor, then durably apply each starting_after
page while persisting a separate backfill cursor. Mark bootstrap complete only
when has_more is false. If the first page is empty, store the watermark with a
null forward cursor.
Pass 1 — advance the id cursor
Read forward from the last id you applied, using ending_before.
curl "https://tickets.lissen.com/api/v1/activities?limit=100&ending_before=act_0199c4e6-3a5b-7c88-9d02-6e4a1f7b3c50" \
-H "Authorization: Bearer tk_test_..." \
-H "Ticketing-Organizer: org_0199c4e2-1f7a-7c31-8d44-2b9f6a1e05c7"ending_before returns the activities immediately after that id — the oldest
ones you have not seen. Every page is newest-first, so the newest item in a page
is data[0], and that is what you pass as the next ending_before. Keep going
while has_more is true.
Store the cursor only after you have durably applied the page. A cursor that moves ahead of your writes is a gap you cannot detect.
Pass 2 — the watermark overlap
Ask a second question that mentions no cursor at all, anchored to a timestamp you persist:
curl "https://tickets.lissen.com/api/v1/activities?limit=100&created%5Bgte%5D=2026-11-02T13:00:00Z" \
-H "Authorization: Bearer tk_test_..." \
-H "Ticketing-Organizer: org_0199c4e2-1f7a-7c31-8d44-2b9f6a1e05c7"Never anchor this read to your last-applied id. Sending created[gte]
together with starting_after or ending_before is a legal query, and it is
the one thing that defeats the whole pattern: the id filter excludes exactly
the late-committing activities you are re-reading time to find.
The watermark rule
A fixed rolling window — "always re-read the last 24 hours" — is not enough,
and the failure is quiet. If a pass fails, or your consumer is down, or two
passes simply fall far enough apart, an activity whose created slid out of the
window while it was still uncommitted is never re-read by any later pass. It is
below the cursor and before the window: gone from both reads at once.
So anchor the window to a watermark rather than to now:
Note the time at which this overlap pass starts. Call it T.
Query from your stored watermark minus a margin — created[gte] = watermark − margin. The margin covers the gap between an activity being stamped and
becoming readable, plus the clock difference between you and us.
Page through the whole window from the top, walking backwards with
starting_after set to the oldest id in the page you just read, until
has_more is false. That cursor is scoped to this one read; it is never your
consumer's position.
Advance the watermark to T only when the entire overlap is durably
applied. A pass that failed halfway leaves the watermark where it was, so the
next pass covers the same ground again. This is the property a fixed window does
not have.
Choosing the margin
Choose a margin greater than the maximum time from activity append to commit visibility, plus worst-case clock skew. The values below are operating recommendations, not completeness guarantees; if that bound is exceeded, a late commit can still be missed.
The margin costs bandwidth and nothing else, so err generously:
| Pass cadence | A sensible margin |
|---|---|
| Continuous (every few seconds) | 1 hour |
| Hourly | 6 hours |
| Nightly | 48 hours |
These are recommendations, not engine limits. History is retained indefinitely, so an over-generous margin is never wrong — it only reads rows you will recognise and skip.
Redacted activities
An activity whose snapshot the engine could not serve in full arrives as an
envelope with data.object reduced to its id and data.redacted: true.
Do not apply one, and do not mark it complete.
Persist it as pending — the id, the type, the created, and the fact that
the payload is missing.
Advance your cursor past it anyway. It is a real activity and blocking on it would stall everything behind it.
Re-fetch that one activity by id on a later pass, until it comes back without the marker. Then apply it and clear the pending flag.
curl https://tickets.lissen.com/api/v1/activities/{id} \
-H "Authorization: Bearer tk_test_..." \
-H "Ticketing-Organizer: org_0199c4e2-1f7a-7c31-8d44-2b9f6a1e05c7"Completing a resource from a redacted payload writes a row whose fields you never saw, and it looks exactly like a row you did see. A pending marker is recoverable; a wrong row is not.
Deduplicate and apply
Union the two passes, deduplicate on id, and apply each activity exactly
once — or apply it repeatedly with no additional effect, which is easier to
guarantee and just as good.
const seen = new Set();
for (const activity of [...fromCursor, ...fromOverlap]) {
if (seen.has(activity.id)) continue;
seen.add(activity.id);
if (activity.data.redacted) {
await markPending(activity); // never apply a redacted snapshot
continue;
}
await apply(activity); // must be idempotent on activity.id
}Two properties make idempotent application straightforward:
data.objectis a full snapshot, not a patch. Applying the same activity twice writes the same row twice — the second write changes nothing.act_ids are unique and stable. Record the ids you have applied (a primary key on your side is enough) and a repeat is a no-op by construction.
Because there is no ordering promise, prefer the snapshot with the later
created when two activities touch one resource. Do not resolve them by arrival
order.
Putting it together
Read your stored forward cursor and watermark. If you have neither, run the first-run procedure above.
Note the time T, then page forward with ending_before until has_more is
false, applying as you go and advancing the cursor behind your writes.
Read the overlap from watermark − margin with no consumer cursor, paging
within it until has_more is false.
Skip ids you have already applied, mark redacted ones pending, apply the rest,
then advance the watermark to T.
That is the whole pattern. It is the recovery rail for the current outbound webhook delivery: if an endpoint is disabled after repeated failures, fix it, re-enable it, and run one sweep to close the gap. A replica built from deliveries and a replica built from sweeps are the same replica by construction. See verifying signatures and webhooks and the outbound feed.
Gotchas
- Do not use
createdas your cursor. It is not unique, and paging on a timestamp will either skip or duplicate activities at a page boundary. The id is the cursor;created[gte]is only ever the overlap filter. - Do not combine
created[gte]with your consumer's cursor. It is the one combination that hides the failure this page exists to prevent. - Do not anchor the overlap to "now minus a window". Anchor it to the watermark, and move the watermark only on success.
- Advance the cursor after applying, never before.
- Do not deduplicate on
request_id. One request legitimately produces several activities, and some activities have none at all. - Tolerate unknown types. A type you do not recognise must not stop a pass — store it, or skip it, but keep the cursor moving.
- Do not assume a sweep is only for outages. It is the normal way to be certain, and the reason the ids are shaped the way they are.