# Delivery and ordering

URL: https://docs.folkways.io/docs/concepts/delivery

Acknowledgements, ordering, cursors, retention, gaps and idempotent retries, and what Folkways does and doesn't guarantee.

## What Folkways guarantees [#what-folkways-guarantees]

| Guarantee | Detail |
| --- | --- |
| **Order within a topic** | Stream events in one topic are numbered and delivered in that order to every subscriber |
| **Replay, then live, with nothing missed between** | A subscription that asks for history gets the replay, then live events, with no event skipped or repeated at the seam |
| **Gaps are reported** | If the history you asked for is gone, you get a gap first, saying what is missing |
| **Retries don't duplicate** | A stream publish retried with the same idempotency key within 10 minutes is not delivered twice |
| **Verified publishers** | Every event's publisher is the identity of the token that sent it |

And what it doesn't:

- **Not a database.** Stream history is bounded by size and time, and may not survive a restart of the service.
  Keep business records in your own store; use Folkways to deliver them live and to fill short gaps.
- **No order across topics.** Two topics are two orders. Order across topics by your own timestamps, or by uid,
  which sorts by time.
- **Accepted, not committed.** A publish receipt means the event was accepted for delivery, not that it was written
  to durable storage. A subscriber still misses it if it falls outside retention before the subscriber reads it, or
  if the service loses the topic's recent history.
- **Signals are best effort.** They may be dropped under load or while offline, and are never replayed.

## Acknowledgements [#acknowledgements]

When a stream or KV publish is accepted, the publisher gets a **receipt** with the event's position, its uid and
whether it was a duplicate. Signals are fire-and-forget unless an acknowledgement is requested. A receipt reports
the level reached: `accepted` today. A refused publish gets an error with a code and, for rate and quota limits, how
long to wait.

## Cursors [#cursors]

Every stream event has a position `(epoch, seq)`, written `7:42`:

- `seq` goes up by one for each stream event in the topic;
- `epoch` changes when the service may have lost the topic's recent history, for example after a topic was released
  and created again.

<Diagram name="cursors" alt="Epoch 7 holds events 7:1 to 7:12. Events 7:1 to 7:3 are purged; 7:4 to 7:12 are retained, up to the rule's byte cap, 5 MiB by default, for at most 24 hours. A client that saved cursor 7:6 subscribes after it and gets 7:7 to 7:12 replayed, then live events. A client that saved 7:2 gets Gap with reason Expired and oldest 7:4, and with on gap from oldest the replay starts at 7:4. If the epoch changes from 7 to 8, a cursor from epoch 7 gets Gap with reason EpochLost." />

**Save the cursor of the last event you processed, per topic.** It is how you resume after a reconnect or a page
reload: subscribe "after" that cursor and you get everything since, then live events. The JavaScript SDK does this
for you while the client is running, and across reloads with a [local store](/docs/js-sdk/local-storage).

When you resume, drop any event whose cursor isn't after the one you saved: after a reconnect you may see the last
event again.

## Where a subscription starts [#where-a-subscription-starts]

| Start | You get |
| --- | --- |
| Latest (default) | Only new events |
| Oldest | Everything still retained, then live |
| After a cursor | Everything after it, then live |
| Last N | The last N retained events, then live |

KV values and signals don't replay: a subscriber gets a snapshot of current values, then changes.

## Retention [#retention]

A stream keeps its events up to its rule's byte cap (5 MiB by default), and never longer than 24 hours. The oldest
events are purged first. KV values live until replaced, deleted or their time-to-live runs out; signals live a few
seconds (5 by default).

## Gaps [#gaps]

If the position you asked for can't be honoured, the subscription first reports a **gap**:

| Reason | Meaning |
| --- | --- |
| `Expired` | The events after your cursor were purged: too old, or the byte cap was reached |
| `EpochLost` | Your cursor is from an earlier epoch |

You choose what happens next: replay from the oldest retained event (the default), skip to live, or fail the
subscription. A gap means events are missing from your point of view: fetch that range from your own backend, then
carry on.

## Idempotent publishing [#idempotent-publishing]

Networks fail mid-request, so a client can't always tell whether a publish arrived. Give each stream publish a
128-bit **idempotency key**, and reuse it when you retry that publish:

- the same key and content within **10 minutes** returns the original receipt, marked duplicate, and is not
  delivered again;
- the same key with different content is refused with `IdempotencyConflict`;
- keys are scoped to the publisher and the topic; a topic remembers up to 10,000 of them.

Stream publishes require a key (`IdempotencyKeyRequired` otherwise). The JavaScript SDK generates one per publish and
reuses it on retries. A retry later than 10 minutes, or after the topic was released, may be delivered again, so make
the effects of your events idempotent too.

## Topics with several types [#topics-with-several-types]

In a topic that carries several types, only stream events are numbered, so a message reader sees no holes in the
sequence. KV values and signals in such a topic carry a separate **state version** instead, which HTTP long-polling
uses to fetch only what changed. See [`POST /v1/poll`](/docs/realtime-api/poll).
