Log pipelines and retention
Ship, buffer, index and expire logs so they are searchable when needed and affordable the rest of the time.
16 min readAnatomy of a log pipeline
Logs flow through four stages. Agents on each host or pod tail files or stdout, add metadata such as host, pod and service, and batch lines. A durable stream such as Kafka absorbs bursts and decouples producers from the indexer, so a slow search cluster never blocks applications. Processors parse, redact and route lines. Storage tiers keep recent logs searchable and older logs cheap. Without the stream, a spike of errors, which is exactly when logs matter most, overloads the indexer and loses data.
Structure and hygiene
Free-text logs are cheap to write and expensive to search. Finding every failed payment for one tenant in free text means a regular expression over terabytes; in structured logs it is a filter on two indexed fields. Structured logs make every field queryable and cost less to index, because the processor does not have to guess the format with fragile parsing rules. The pipeline is also where volume is controlled, and volume is what you pay for. A few rules applied consistently across every service make the difference between a useful platform and an expensive one.
- Emit JSON with standard fields: timestamp, level, service, version, trace_id, tenant_id, message
- Redact secrets and personal data at the source and again in the processor; never rely on one layer
- Rate-limit per service so one runaway loop cannot flood the pipeline
- Drop or sample DEBUG logs in production; they are often 70% of volume and rarely read
Hot, warm and cold storage
Most log queries hit the last 24 to 72 hours. A full-text index on fast SSDs is the right home for that window but costs roughly ten times more per GB than object storage. A common layout keeps 7 days hot and indexed, 30 days warm on cheaper disks with fewer replicas, and 1 year or more cold in compressed object storage, queried by scanning when an audit or investigation needs it. Logs compress well, often 10:1, so 1 TB per day becomes about 100 GB per day in the cold tier.
Retention is a policy decision
Retention balances three forces: debugging value, cost and legal obligations. Health and financial data may require years of audit logs, while privacy law requires deleting personal data you no longer need. Set retention per log class, not globally: audit logs for seven years in write-once storage, request logs for 30 days, debug logs for 3. Charge each team for its volume so the cost is visible to the people who can reduce it. Make deletion automatic through index lifecycle policies and bucket rules; manual clean-up never happens.
Key takeaways
- Put a durable stream between agents and the indexer to survive bursts
- Structured logs with a trace ID are cheaper to index and easier to join
- Tier storage: hot for days, cold object storage for months
- Retention is set per log class by debugging value, cost and law
Sign in to track lessons and earn badges.