Sebastian KaiserA personal engineering notebook

The notebook

From Time-Series to State: Designing a Cache for Periodic Device Telemetry

A table of device readings was slow during ordinary use. Someone opening it had to wait while the backend looked up the latest observations, assembled the devices, and serialized the result. The next request repeated much of that work, even when few readings had changed.

The proposed design stores each device as a hash of independently encoded metrics. An update replaces one field; a selective read decodes only the fields it needs. Assembled JSON has a separate place in the design: a short-lived local snapshot. Snapshot reuse and permission to query history are separate choices. Readers still need the age of each observation to judge how current that view is.

1 A slow view of current readings

The table needed current readings from many devices. Behind it, the backend queried a time-series store for the most recent values of each metric, combined them into device-level structures, and returned the result. Work that was manageable for one device became expensive across a larger view.

There was a reasonable explanation for every step. The observations lived in a time-series database, the table needed their latest values, and the backend assembled the response the client expected. Taken together, those steps made ordinary use painfully slow.

The incoming data offers a smaller unit of work. Each observation changes one metric, while the table asks for an assembled view of many metrics. Keeping those two representations separate lets the update path stay small without making every reader reconstruct devices from history.

2 The devices give us some room

Consider physical meters, sensors, and controllers that report measurements periodically. A table of their latest observations does not necessarily need a reaction within milliseconds. The acceptable delay depends on what the view is used for.

Each metric also arrives independently. A temperature observation may be newer than the last consumption reading. There is no single message containing the complete, current device.

The table therefore needs the latest known observation for each metric. Those observations may have different ages. Keeping their timestamps matters: a newly assembled response must not make an old reading look newly measured.

This leaves room to maintain the device's state between requests, and to reuse an assembled response briefly when the caller allows it.

3 Maintain what the table keeps asking for

The design adds a current-state projection alongside historical storage. As telemetry arrives, a consumer updates the latest metric values in a shared cache. The time-series store remains the historical source; the projection serves the recurring current-state lookup.

For each device and metric, the merge rule is small:

TextThe per-metric merge rule, shown as pseudocode.Link
if no value is stored, or incoming.timestamp > stored.timestamp:
    store incoming value and timestamp

Under that rule, an older observation arriving late cannot replace a newer stored one. Different metrics can advance independently, so an out-of-order temperature update need not prevent a consumption update from being applied.

The comparison and replacement need to happen atomically. Reading a timestamp and writing the value in separate operations would let concurrent updates overwrite one another. The timestamp must also be a meaningful ordering key; the rule cannot correct a wrongly timestamped observation.

Telemetry updates shared metric state through an event stream and per-device batching consumer. A read service can reuse local snapshots, load shared metric state, and query historical storage for missing devices when allowed. Snapshot-only requests use neither backend. Historical ingestion is omitted.
Figure 1: Conceptual update and read paths. The service can reuse local snapshots or load shared metric state. Historical queries for missing devices are separately controlled; a snapshot-only request uses neither backend. Historical ingestion is omitted.

The state is an aggregate of observations, not a measurement of the whole device at one instant. That fits the table's purpose without pretending the devices report more synchronously than they do.

4 Store metrics in the shape they arrive

One option is a serialized JSON object for each device. That resembles the response, but changing a single metric means rewriting the object. Reading a subset also means deserializing unrelated values.

The proposed layout uses one hash per device, with independently addressable metric fields [1]. Each field holds a compact byte string containing its timestamp, value, and optional unit. The cache selects the field; the application decodes what is inside it.

One device hash has independent temperature and voltage fields. The temperature payload contains an 8-byte timestamp, 2-byte value length, 4-byte UTF-8 value 21.4, 1-byte unit length, and 3-byte UTF-8 unit degrees Celsius: 18 bytes or 144 bits in total. Keys and cache overhead are excluded.
Figure 2: One device hash maps metric names to individual byte strings. The expanded field shows fixed-width timestamp and length fields followed by variable-length UTF-8 contents. The illustrative temperature payload occupies 18 bytes; cell widths are schematic, and hash keys and storage overhead are excluded.

Both text fields use the same decoding rule: read the length, then consume that many bytes. A missing unit has length zero and needs no separate parsing path. Although the decoder could infer the unit's length from the remaining buffer, the explicit length keeps it independent of that outer boundary and makes each metric payload self-delimiting, at a cost of one byte per metric. Values remain text in this representation; interpreting a reading as a number or another type is the reader's job.

The fixed fields occupy 11 bytes: 8 for the timestamp in Unix milliseconds, 2 for the value's byte length, and 1 for the unit's byte length. The payload then needs as many bytes as the UTF-8 value and unit contain. In the example, the value 21.4 takes 4 bytes and the unit °C takes 3, giving 18 bytes (144 bits) in total. That counts the metric payload, not the field name, device key, or storage engine's overhead.

A new voltage reading can then update voltage without reconstructing temperature and consumption. The service can also ask for the fields a particular view needs. Those selected fields still need decoding, but the others can remain untouched. The JSON response becomes something built from that state, rather than the unit that every update must replace.

4.1 Follow two metrics through the hash

Consider an initially empty hash for a fictional device. The timestamps below are ordered t0 < t1 < t2 < t3; arrival order differs from observation order.

Table 1: Illustrative observations and their effects on one device hash, not production data or an executed benchmark.
Arrival Metric Timestamp Reading Effect
First temperature t1 21.4 °C Store the temperature field
Next voltage t2 3.5 V Store voltage; leave temperature at t1
Late temperature t0 20.9 °C Ignore it; the stored temperature is newer
Latest temperature t3 21.8 °C Replace temperature; leave voltage at t2

A temperature-only read now selects that field and decodes 21.8 °C with timestamp t3. It does not fetch or decode voltage. A view needing both metrics can select both, retaining their different timestamps. Neither read needs to replay the arrivals to discover which values survived.

This example describes the storage layout's capabilities. A public API can choose a coarser boundary, such as returning all metrics for the selected devices; field-level storage does not require field selection in every endpoint.

5 Give updates a short window to meet

Field-level updates remove the full-object rewrite. Issuing a separate request for every incoming metric would still create substantial network overhead under load.

The consumer can briefly collect updates for each device, merge them, and write them together. The window should be small relative to the delay the application can accept. If the same metric appears more than once in the window, the newest timestamp wins there too. The stored state still needs its own atomic comparison when the batch is applied.

There is no need to wait for unrelated devices. One device's updates can be flushed while another device's window is still open. The small delay buys fewer network round trips, which is a useful trade for readings that normally arrive seconds or minutes apart.

6 A fast lookup can still make a slow response

Once the state is cheap to read, the table still needs a response containing many devices. Fetching fields from the cache does not remove the CPU work of turning them into JSON.

For repeated reads of one device, a short-lived JSON snapshot local to the read service can avoid rebuilding the same response. A request spanning many devices also needs efficient assembly: collect the selected fields into a shared buffer, reuse memory where practical, and reuse per-device snapshots when they fit the request.

This gives the two cache representations different jobs. Metric fields are convenient to update and select. Serialized snapshots are convenient to return. Using both avoids making every update pay for a response that nobody may read, or making every reader assemble a response that was just assembled.

7 How fresh does this request need to be?

A routine table read can reuse unexpired local snapshots and load the devices missing from them. Those snapshots expire after a configured lifetime. If a reader wants a newly assembled response, it can skip existing snapshots and replace them with the loaded result. Another request can skip snapshot reads and writes altogether, leaving the local cache undisturbed.

A snapshot-only read makes a different trade-off: it returns what is available locally without contacting either backend. That can mean a partial result, or an empty one when none of the requested devices are available. It does not start rebuilding the missing snapshots.

Permission to query history is a separate choice. A table refresh can still read shared metric state while avoiding the cost of historical queries for missing devices. An existing snapshot may contain data obtained by an earlier historical lookup; disabling that lookup for this request does not change where an already cached result came from.

There are two ages here. Snapshot age describes when the response was assembled. Observation age describes when the device produced a reading. A snapshot built now may contain a consumption value from several minutes ago. Expiring the snapshot does not make that measurement newer, and an old metric timestamp does not by itself trigger a historical query. The caller must judge the reading's age separately from the lifetime of its JSON representation.

For a view that can accept a short delay, snapshot reuse avoids repeated assembly between device reports. A view of both metrics might reuse a device snapshot made before t3 while that snapshot remains unexpired. Skipping it would load the current metric state instead. Whether missing devices also trigger historical queries depends on the separate lookup policy.

8 When the live state stops moving

When a requested device is missing from shared metric state, the service can query history if the request allows it. The assembled result can enter the local snapshot cache when the snapshot policy permits. A request restricted to local snapshots never reaches this path.

Without historical lookups, missing devices are omitted from the result. A failure to reach the shared cache is still an error, not proof that the devices have no readings. Keeping those cases separate prevents a backend outage from looking like a successful but empty table.

If the consumer stops, cached readings stop advancing. Catching up requires retained events or reconstruction from history, including updates that were buffered but not written before the consumer exited.

The merge rule still applies during recovery: a delayed rebuild must not replace a newer value already received from the stream. Observation timestamps help readers judge the result's age, but the service also needs to distinguish a quiet device from an ingestion path that has stopped receiving updates.

9 Where the work belongs

Historical storage remains useful for history and recovery. The proposed read path serves the usual request from maintained metric state, with short-lived local snapshots avoiding repeated assembly. Readers can control snapshot reuse without automatically enabling expensive historical lookups.

The changes follow the work that was being repeated. Per-metric state avoids reconstructing devices from history. Short, per-device batches reduce write overhead. Response snapshots avoid assembling the same JSON for every reader.

This is a useful pattern for periodic telemetry because the reporting intervals leave room for those trade-offs. Its suitability comes from what the screen needs to show, how observations arrive, and how much delay the caller can accept. Those decisions can be considered independently of the database products.

10 References

  1. Redis, Redis hashes, a public example of field-addressable storage, not a statement of the deployed technology.