- Category
- Operations
- Reading time
- 9 minutes
- Published
How to scale Ethereum WebSocket subscriptions across workers
A small event consumer can keep one WebSocket open and act on each new head notification. At higher concurrency, opening a new subscription for every job or application replica multiplies connections and duplicate work. Sharing one socket without a recovery plan creates a different problem: one slow consumer can hold up everyone behind it.
The useful question is not how many sockets fit in a pool. It is who owns each subscription, how notifications reach workers, and how every worker recovers when a connection or process disappears. This guide gives a practical design for head-driven EVM workloads. Subscription support and limits vary by endpoint, so qualify the exact chain and service before rollout.
What does connection pooling mean for eth_subscribe?
An eth_subscribe result is a subscription ID tied to the connection that created it. Closing that connection removes its subscriptions. Notifications cover current events, not a replayable history. Ethereum's real-time event documentation describes these connection and delivery rules.
This differs from reusing idle HTTP connections. A WebSocket subscriber is a long-lived owner of state. A pool of interchangeable sockets cannot move a live subscription ID from one socket to another. If a socket is replaced, create a new subscription and reconcile the missed block interval from an application-owned cursor.
For newHeads, begin with one subscription owner per chain and feed its observed heads to workers. Add independent owners only when a measured throughput or isolation need justifies them. Do not assume that more sockets make block production or downstream commits faster.
Should every worker subscribe, or should one reader fan out?
If ten workers all subscribe to the same heads, each can receive the same signal and independently fetch the same block. That may be acceptable for isolated applications, but within one indexer it usually duplicates reads and makes ownership unclear. A single reader can publish block identities to a bounded internal queue, while workers partition the actual block or filter work.
Keep the reader's job small: validate the notification envelope, record the observed head number and hash, and enqueue a wake-up signal. Workers then claim explicit block ranges or filter partitions. Assign each range to one active worker at a time, and make writes idempotent so a reassigned range is safe to replay. If consumers need different durability or security boundaries, give each its own cursor and delivery contract rather than assuming one shared in-memory queue is durable.
A central reader is also a failure domain. Run a replacement strategy that can elect a new owner, create a fresh subscription, and restart range reconciliation. Multiple readers for availability may produce duplicate signals. Treat those signals as hints, then deduplicate work by chain and block identity.
How do you close the gap when a subscriber restarts?
Store a durable cursor containing at least chain ID, last committed block number, and block hash. The socket's subscription ID is not that cursor. On startup, open the socket and subscribe first, then read a current catch-up target. Buffer incoming heads while a range worker fills the interval from the durable cursor through that target. Reconcile the buffered heads and continue live processing only after the committed range reaches them.
Use explicit block or log reads for catch-up. The Execution API `eth_getLogs` method accepts a bounded block range and, where supported, a blockHash filter for one block. EIP-234 explains why a hash-bound log read helps when a disconnect and reorganization overlap. Commit derived records and cursor movement atomically.
If the connection closes, a queue overflows, or the consumer cannot prove that every block was committed, mark the live feed behind and repeat this procedure. A reconnect is transport recovery. It does not by itself establish data completeness.
How should the queue handle bursts and slow consumers?
Set a finite queue capacity and track its depth, oldest item age, and the distance from the observed head to the committed cursor. A fast socket reader should not wait for decoding, database writes, or a slow downstream API. Give those tasks separate workers and concurrency limits.
When the queue fills, do not silently discard a head and keep calling the stream current. Stop admitting live work, preserve the last durable cursor, and reconcile the missing range through explicit reads. Head notifications are useful wake-up signals, but they are not a complete per-block inventory. The real-time event documentation notes that a client catching up can emit only the last of several new heads and that a slow receiver can lose its connection.
Bound recovery traffic separately from live traffic. Otherwise a burst of backfill requests can delay the very reader that tells the system it is falling behind. Measure completed canonical blocks per minute and oldest uncommitted block, not just notifications per second.
How do you prevent duplicates and handle reorganizations?
A block number alone does not identify a canonical block. Keep the hash and parent hash for each processed block, and compare the next parent hash with the last committed hash. A mismatch means the application must find a common ancestor, reverse orphaned records, and replay the replacement chain. The same height can appear again with a different hash.
For logs, retain chain ID, block hash, transaction hash, and log index with the raw event. A logs subscription may send removals during a reorganization, but a disconnected subscriber can miss those removals. Hash continuity and range reconciliation are still required. Make worker retries and owner changes idempotent, since both can deliver the same block more than once.
Choose a finality boundary that fits the product. Waiting for a stronger boundary can reduce rollback work but increases freshness delay. The block-tag guide explains the trade-off, while the log backfill guide covers cursor and reorganization mechanics in detail.
What should you measure before increasing connections?
Test the exact chain and supported subscription type with the intended endpoint. Record concurrent connections, subscriptions per connection, notifications per second, bytes per second, reconnects, queue depth, cursor lag, range-read rate, and time to become current after a forced disconnect. Confirm documented limits and billing for connections, notifications, and catch-up calls rather than assuming a pushed head is free or unlimited.
Increase load in controlled steps. First measure one reader and its downstream workers. Then test a busy block period, a slow database, a process restart, and simultaneous reconnects across replicas. If the reader itself is saturated, shard by chain or clearly defined workload with a separate durable cursor per shard. If downstream work is saturated, more sockets only multiply the input pressure.
Use WebSocket ping and pong or a client-supported liveness check to detect a dead transport. RFC 6455 defines ping frames, but a pong does not prove the chain head is advancing. Monitor transport liveness and chain progress separately.
A production subscription scaling checklist
- Verify the exact chain, subscription type, connection policy, and catch-up method.
- Assign one explicit subscription owner per chain or measured shard.
- Keep each worker's committed block number and hash in durable storage.
- Fan out bounded work items rather than sharing a socket's subscription ID.
- Subscribe before closing the historical-to-live gap, and buffer heads during catch-up.
- Commit application rows and cursor advancement together.
- Treat duplicate heads, queue overflow, reconnects, and reorganization as normal recovery paths.
- Bound live work, catch-up reads, retries, and reconnect concurrency independently.
- Load-test a slow consumer and a multi-replica restart, then verify the final block inventory.
The broader WebSocket versus HTTP polling guide helps decide whether push delivery is worth operating at all. If block-aware polling meets the product's delay target, it can remove the connection-ownership problem entirely.