Process events in bulk
Run hundreds of thousands or millions of events through a worker: a simulation, a backtest, a replay of recorded data. Why the steps below matter is in Bulk input.
1. Create the worker with batching
A worker takes one event per turn by default. For bulk input, let it take up to 1024 queued events per turn:
# The most events per turn; 1024 is the largest the WorkManager accepts.
MAX_BATCH = 1024
worker_id = await workmanager.create_worker(
code_source={"content": source.encode()},
mime_type="text/x-python",
topics=[INPUT_TOPIC],
# 0 ms: take what is queued, never wait for more.
batching={"max_size": MAX_BATCH, "max_wait_ms": 0},
)
max_sizeis 2..1024 (API reference). Use 1024 for bulk input.max_wait_ms = 0adds no latency: a batch is whatever is queued when the worker's turn comes.- The worker's code does not change. Its
Processis still called once per event, in order.
2. Publish with PublishEvents, 1000 events per call
# What one PublishEvents call accepts; the gateway stores a call's events of one topic as one stream entry.
PUBLISH_BATCH = 1000
for start in range(0, len(events), PUBLISH_BATCH):
response = await api.pubsub.publish_events_async(
[(INPUT_TOPIC, event) for event in events[start:start + PUBLISH_BATCH]]
)
if response["failed_indexes"]:
raise RuntimeError(f"not published: {response['failed_indexes'][:10]}")
- Events of one topic keep the order of the call. Send one topic's events from one publisher loop when their order matters: calls of concurrent publishers interleave.
- Give every event its own CloudEvent
id. Delivery is at-least-once, and workers recognize a repeated event bysourceandid. - Publishing never waits for the worker. A publisher that is faster than the worker fills the topic's stream, not anyone's memory.
- The events of one call reach the Runtime as one response and are cut apart there without being parsed (Whole batches). Larger calls mean fewer, larger stream entries.
3. Keep the worker within the stream's retention
The worker reads its backlog from the topic's stream. The stream keeps what was published during the last pubsub.streamTTL (chart value, 1 minute by default), counted while the topic is published to.
| Run | What happens |
|---|---|
| Everything is published within the retention time. | Nothing is removed while the worker works through it, however long that takes. |
| Publishing goes on for longer than the retention time, and the worker falls further behind than that. | Paced: the publisher waits until the worker has caught up to within half the retention. Not paced: the events the worker has not read yet are removed; the worker gets a lifecycle gap event and continues with the newest events. |
So for a long run, publish paced: the publisher then waits while a consumer of the topic is behind by half the retention, and nothing is removed before the worker has read it (Paced outputs).
- A worker as the source (a replay worker) is created with
output_pacingON (workmanager 0.52.0 or later; it is off by default). - A client as the source (a notebook, the bulk benchmark) sets
paceonPublishEvents(api 0.25.0 or later). - Held too long: a paced call the gateway held for its whole wait (60 s) ends with
RESOURCE_EXHAUSTEDand publishes nothing; publish it again. A paced worker does that itself and reports a lifecyclepacedevent.
Pacing waits only for workers that report their progress: workers with input replay on that are not in a shard.
4. Check the run
- Count. Compare the events published with the worker's outputs, or with a count the worker reports.
- Gaps. Subscribe to
sys.workmanager.worker.lifecycleand look for stategapfor your worker (lifecycle events). Nogapevent means the worker got every event. - Dead letters. An event the worker failed on is on
dlq.<topic>(dead letters).
What to expect
Measured on the local stack (one machine, Redis backend, one C# source worker that publishes one output per input), 2026-11-21. The two ends of each range are two sessions on the same machine a few hours apart:
| Setup | Events/s end to end |
|---|---|
| One event per turn | 520 to 1,240 |
batching with max_size 1024 |
17,000 to 31,700 (1,000,000 events in 32 to 59 s) |
batching with max_size 1024, api 0.22.0 and workmanager 0.47.0 (measured 2026-11-22) |
58,600 to 77,900, against 52,000 to 63,700 for the previous versions in the same session |
- The absolute figures depend on the machine and on what else it runs. One event per turn is a chain of round trips between processes, so it is bound by how fast the machine wakes a waiting process, not by computing. Measure on the deployment that will run the simulation.
- The ratio holds. Batching was about 30 times faster in both sessions.
- A Python or TypeScript worker is bounded by its own code. With batching its events cross to the worker's process as one frame per batch, not one round trip per event: 100,000 events went through at about 14,000 events/s for either.
The pipeline-bulk benchmark series repeats this measurement for 10,000, 100,000 and 1,000,000 events (benchmarks). It runs only when you start it: dispatch the workmanager benchmark workflow with the input bulk, for example 10000,100000. The nightly run never includes it.