← Back to System Design, page 3

system-design-0048 · Medium mid

Design a centralised logging system that collects, stores and searches logs from 5,000 servers.

Run an agent on each server that tails logs, adds metadata and ships batches to a durable buffer such as Kafka; stream processors parse and enrich them, then write recent logs to a search index for fast queries and all logs in compressed form to object storage for long retention. At 500,000 lines per second of 300 bytes, raw volume is about 150 MB/s or 13 TB per day, so retention tiers and sampling decide the cost.

Full answer

Requirements

Functional:

  • Collect application and system logs from every server and container.
  • Search by time range, service, host, level, request ID and free text; live tail.
  • Alerts on patterns (error rate spikes); retention of 7 days hot and 90 days cold.

Non-functional:

  • Logging must never slow down or crash the applications.
  • Ingestion survives spikes and backend outages without losing data (or with clearly bounded loss).
  • Searches over recent data return in seconds.

Capacity estimates

Assumptions: 5,000 servers; 100 lines per second each on average; 300 bytes per line; compression ratio about 10x for cold storage; hot index size roughly equal to raw size per replica, with 2 replicas.

  • Lines: 5,000 x 100 = 500,000 per second.
  • Raw bytes: 500,000 x 300 B = 150 MB/s; per day 150 MB/s x 86,400 = about 12.96 TB.
  • Hot index: 12.96 TB x 7 days x 2 replicas = about 181 TB.
  • Cold archive: about 1.3 TB per day compressed; 90 days = about 117 TB in object storage.
  • Spikes: an incident can multiply error logs 5 to 10x; the buffer must hold that for hours.

181 TB of hot index is the expensive part, which is why many teams index fewer fields or use a label-indexed store such as Grafana Loki that keeps logs compressed and only indexes labels.

API

Ingestion (agent to collector): batched, compressed, over HTTP or gRPC.

POST /v1/logs:batch (gzip) [{"ts":"2026-12-04T10:15:02.118Z","service":"payments","host":"pay-17","level":"ERROR", "trace_id":"7f3a...","msg":"provider timeout","attrs":{"provider":"x","latency_ms":3021}}]

Query:

GET /v1/search?q=level:ERROR AND service:payments AND msg:"timeout"&from=...&to=...&limit=200 GET /v1/tail?service=payments (server-sent events)

Data model

  • Structured JSON log events with required fields (ts, service, env, host, level, trace_id, msg) and free-form attrs.
  • Hot store: one index per day per environment (time-based indices make deletion cheap: drop the whole index).
  • Cold store: compressed files partitioned by date/service/hour in object storage, readable by batch query engines.

High-level design

mermaid
flowchart LR
  APP[App writes stdout / file] --> AG[Agent on host: Fluent Bit / Vector]
  AG --> K[Kafka: logs topic, partitioned]
  K --> PR[Stream processors: parse, enrich, redact]
  PR --> ES[(Search index: 7 days)]
  PR --> OS[(Object storage: 90 days)]
  PR --> AL[Alerting rules]
  UI[Log UI / query API] --> ES
  UI --> OS

Key design points

  • Agents write to a local disk buffer and send asynchronously in batches. If the pipeline is down, they buffer up to a limit, then drop the lowest-priority logs (debug first) rather than block the application.
  • Kafka decouples producers from the indexers. With replication factor 3 and producer compression, it holds a few hours to a day of logs so an index outage does not lose data.
  • Processing parses unstructured lines, adds Kubernetes or host metadata, and redacts secrets and personal data (card numbers, passwords, emails) before anything is stored.
  • Indexing writes in bulk; time-based indices roll over daily; old indices are deleted or moved to cheaper nodes.
  • Correlation: a trace_id in every log line links logs to distributed traces.

Bottlenecks and trade-offs

  • Cost vs searchability: full-text indexing of everything is fast to query but expensive; label-only indexing is cheap but slower for free-text search. Many teams index errors and key services fully and archive the rest.
  • Sampling: keep all errors, sample a percentage of debug and info logs from high-volume services.
  • Noisy neighbours: one service logging in a loop can flood the pipeline; enforce per-service quotas at the agent or collector.
  • Ordering: logs from different hosts arrive out of order; sort by event timestamp at query time and accept clock skew of a few milliseconds.
  • Reliability: logs are best-effort compared with business data; state the acceptable loss under extreme overload (for example, debug logs may be dropped) rather than promising zero loss at any cost.

By level

Fresher: Describe agents collecting logs from servers, a central store, and searching by time and service. A fresher should know that logging must not slow the application.

Mid: Estimate volume and storage, add a Kafka buffer, design time-based indices, retention and structured log format. Expect volume math in MB per second and TB per day.

Senior: Optimise cost with tiering, sampling and label indexing, enforce quotas, handle incident spikes, ensure privacy and compliance, and integrate with traces and metrics. The senior answer cuts cost without losing error visibility.

Key points

  • Agent with local buffer, then Kafka, then processors, then hot index and cold object storage
  • 500k lines per second of 300 B is 150 MB/s, about 13 TB per day
  • Hot index size dominates cost; tier, sample and index selectively
  • Redact secrets and personal data before storage
  • Logging must never block the application

Common mistakes

  • Sending logs synchronously from the request path
  • Indexing every field of every log for 90 days
  • Storing passwords or personal data in logs
  • No per-service quotas, so one noisy service floods the pipeline

Follow-ups

  • How would you find all logs for one failed checkout across 12 services?
  • How would you cut cost by half without losing error visibility?
  • How do you handle a 10x log spike during an incident?

Tips

  • Size the pipeline in MB/s and TB per day first
  • Separate hot and cold paths
  • Mention redaction and trace IDs

Evaluation rubric

  • Requirements include safety for applications and retention
  • Correct volume and storage math
  • Buffered, decoupled pipeline with hot and cold tiers
  • Cost, sampling, quotas and reliability trade-offs