Design a Distributed Logging System
Understanding the Problem
🪵 What is a distributed logging system? A distributed logging system collects the log lines that services, hosts and containers write, ships them to central storage, and lets engineers search and filter them across the whole fleet. Without it, debugging means SSH-ing into dozens of machines and running
grep. The ELK stack (Elasticsearch, Logstash, Kibana), Splunk, Grafana Loki, Datadog Logs and Amazon CloudWatch Logs are all versions of this.
You'll hear this asked as "design ELK," "design a log search system for petabytes a day," "design log monitoring" or "design an audit logging service." They're one system with different emphasis. Log monitoring is alerting on the log stream. Audit logging uses the same pipeline with much stricter rules: an audit event must never be lost or altered, and it's kept for years. We'll build the general system and give audit logs their own requirement and deep dive, including how to store events whose schemas have hundreds of mostly unused columns.
Functional Requirements
Core Requirements
- Services should be able to ship logs, structured (JSON) or plain text, from any host or container.
- Users should be able to search logs by time range, service, fields and free text, and live-tail a stream as it's written.
- Users should be able to define alerts on log patterns, such as "more than 100
payment declinederrors in 5 minutes." - Compliance and security teams should be able to record audit events (who did what to which resource) that are complete, tamper-evident and retained for years.
Below the line (out of scope)
- Metrics and traces. They have their own pipelines.
- A full SIEM with threat detection rules.
- Log-based analytics dashboards beyond simple counts over time.
Non-Functional Requirements
Ask your interviewer for numbers. We'll assume a large company: 10 GB/s of raw logs at steady state, with bursts of 3x during incidents. With an average line of about 500 bytes, that's 20 million lines per second, and about 860 TB per day, close to a petabyte.
Core Requirements
- The system should ingest 10 GB/s with 3x bursts without slowing down the services that write the logs.
- Logs should be searchable within about 10-30 seconds of being written, and live-tail should show them within a couple of seconds.
- A search over the last hour of one service should return the first page of results in under 2-3 seconds. Searches over 30 days can take longer and may run in the background.
- Application logs should be kept searchable for 30 days and archived for a year. Audit logs are kept for 7 years, and an acknowledged audit event must never be lost.
- Storage should cost close to object-storage prices, not SSD prices.
Below the line (out of scope)
- Exactly-once delivery for application logs. A duplicate line during a retry is fine.
- Global ordering. Lines are ordered by timestamp within a query result, but we don't promise a total order across hosts.
Here's the math that shapes the design. Text logs compress very well, often around 10x with zstd in a columnar layout, so 860 TB a day becomes roughly 86 TB stored, and 30 days is about 2.6 PB. That's affordable in S3. A traditional full-text index is a different story: an inverted index over every token is often as large as the compressed data itself, and it lives on SSDs with replicas, so you'd be running several petabytes of fast storage just for search. At the same time, almost every query is narrow: "this service, the last hour, errors containing timeout." The design should make storage cheap and do the work at query time, on a small slice of the data.
Separate the three things logs are used for: debugging (search recent logs of one service), monitoring (count matches in real time) and compliance (keep everything, read rarely). They want different storage and latency, and a strong answer doesn't force all three through the same index.