Real-Time Subscriptions and Fan-Out Cost at Scale
Real-Time Change Streams
Some document databases offer change streams that push updates to subscribed clients instantly. When a document changes, all active listeners receive the update without polling. This powers collaborative apps, live dashboards, and chat systems with minimal client complexity. The trade-off is fan-out cost: each write triggers notifications to all subscribers.
Fan-Out Cost Analysis
Consider a chat room with 100,000 subscribers and 1 message per second. Each message triggers 100,000 read events (one per subscriber). In operation-billed systems at $0.06 per 100,000 reads, that is $0.06 per second or $216 per hour. Cost scales linearly: doubling subscribers or message rate doubles the bill.
Real-time is excellent for small to medium scale. At large scale, notification costs can exceed compute costs by orders of magnitude. Model costs carefully before committing to this pattern.
Mitigation Strategies
Narrow listener scope: instead of subscribing to an entire room, subscribe to the latest 50 messages or per-user channels. This reduces active listeners from 100,000 to perhaps 5,000 concurrent viewers, cutting costs by 20x.
For high-frequency updates like typing indicators, use short TTL documents (Time-To-Live, automatic expiration after a set duration). A document expires 5 seconds after creation, avoiding unbounded history and keeping churn cheap.
Back-Pressure and Rate Limits
If update rate outpaces client consumption, listeners disconnect or fall behind, triggering retry storms (many clients reconnecting simultaneously, overwhelming the server). Systems enforce per-client quotas to prevent cascading failures. Design for back-pressure: batch updates where possible, scope listeners narrowly, and test under peak load.