01

The Advantage of In-Memory Message Storage

Keeping messages in memory allows both message puts and gets to be fast.

02

Drawbacks of Memory-Only Storage

  • Memory resources are limited, restricting the number of messages that can be stored.
  • System performance degrades under these conditions:
    • Consumers become slow
    • Messages grow too large
    • Producers send significantly more messages than usual

In such cases, either messages must be discarded or stored externally to preserve data.

03

Data Loss on Broker Failure

If messages are stored only in memory, a broker crash results in complete data loss.

04

Disk-Based Storage as a Solution

Storing messages on disk helps mitigate both memory limitations and data loss risks.

05

Examples of Messaging Systems

  • RabbitMQ and ActiveMQ: store messages both in memory and on disk.
  • Kafka: stores all messages exclusively on disk, with no in-memory storage.
06

Performance Paradox

Kafka achieves high throughput and low latency despite not using memory.

07

Design Principles for Disk-Based Systems

These techniques enable high performance using disk storage.

They are not limited to message queues but apply to databases, object storages, and file systems.

08

In-Memory Message Structure

Messages are stored using a queue data structure.

  • A queue is essentially an array.
  • New messages are added to the end.
  • Messages are read from the beginning.

This creates a sequential sequence of messages.

09

Disk-Based Message Analogy

  • Instead of an in-memory array, use a file on disk.
  • Messages are appended to the end of the file.
  • Each message has a unique identifier.
10

Message Identifier

  • The identifier can be an auto-generated Unix string.
  • For simplicity, think of it as a sequential number.
11

Message Ordering

All messages are stored in chronological order.

12

LOPOC: Append-Only, Totally Ordered Sequence

  • The sequence of messages is append-only and totally ordered.
  • This structure is known as a LOPOC.
  • Also referred to as a Datalog or Journal.
13

Application Logs vs. Datalogs

  • Application logs are human-readable text files.
  • Datalogs store messages as raw bytes for program consumption.
14

Message Appending

  • Adding messages to a log file is simple.
  • Messages are appended to the end of the file.
15

Deletion Challenge

  • Deleting a message from a large file is inefficient.
  • Scanning and removing a single message takes significant time.
16

Solution: Segment-Based Storage

  • Split the log file into chunks, called segments.
  • Use predefined segment sizes (e.g., 1GB).
17

Segment Management

  • Messages are written to the active segment.
  • When the active segment reaches its size limit, it is rolled over to a new segment.
  • Once all messages in a segment are processed, the segment is deleted.
18

Deletion Efficiency

  • Messages are deleted in chunks, not individually.
  • Segments are removed en masse when no longer needed.
19

Key Design Outcome

This approach avoids costly individual deletions and enables efficient, scalable log management.

20

Message Retrieval Efficiency

To retrieve a specific message efficiently, we need to locate it quickly in the log.

A naive approach involves iterating through log segments and messages within each segment until the target is found. This method is slow and inefficient.

A better approach requires knowing the byte position of each message's start within its log segment.

With the message's byte position known, we skip segment iteration entirely.

We simply identify the correct segment and seek directly to the message's position in the file to read its contents.

Thus, efficient message retrieval depends on maintaining a mapping from message identifier to message position in the log segment.

Every time a new message is appended to the active segment, a new record is added to this mapping.

Two key questions remain:

  • Where to store this mapping?
  • How to store this mapping?

Storing the mapping in memory is straightforward and enables fast access.

The storage structure of the mapping, however, presents multiple design options.

These options will be discussed in the next video.

Questions

  • How do we efficiently retrieve a message from a log file when we know its identifier?
  • What happens to messages once they are consumed and no longer needed?
  • Where should the mapping between message identifiers and file positions be stored?
  • How is the message-to-position mapping stored in the system?
  • Why is it inefficient to delete individual messages from a large log file?
  • Can a system achieve high throughput without storing messages in memory?

Key takeaways

  • Messages in memory are fast to access but limited by available RAM.
  • Storing messages on disk helps mitigate memory limits and data loss risks.
  • Some systems, like Kafka, store all messages on disk and avoid in-memory storage.
  • A log file can be treated as a sequence of messages appended in order—this is called a LOPOC.
  • Application logs are human-readable text; Datalogs store messages as binary data for program use.
  • To avoid long deletion times, log files are split into fixed-size segments that are rolled over and deleted as a unit.
  • Efficient message retrieval requires storing a mapping from message ID to byte position in the log segment.