Build a Weighted Traffic Splitter for Multi-Backend Log Pipelines
System DesignModel answerLast updated
Design a scalable service that takes logs from many services and splits them across storage backends by live-adjustable percentages, surviving backend outages without losing data.
Scenario
Many microservices emit logs continuously. Your job is to design a platform that receives these logs and forwards each one to one of several storage destinations, such as object storage buckets, search clusters, or local disks. The share of traffic each destination receives is controlled by operator-defined percentages that are set per source service. For example, 70% of Service A's logs go to Cluster X and 30% go to Cluster Y.
Functional requirements
- Live reconfiguration: Operators can change the percentages while the system is running, with no downtime and no restart.
- Even spread: Load is distributed so that no single destination, or node inside a destination, becomes a hotspot.
- Resilience to outages: If a destination cannot be reached, its logs are buffered or redirected to a healthy destination. No data may be lost.
Non-functional requirements
- Throughput: 100,000 events per second on each node.
- Latency: Ingestion completes in under 50 ms.
- Scalability: Capacity grows horizontally as log volume grows.
What to deliver
- A high-level architecture covering how logs are received, routed, and persisted.
- Your choice of storage for the routing percentages, with justification.
- How the system handles a slow or unavailable destination (backpressure and outages).
- The API used to manage routing rules.
State any assumptions you make (event size, delivery guarantee, ordering, and what exactly the latency target measures), and size the system with a quick back-of-envelope estimate.
Hints
Hint 1
Pin down what the 50 ms target actually covers. A durable acknowledgement to the sender is achievable; being searchable in a downstream store is not. This decides where a durable log or queue must sit in the design.
Hint 2
Separate the pipeline into stages joined by a durable, replayable buffer: receive, route, then write to each destination. Give each destination its own queue so a slow one cannot stall the others.
Hint 3
The routing config is tiny and read constantly, but must change instantly. Think about a consistent store with a push/watch mechanism, and about routers that keep serving their last known config if that store goes down.
Hint 4
At-least-once delivery means duplicates. Plan for idempotent writes using a stable event ID or deterministic object names. Also decide, per rule, whether an outage should lead to waiting (buffering) or to reweighting across healthy destinations.
Model answer
Medium Try it yourself first — then compare your approach, complexity and edge cases.
1. Clarify and size
Assumptions to state:
- Average event is about 1 KB.
- Delivery is at-least-once. Logs tolerate duplicates better than loss.
- Global ordering is not needed for weighted splitting. Per-key ordering matters only if a hash-based routing variant is requested.
- "Under 50 ms" means the time until the sender receives a durable acknowledgement. It does not mean the time until the log is searchable in a search cluster or written to object storage. Naming this explicitly is the most important clarification.
Estimate:
- 100k events/s x 1 KB is about 100 MB/s per node, close to a 1 GbE link. Use 10 GbE and compress batches (zstd typically gives 5-10x on logs).
- Ten nodes means about 1M events/s, roughly 1 GB/s raw or about 86 TB/day before compression.
- A message queue with 24 h retention and replication factor 3 needs a few hundred TB raw, or tens of TB compressed. That retention is the outage buffer, so size it on purpose.
2. Architecture
Services (agent/SDK with local disk spool)
-> L4 load balancer
-> Stateless ingestion gateways
-> Kafka "ingest" topic (acks=all) -> ack to client
-> Router workers <-watch- Config store <- Control-plane API
-> One Kafka topic per destination
-> Sink workers (circuit breaker, backoff) -> S3 / Elasticsearch / disk
-> Dead-letter topic for poison events
Ingest: the gateway authenticates, validates, assigns an event_id, produces to the durable queue, and acks. Nothing slow sits on this path, which is how the latency target is met (roughly 5-15 ms).
Route: routers consume the ingest topic, look up the rule for the event's source in an in-memory snapshot, choose a destination, and publish to that destination's topic.
Persist: sink workers batch writes (search bulk requests of roughly 5-15 MB, object-store files of roughly 64-128 MB) and commit queue offsets only after the backend confirms.
Why a topic per destination: it is a bulkhead. A slow search cluster builds lag in its own topic and never blocks the router or other destinations.
3. Weighted selection
- Store each rule as cumulative weights, e.g.
[X:70, Y:100]. Draw a number in [0,100) and binary search, or use the alias method for O(1) with many destinations. - Choose per batch (say every 500 events) instead of per event, to keep downstream batches large. The split still converges to the configured ratio.
- If short-window accuracy matters, use smooth weighted round-robin, which is deterministic and has no sampling variance.
- Avoid hotspots inside a destination by spreading across shards or rotating object-key prefixes, and give destination topics enough partitions for sink parallelism.
4. Config storage
Use etcd (or ZooKeeper if already deployed):
- Raft gives consistent writes.
- Rules are kilobytes, so size is no concern.
- The watch API pushes changes to all routers in milliseconds.
- Revisions give versioning for free.
Example rule:
/routing/rules/{id} = {
source, optional match filter,
destinations: [{id: "x", weight: 70}, {id: "y", weight: 30}],
failover_policy: "reroute" | "buffer",
version
}
Zero-downtime update: each router holds an immutable snapshot. On a watch event it builds a new one and swaps an atomic pointer. In-flight batches finish on the old version. No locks, no restarts.
Trade-offs: routers may briefly disagree (hundreds of ms) on the version. This is acceptable for log splitting and worth saying. If etcd is down, routers continue on the last known config, because it is control plane, not data plane. Optionally mirror changes to a relational DB for audit queries.
5. Backpressure and outages
Defenses from outside in:
- Agents spool to local disk and retry on
429/503honoringRetry-After. - Gateway enforces per-tenant token-bucket limits and sheds load when queue produce latency rises, rather than growing unbounded.
- Durable queue as the main buffer. A destination outage becomes consumer lag, not loss. Retention (24-72 h) defines the longest survivable outage.
- Sinks retry with exponential backoff and jitter (e.g. 100 ms up to a 30 s cap) and run a circuit breaker per destination (open on >50% errors or high p99). When open, they pause consumption and send half-open probes.
- Per-rule failover policy:
buffer: leave data in the topic until recovery. Right for compliance archives that must land in a specific place.reroute: routers read health state (published with a TTL lease) and renormalize weights across healthy destinations, so 70/30 with X down becomes 0/100. A long-open sink can also republish its backlog to siblings.
- Dead-letter topic for events that fail deterministically (e.g. mapping errors), so they do not block a partition.
Idempotency: with at-least-once delivery, make writes idempotent: use event_id as the document ID in the search store, and deterministic offset-range object keys in object storage.
6. API
Data plane: POST /v1/logs (or a gRPC stream), batched and compressed. Returns 202 {accepted: n} or 429 with Retry-After.
Control plane (authenticated, audited):
POST /v1/destinations,GET /v1/destinations/{id}/healthPOST /v1/rules,GET /v1/rules/{id}(returns version as ETag)PUT /v1/rules/{id}withIf-Matchfor optimistic concurrencyDELETE /v1/rules/{id}- Validation: weights must be non-negative and sum to 100, destinations must exist, and a dry-run endpoint can preview the effect.
7. Common mistakes
- Promising 50 ms to a searchable store instead of to a durable ack.
- Routing directly from gateway to backends with no durable buffer, so an outage means loss.
- A single shared queue for all destinations, letting one slow backend block everyone.
- Making the config store a hard runtime dependency of the data path.
- Ignoring duplicates under at-least-once semantics.
- Per-event random selection that fragments batches and hurts throughput.
8. Likely follow-ups
- Hash-based routing for per-key ordering: use consistent hashing on the key, and accept that reweighting moves some keys.
- Exact percentages over short windows: smooth weighted round-robin.
- Multi-region: local ingest and queues per region, with async replication of config.
- Gradual rollouts of weight changes: ramp weights in steps, watching destination error rates.