Logging at scale

Loki vs ELK vs CloudWatch Logs: ingestion pipelines, structured JSON logs, retention/cost tiers, and the queries that actually get used at 3am.

About 23 min read3-question knowledge check

The mental model

Think of logs as a paper trail in a giant office. Every worker (service) scribbles notes about what they did. When something goes wrong, an investigator (you, at 3am) has to find the three relevant notes among millions. Two things decide whether that search takes 30 seconds or 3 hours: how the notes are written (structured vs freeform) and how they are filed (indexed vs just piled up).

A logging pipeline has four stages: emit (the app writes a line), collect (an agent ships it off the box), store (a backend indexes or compresses it), query (you search it). Every logging decision — JSON vs plain text, Loki vs Elasticsearch, 7-day vs 90-day retention — is a trade-off inside one of those stages, and the currency is almost always money vs query speed.

The key mental split between the big backends:

  • Elasticsearch/OpenSearch (ELK) indexes every field of every log line. Searches are fast and flexible; storage and RAM costs are brutal at scale.
  • Grafana Loki indexes only labels (service, env, level) and stores the log body as compressed chunks. Storage is cheap (object storage like S3); queries that filter on labels then grep the body are fast enough, and full-text search across everything is slower.
  • CloudWatch Logs is the zero-ops option on AWS. You pay per GB ingested (~$0.50/GB in us-east-1 for standard class) which quietly becomes one of the biggest line items on the bill.
The full lesson continues with 15 more sections
  • Deep dive
  • Internal architecture
  • Real-world use cases
  • Best practices
  • Common mistakes
  • Performance
  • Security
  • Production troubleshooting
  • Advantages & disadvantages
  • Alternatives
  • Interview expectations
  • Interview questions
  • Facts to remember
  • Cheat sheet
  • Quick revision