Course contentsShow
← Databases & SQL
Lesson 1292 of 1,95312. Key-Value and Caching EnginesFree lesson

Stream Data Structures

Utilize append-only logs for persistent event sourcing and message streaming.

In this lesson, you will learn how to leverage append-only logs to achieve persistent event sourcing and reliable message streaming within your database architecture.

The Power of the Append-Only Log

Most data structures we’ve covered so far focus on modifying state: updating a row in a table or incrementing a value in a key-value store. However, a Stream Data Structure works differently. Instead of overwriting old data, it treats every event as an immutable fact that is simply appended to the end of a log.

By using an append-only log, you create a "source of truth" that records the history of what happened, rather than just the current state of the system. If your application crashes or you need to reconstruct the state of a user’s session, you simply "replay" the stream from a specific point in time.

Persistent Event Sourcing

Think of a banking application. If you only store the "current balance," you lose the context of how that balance was reached. If you store an append-only stream of transactions, the balance becomes a derivative of the stream.

In a database context, a stream structure allows you to:

  1. Append: Add new events to the tail of the log.
  2. Read/Consume: Allow different consumers to read the stream at their own pace.
  3. Persist: Ensure that even if the server restarts, the sequence of events remains intact on disk.

For example, imagine a system tracking IoT device telemetry:

-- Conceptual representation of an append-only stream log
-- Instead of UPDATE, we append events:
APPEND INTO device_stream VALUES ('temp_sensor_01', 22.5, '2023-10-27T10:00:01Z');
APPEND INTO device_stream VALUES ('temp_sensor_01', 22.7, '2023-10-27T10:00:02Z');

By streaming this data, downstream systems—like an analytics engine or a real-time dashboard—can subscribe to these events as they arrive, decoupling the data producer (the sensor) from the data consumers. Because the log is append-only, you achieve high performance by avoiding the overhead of locking rows or managing complex indexing during the write phase.

Key Takeaway: Stream data structures use append-only logs to provide an immutable, ordered history of events, enabling reliable system recovery and real-time event-driven architectures.