Triaging an Outage from Giant Logs, Then Building Log Search
System DesignModel answerLast updated
Find the cause of a production failure using only SSH and huge log files, then design a centralized logging platform and justify the storage and cost trade-offs.
A service in production has started failing. All you have is an SSH session to one affected host, and the log files on it are tens of gigabytes.
Part 1: Single-host triage
Describe how you would find the relevant errors and the time window in which things went wrong. Be specific about:
- the commands, filters and tools you would use,
- how you would avoid making a struggling host worse,
- how you would turn raw log lines into a conclusion (when it started, what the dominant error is, what changed).
Part 2: Centralized logging
Suppose this kind of incident keeps happening. Design a centralized logging and search system that would make this triage fast and routine. Cover:
- requirements and a rough scale estimate,
- the ingestion path and where data is buffered,
- storage tiers, indexing and retention,
- the query and alerting experience,
- failure modes and the trade-offs you would weigh (cost, latency, completeness, privacy).
Aim for a structured answer: state your assumptions, justify each choice, and say how you would verify that the fix or design works.
Hints
Hint 1
Before searching, check whether the host itself is sick (disk, inodes, memory, OOM kills, recent restarts), and look at the end of the file first because the newest events are there.
Hint 2
Narrow by time window before searching by content, then aggregate (count per minute, group similar messages after stripping IDs) instead of reading lines. Use correlation IDs to follow one failing request.
Hint 3
For the design, size the volume first (hosts x GB/day), then decide what must be fully indexed versus cheaply archived. Put a durable buffer between agents and the index, and plan for bursts and PII.
Model answer
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Part 1: Triage on one host
Frame the problem first. Establish when it started (alert or deploy time), what the symptom is (5xx, latency, crash, stuck queue), which logs exist (application, access, journal, kernel), and whether the host is healthy enough to run searches on.
1. Check the host before the logs. Many "application" failures are really machine failures.
uptime; free -h; df -h; df -i
dmesg -T | tail -50
systemctl status myservice
journalctl -u myservice --since "30 min ago" -p err
ls -lhS /var/log/myservice/
A full disk, exhausted inodes or an OOM kill can explain everything in under a minute.
2. Do not make it worse. Run scans with nice -n 19 ionice -c3, stream with grep file instead of cat file | grep, never open multi-GB files in an editor, and if the analysis will be long, copy a slice off the host.
3. Start from the tail. Recent events are at the end.
tail -n 5000 app.log | grep -iE 'error|exception|fatal|timeout'
tail -F app.log | grep --line-buffered -i error
tac app.log | grep -m1 -n ERROR
4. Bound the time window before searching content. With ISO-8601 timestamps, string order equals time order.
LC_ALL=C awk '$1 >= "2024-05-01T10:00" && $1 <= "2024-05-01T10:30"' app.log > window.log
If the file is sorted by timestamp, bisect by byte offset (tail -c +N | head -1) or use look. That is O(log n) seeks instead of a full scan. For rotated files, pick archives by mtime (ls -l --time-style=full-iso) and read them with zgrep/zcat.
5. Search efficiently. Use LC_ALL=C grep -F for fixed strings, or rg if available. Get counts first (-c), cap output (-m), and use -B5 -A20 to capture stack traces.
6. Aggregate instead of reading.
# onset: errors per minute
grep ERROR window.log | cut -c1-16 | sort | uniq -c
# dominant error classes: normalize IDs and numbers
grep ERROR window.log | sed -E 's/[0-9a-f]{8,}|[0-9]+/N/g' | cut -d' ' -f3- \
| sort | uniq -c | sort -rn | head -20
# access logs: status mix and worst endpoints
awk '{print $9}' access.log | sort | uniq -c | sort -rn
awk '$9>=500 {print $7}' access.log | sort | uniq -c | sort -rn | head
7. Correlate. Take one failing request ID and grep it across all logs on the host to see its path. Line up the onset time with deploys, config changes, dependency errors (DB timeouts, DNS, TLS) and resource graphs.
8. Mitigate, then root-cause. Roll back, restart or fail over first. Afterwards record what slowed you down (no request IDs, unstructured logs, no central search). Those gaps motivate Part 2.
Part 2: Centralized logging
Requirements
- Functional: ingest from all hosts, search by text and fields (time, service, host, level, trace ID), live tail, alerting, retention.
- Non-functional: producers never blocked by the pipeline, logs searchable in about 10 s, recent-day queries in a few seconds, no silent loss of error-level logs, PII controls.
Scale estimate (assumptions)
| Quantity | Value |
|---|---|
| Hosts | 3,000 |
| Per host per day | ~5 GB |
| Raw per day | ~15 TB |
| Average ingest | ~175 MB/s |
| Peak (about 3x) | ~500 MB/s |
| Event size | ~500 B |
| Event rate | ~350k/s average, ~1M/s peak |
A full-text index with one replica costs roughly 1 to 1.5x raw size, so 7 days hot is about 100 to 150 TB. That cost drives the main trade-off.
Data model
Structured JSON events: timestamp (event time plus ingest time), service, env, host, level, trace_id, request_id, message, and free-form attrs. Partition by time and service because nearly every query is time-bounded, so most data can be skipped.
Architecture
- Agents (Fluent Bit or Vector) on each host tail files and the journal, parse, add metadata, batch, compress, and buffer to a size-bounded local disk queue so an outage downstream neither loses data nor fills the disk.
- Kafka as a durable buffer: decouples producers from indexers, absorbs incident bursts, and enables replay after an indexer bug. Partition by service or host.
- Stream processing: parse and normalize, redact PII, route (errors to hot storage, debug sampled), and derive metrics such as error counts per service for alerting.
- Storage tiers
- Hot (7 to 14 days): Elasticsearch/OpenSearch for rich full-text search.
- Cold (months to a year): compressed Parquet or chunks in object storage, queried with Trino/Athena or rehydrated on demand.
- Query service and UI: fans out across time-partitioned shards, enforces limits and per-user quotas, supports cursor pagination and live tail (WebSocket/SSE).
- Alerting: rules on extracted metrics or saved queries, with thresholds, windows and routing to paging.
API sketch
GET /search?q=&service=&start=&end=&limit=&cursor=GET /tail?service=&filter=POST /alertswith query, threshold, window and target.
Key trade-offs
- Full-text index vs label index. Elasticsearch gives flexible search but is expensive in storage and CPU. Loki indexes only labels and greps compressed chunks: much cheaper, slower for needle-in-haystack queries. ClickHouse sits between, with strong aggregations and good compression.
- Retention vs cost. Short hot retention plus cheap cold storage covers most debugging while meeting compliance.
- Sampling vs completeness. Sample debug and access logs, never errors. Prefer head-based sampling only where loss is acceptable.
- Freshness vs efficiency. Larger batches and bulk indexing cut cost but add latency; target a few seconds.
- Delivery semantics. At-least-once delivery with idempotent event IDs is simpler and safer than exactly-once.
- Multi-tenancy and privacy. Redact at the pipeline, apply field-level access control, and keep audit logs of who searched what.
Failure modes
- Index cluster down: Kafka retains data, then replay after recovery.
- Agent cannot reach the pipeline: local bounded buffer; drop low-priority levels first when full.
- Hot shard overload from a chatty service: per-service rate limits and quotas.
- Clock skew: index both event and ingest time, and search on a tolerance.
- Log storms during incidents: backpressure and priority lanes so errors survive.
Validation
Run synthetic canary log lines end to end and alert on ingest-to-searchable lag. Monitor drop counters at agent, Kafka and indexer. Load-test at 3x peak, and run game days that kill an indexer node.
Common mistakes
- Running
grepon a 50 GB file without bounding the time window, or on a host already out of resources. - Reading lines instead of aggregating them.
- Proposing Elasticsearch for everything without costing storage.
- Having no buffer between agents and the index, so bursts drop data.
- Forgetting PII, retention rules and request IDs for correlation.