Append once.
The edge Worker turns a small JSON event into bytes and sends an atomic batch to K2. A successful receipt means durable acceptance, not that an order was paid or a consumer finished work.
K2 is a durable event log between producers and consumers. A checkout Worker can append events once, while analytics and audit readers independently process the same retained records at their own pace. It is not an event-time sorter or a rules engine that automatically routes event types.
Imagine one Worker accepting a checkout. Fraud review, analytics, and an archive all want the event, but they run at different speeds. K2 durably buffers the record so the producer can finish after the write is accepted; each consumer catches up on its own subscription before retention expires.
The edge Worker turns a small JSON event into bytes and sends an atomic batch to K2. A successful receipt means durable acceptance, not that an order was paid or a consumer finished work.
K2 retains accepted records independently of who reads them. This lab configures a 24-hour window; a newly created earliest subscription can replay records still inside that window.
Analytics and audit can each have their own cursor and receive every record. Slow or offline consumers do not make the producer wait, provided they recover before the retention window expires.
This checkout is an architectural example. The demo sends only synthetic records; it does not process a payment, run fraud detection, or write to a production analytics system.
The arrows below show the real producer path. Each subscription lane checks its server-side access at runtime; any records displayed below were leased from K2, never copied from the producer receipt.
Chooses a fixed synthetic event lifecycle. No customer data or arbitrary messages.
POST /api/produceAssigns stable event IDs, encodes JSON as bytes, and calls the K2 producer binding.
env.EVENTS.send(records)Accepts or rejects the whole batch. K2 stores the ordered log on R2 internally.
bdayweek2026_k2_labThe Worker producer is ready. Checking whether real subscription readers are configured…
A successful send() means K2 accepted all records atomically. The producer binding does not return offsets or a consumer acknowledgement. Our batch ID is assigned by this Worker, not by K2.
A real subscription requests a batch from K2's authenticated HTTP API, gets a five-minute lease, processes every record, and acknowledges the entire batch. A second subscription sees the same retained records; neither subscription routes only certain event types.
The new Out-of-order event times example sends three records in one batch with synthetic event_time values of 10:03, 10:01, and 10:02. K2 appends the batch as supplied; it does not inspect that field and reorder the records. A consumer that needs chronological event-time order must implement it.
K2 records append order. The event-time values are just bytes in the payload.
This right-hand view is an illustration of application logic, not K2 output.
K2 uses partitions internally. Its ordered log is not a promise of global event-time order across concurrent producers; work shared by multiple workers within one subscription can finish out of order. The test batch uses one producer, and its ordinal field is assigned by this Worker—not a K2 offset.
Every subscription receives every record after its start position. Subscription Alpha and Beta each have an independent cursor. If two workers share Alpha, K2 leases different batches to those workers so they divide Alpha's workload. Record headers such as event-type help your consumers filter or route after reading; filtering is your consumer's decision, not a K2 subscription rule. This demo has no K2-side rule that sends only checkout events to Alpha or only security events to Beta.
Pick an example, inspect its exact three-record template, then produce it through a real Worker binding. Nothing here purchases, publishes, or triggers a security rule in another app.
Loading K2 examples…
Waiting for the example catalog…
The Worker adds fresh event UUIDs, an application batch ID, and a producer timestamp when you send. Only the three predefined records can be written.
One click creates one batch of three records. Either K2 accepts them all, or this page shows an error. A non-retryable error may have an unknown outcome, so the Worker does not retry on your behalf.
No accepted batch yet.
Each lane uses its own real subscription—not a mirrored array from the producer response. If one reader acknowledges a batch, the other still has its own position. Public visitors share each subscription's cursor.
Receives every event type, not only orders. This lab validates up to 10 leased records and acks the whole batch; a real operations app would decide which types to act on.
Receives all the same event types again, on an independent cursor. Reading or acknowledging Alpha cannot drain Beta; duplicates must still be handled idempotently.
Checking whether scoped K2 Consume credentials are configured. Nothing is simulated.
Durable acceptance, consumption, and processing are three different states. This lab labels them separately.
K2 accepts every record in a batch together. It stores bytes; your application decides what they mean. A producer success is not proof any reader has consumed an event.
If a lease expires or a batch is released, K2 can deliver it again. Producer retries with uncertain outcomes can also create duplicates. Use the event ID as an idempotency key.
Each subscription receives the log independently. Acknowledging a batch advances only that subscription; the bytes stay in the stream until the one-day retention window ends.
HTTP production is disabled. Only the Worker binding can append, and this API permits small preset batches behind a rate limit. A K2 Consume token, when configured, stays in a Worker secret.
These are current K2 public-beta limits, not benchmarks measured by this small lab. Our Worker imposes tighter public-demo limits. Check current limits ↗
| Dimension | K2 beta maximum | This lab |
|---|---|---|
| Streams & storage | 20 streams per account; 10 GB across streams | One dedicated stream, with another independent field-guide stream |
| Retention | 1 hour to 30 days | 24 hours; old events expire even if unread |
| Produce rate | 30 MB/s per stream | 12 preset batch requests/minute per Cloudflare location |
| Payload | 5 MB per batch request; about 1 MB per record | Three short JSON records in each batch |
| Fan-out | 100 subscriptions per stream; 128 active leases per subscription | Two independent subscriptions, one fixed consumer ID each |
| Consumption | 10,000 records and 10 MB per response | At most 10 records per pull; a five-minute lease |
K2 uses R2 as its durable storage layer. Cloudflare handles storage replication internally; this is not a second customer-managed K2 stream you can fail over to.
Alpha and Beta are independent offsets into the same stream. Fan-out lets different readers process the same records, but it does not make a copy of the stream.
The documented K2 API does not provide a one-click stream replica. You can create a second stream and build a consumer that forwards records before acknowledging the source, but that is application-level copying with lag and possible duplicates—not automatic replication or failover. This demo does not create that mirror.
const records = events.map(event => ({
content: new TextEncoder().encode(JSON.stringify(event)),
headers: { "event-type": event.type }
}));
const result = await env.EVENTS.send(records);
if (!result.success) return failedBatch();The binding authenticates on behalf of the Worker; it receives bytes, not strings or browser tokens.
POST /subscriptions/:id/consume
→ batch_id + records + lease expiry
validate every record → process the batch
POST /subscriptions/:id/batches/:batch_id/ackThere is no read binding in this launch. Subscription HTTP requests use a server-side K2 Consume credential, never JavaScript delivered to your browser.