Core Concepts

Time-Series Databases

Store timestamped measurements for recent queries, handle late samples, and keep only the detail historical reports still need.

Free~18 min

A time-series database stores measurements so they can be queried by what they measure and when the samples were taken. A dashboard may need ten minutes of traffic in one region and a month of response times across all regions. The time range affects how much data the database scans and how much detail is worth keeping.

Choose which clock the chart displays

In this extension of the URL Shortener, a collector records redirect_in_flight, the number of active redirects in a region. This count is a gauge because it can rise or fall. A sample pairs a timestamp with a value. Sample A recorded 40 active redirects in the east region at 10:00:10. Samples of the same measurement in the same region form a time series. Prometheus data model

The observation time says when the value was measured. The ingestion time says when storage received it. The database receives A first, then sample C, which was observed at 10:00:40 with value 60. Sample B measured 50 at 10:00:25 but arrives last at 10:01:05. If the storage accepts it, an observation-time query puts it between A and C in the 10:00 minute. A chart of collector delays would use both clocks instead.

A storage engine can optimize for incoming timestamps that move forward. It may use a separate write path for older samples. Decide how late a sample may be, how to handle repeats at the same timestamp, and whether to revise a summary you already published. Prometheus local storage can accept out-of-order samples within a configured window measured back from the latest timestamp in the database. For B, check the storage cutoff and the dashboard's policy for updates to closed intervals.

If the collector misses the observation entirely, leave a gap. A zero would say there were no active redirects at that moment. Prometheus TSDB configuration

Manage the number of series

Tag the measurement with region=east or region=west to keep the observations separate. In Prometheus, the metric name and the full set of labels, or name-value pairs, identify a series. An east-region chart selects the east series and reads its samples over the requested time window.

For a separate count of completed redirects, three route patterns, two regions, and two outcome classes give up to 12 series if all combinations occur. Cardinality is the number of distinct series we actually observe. Adding a unique request ID can create a new series for each request. Each series has its own index entries and storage overhead. Keep labels bounded for dashboards, and use a log or trace to locate a specific request. Prometheus metric and label naming

Track the number of active series and new series appearing over time. One rapidly sampled gauge can produce many points with little growth in series. An ID that changes constantly can create many series even at the same sample rate. Use a time-bounded query and keep the labels the dashboard uses to filter or group results.

Use blocks of time to bound the work

A storage engine can group samples into time blocks. For an east-region query from 10:00 to 11:00, it can skip blocks outside that hour and use the series index to find region=east inside the remaining blocks. Prometheus collects new samples in a mutable head block and uses a write-ahead log to recover them after a crash. It writes two-hour on-disk blocks that can merge into longer blocks later. Keeping adjacent samples together also makes compression practical. For example, Gorilla can encode how the interval between samples changes. On a regular schedule, that change is often zero. Prometheus local storage · Gorilla time-series compression

A relational database can also partition a table by timestamp. Consider a specialized engine when ingest volume, time-range scans, or recurring retention work become a large part of the system. TimescaleDB hypertables

Retention specifies how long a dataset remains available. Dropping whole expired blocks or chunks can be cheaper than deleting old rows individually. Choose block sizes and retention with the engine's rules in mind. A block containing both expired and still-needed data may have to remain until the whole block qualifies.

Keep summaries that can answer old questions

Suppose the team keeps fine-grained redirect duration observations for seven days, but wants aggregate latency for ninety. A rollup combines a fixed interval's observations into a smaller record. For an arithmetic mean, keep both the duration sum and the count. If one interval has two requests totaling 80 ms and another has six totaling 360 ms, their combined mean is 440 ms divided by eight, or 55 ms. Averaging the two interval means would give 50 ms because it would weight both intervals equally.

For historical p99, the team needs to know the latency at or below which about 99 percent of requests fall. A sum and count can only give the mean. Keep the raw values or a histogram that counts how many observations fall in each latency range. Combine counts from histograms with matching bucket bounds, then estimate p99 from the result. If the 99th percentile falls in the 100 to 200 ms bucket, those counts locate it within that range but cannot recover the exact observed value. Prometheus histograms and summaries

Allow time to incorporate late samples before the rollup becomes final. Verify the rollup before deleting the input it needs. TimescaleDB can retain a continuous aggregate after older raw chunks expire. A refresh over the deleted raw window can also delete the aggregate's old data, so coordinate the refresh window with retention. TimescaleDB retention and continuous aggregates

See also

Used in

  • URL Shortener - the actual design records click events and keeps analytics work off the redirect path. The metrics store here is an illustrative extension for time-range reporting.

Done reading? Mark it so it sticks in your dashboard.

Discussion