Chapter 95: Logging with Loki, Elastic Stack and CloudWatch
95.5 Loki — streams, labels and cardinality
Story (fictional teaching scenario). At a fictional Kolhapur app team, Aisha and Luca from Rome first label every request separately. They then see why that makes too many tiny streams.
Why
Loki indexes labels rather than treating every line as a fully indexed document. Label design shapes query cost and performance.
What
- Loki stores logs grouped into streams and indexes their label sets.
- A label is a key/value describing origin, such as app or environment.
- A stream is one unique combination of labels.
- Cardinality is the number of distinct values/combinations.
- Structured metadata can carry high-cardinality context where supported without making it an index label; ordinary log fields are also useful.
How
-
Choose a few stable origin labels.
-
Keep unique request IDs and timestamps in content/metadata, not index labels.
-
Estimate combinations rather than counting label names alone.
Example 1: Count possible streams
- Read the command or request.
2 services ×3 environments = up to 6 label combinations
Add 1000 request IDs = up to 6000 combinations
- Read the result.
Output (illustrative; NOT executed against Loki, Elastic or CloudWatch)
One high-cardinality label can multiply the stream count.
- Line by line: The multiplication is an upper bound, not an observed Loki test. The number of actual combinations depends on which values occur together. Avoid user identifiers, unique IDs and timestamps as labels, even when they seem convenient for one search.
Ravindra Bagale's Tip
Use labels to select the source, then search event fields.
Source निवडायला labels वापरा; मग event fields शोधा.
Source चुनने के लिए labels लगाओ, फिर event fields खोजो.
Did you understand? Let us try it once more.