K2THE EVENT
STREAM LAB
Field guide ↗
CLOUDFLARE BIRTHDAY WEEK 2026 / K2 PUBLIC BETA

Write now.
Read later.
Replay again.

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.

LIVE SYSTEM MAP01 / PRODUCE
WORKERK2
K2
NOT A QUEUE · NOT A PIPELINE · NOT A SIMULATED LOGSCROLL TO EXPLORE ↓
01ATOMIC PRODUCER BATCH
24HTHIS STREAM'S RETENTION
02CHECKING READER STATUS
REALWORKER-TO-K2 WRITES
01 / WHY K2 EXISTS

A sale should not wait for every downstream system.

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 PRODUCER

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.

THE STREAM

Keep a replayable log.

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.

THE CONSUMERS

Read at your pace.

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.

02 / FAN-OUT ARCHITECTURE

One stream. Two independent timelines.

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.

01 / INPUT

Your browser

Chooses a fixed synthetic event lifecycle. No customer data or arbitrary messages.

POST /api/produce
02 / ENCODE

Edge Worker

Assigns stable event IDs, encodes JSON as bytes, and calls the K2 producer binding.

env.EVENTS.send(records)
03 / APPEND

K2 durable log

Accepts or rejects the whole batch. K2 stores the ordered log on R2 internally.

bdayweek2026_k2_lab
CHECKING INDEPENDENT CURSORS
04A / SUBSCRIPTION ALPHALease → validate → ackGets every record; cursor A advances only when A acks.
04B / SUBSCRIPTION BETALease → validate → ackGets every record again; cursor B is independent.

The Worker producer is ready. Checking whether real subscription readers are configured…

THE PRODUCER'S ANSWER

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.

THE CONSUMER'S ANSWER

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.

03 / ORDERING IS NOT SORTING

Arrival order, not event-time order.

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.

WHAT THE PRODUCER APPENDS / WHAT TO READ BACK
  1. 01 event_time 10:03
  2. 02 event_time 10:01
  3. 03 event_time 10:02

K2 records append order. The event-time values are just bytes in the payload.

IF YOUR APPLICATION SORTS BY EVENT_TIME
  1. 01 event_time 10:01
  2. 02 event_time 10:02
  3. 03 event_time 10:03

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.

ROUTING TO SUBSCRIBERS

Fan-out by subscription. Split work within one.

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.

04 / INTERACTIVE WORKBENCH

Put bytes on the stream.

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.

01 / DESIGN A BATCHFIXED SAFE EXAMPLES

Choose a lifecycle.

Loading K2 examples…

THE PAYLOAD SHAPETEMPLATE / BEFORE SENDING
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.

02 / WRITE AT THE EDGECONNECTING

An atomic commit to K2.

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 batch has been sent yet. Inspect the template at left, then append it.
Inspect the actual accepted JSON records
No accepted batch yet.
04B / THE READ SIDE

Same events. Separate cursors.

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.

SUBSCRIPTION / ALPHACHECKING

Operations reader

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.

Waiting for a real subscription.
SUBSCRIPTION / BETACHECKING

Audit reader

Receives all the same event types again, on an independent cursor. Reading or acknowledging Alpha cannot drain Beta; duplicates must still be handled idempotently.

Waiting for a real subscription.

Checking whether scoped K2 Consume credentials are configured. Nothing is simulated.

05 / DESIGN CONSTRAINTS

What the receipt does not tell you.

Durable acceptance, consumption, and processing are three different states. This lab labels them separately.

01 / ATOMIC BATCHES

All or nothing.

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.

02 / AT LEAST ONCE

Duplicates can happen.

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.

03 / INDEPENDENT CURSORS

Read without deleting.

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.

04 / SECURITY BOUNDARY

No public stream endpoint.

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.

06 / LIMITS AND REPLICATION

What scales, and what does not happen automatically.

These are current K2 public-beta limits, not benchmarks measured by this small lab. Our Worker imposes tighter public-demo limits. Check current limits ↗

K2 beta limits compared with this demo
DimensionK2 beta maximumThis lab
Streams & storage20 streams per account; 10 GB across streamsOne dedicated stream, with another independent field-guide stream
Retention1 hour to 30 days24 hours; old events expire even if unread
Produce rate30 MB/s per stream12 preset batch requests/minute per Cloudflare location
Payload5 MB per batch request; about 1 MB per recordThree short JSON records in each batch
Fan-out100 subscriptions per stream; 128 active leases per subscriptionTwo independent subscriptions, one fixed consumer ID each
Consumption10,000 records and 10 MB per responseAt most 10 records per pull; a five-minute lease
REPLICATION IS NOT FAN-OUT

Can you replicate a K2 instance?

STORAGE DURABILITY

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.

SUBSCRIPTIONS

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.

ANOTHER 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.

07 / UNDER THE HOOD

Readable primitives, real boundaries.

PRODUCER / RUNNING NOW
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.

CONSUMER / SERVER-SIDE SCOPED CREDENTIAL
POST /subscriptions/:id/consume
→ batch_id + records + lease expiry

validate every record → process the batch
POST /subscriptions/:id/batches/:batch_id/ack

There is no read binding in this launch. Subscription HTTP requests use a server-side K2 Consume credential, never JavaScript delivered to your browser.