Redis Caching & Messaging (Pub/Sub, Streams) · Lektion

Was sind Redis Streams?

Verstehen Sie die Architektur und Vorteile von Redis Streams als leistungsfähige, persistente Nachrichtenwarteschlange.

Lektion 1 von 411 Schritte

Was sind Redis Streams? ist eine kostenlose Redis Caching & Messaging (Pub/Sub, Streams)-Lektion auf CoddyKit. Dies ist Lektion 1 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Redis Caching & Messaging (Pub/Sub, Streams)-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Redis Caching & Messaging (Pub/Sub, Streams)-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

What are Redis Streams?

Welcome! In this lesson, we'll dive into Redis Streams, a powerful data structure for managing real-time event data.

Think of a stream as an append-only log of messages, similar to a traditional message queue or a Kafka topic, but built right into Redis.

Streams are ideal for building event-driven architectures, processing sensor data, or tracking user activity.

Streams vs. Pub/Sub

Redis already has Pub/Sub, so why Streams?

  • Persistence: Stream messages are stored and not lost if consumers are offline. Pub/Sub messages are fire-and-forget.
  • Consumer Groups: Streams support consumer groups for cooperative message processing, allowing multiple applications to read the same stream without duplicating work.
  • Message History: You can read messages from any point in the stream's history.

Streams offer durability and advanced consumption patterns that Pub/Sub doesn't.

Stream Architecture: Entries & IDs

A Redis Stream is an ordered sequence of entries. Each entry is composed of:

  • A unique Stream ID: This is a timestamp-based ID (e.g., 1678886400000-0) that ensures messages are strictly ordered.
  • One or more field-value pairs: These are the actual data for the message, similar to a small Redis Hash.

Think of it as a log where each line has a unique timestamp and some key-value data.

Adding Entries with XADD

Producers add new entries to a stream using the XADD command. Redis automatically generates a unique ID for each new entry, or you can provide one.

The * wildcard tells Redis to generate a new, unique ID for the entry.

Try running this example:

public class Main {
  public static void main(String[] args) {
    System.out.println("Simulating Redis XADD command:");
    String streamName = "sensor_data";
    
    // XADD sensor_data * temp 25 hum 60
    System.out.println("redisClient.xadd(");
    System.out.println("  \"" + streamName + "\", \"*\",");
    System.out.println("  \"temp\", \"25\", \"hum\", \"60\"");
    System.out.println(");");
    System.out.println("\nOutput (example ID): 1678886400000-0");
  }
}

Understanding Stream IDs

Stream IDs have two parts: <milliseconds-timestamp>-<sequence-number>.

  • The timestamp is the time the entry was added (in Unix milliseconds).
  • The sequence number is incremented if multiple entries are added within the same millisecond.

This structure guarantees that IDs are always increasing, providing a natural order for messages. You can use 0-0 to refer to the very beginning of a stream.

Reading from a Stream with XREAD

Consumers use the XREAD command to retrieve entries from one or more streams. You specify the stream name and the ID from which to start reading.

XREAD allows you to fetch a batch of messages, making it efficient for consumers.

Try running this example:

public class Main {
  public static void main(String[] args) {
    System.out.println("Simulating Redis XREAD command:");
    String streamName = "sensor_data";
    String startId = "0-0"; // Read from the beginning
    
    // XREAD STREAMS sensor_data 0-0
    System.out.println("List<Map<String, String>> entries =");
    System.out.println("  redisClient.xread("STREAMS",");
    System.out.println("  \"" + streamName + "\", \"" + startId + "\"");
    System.out.println(");");
    
    System.out.println("\nExample Output:");
    System.out.println("ID: 1678886400000-0, {temp=25, hum=60}");
    System.out.println("ID: 1678886400001-0, {temp=26, hum=61}");
  }
}

Blocking Reads for Real-time

To read new messages as they arrive, you can use the BLOCK option with XREAD. This makes the command wait for new data if no new entries are available.

You can also specify a timeout for the blocking operation. This is crucial for building real-time applications that react instantly to events.

Example: XREAD BLOCK 0 STREAMS mystream $ ($ means 'last ID in stream').

Stream Persistence & History

Unlike Pub/Sub, Redis Streams are persistent. Messages added to a stream remain there until explicitly removed or trimmed.

This allows new consumers to read the entire history of events from the beginning, or catch up from a specific point if they were temporarily offline.

This persistence is a key feature for reliable event logging and replay capabilities.

Ordered & Immutable Entries

Redis Streams guarantee that entries are always added in the order they are received and assigned strictly incrementing IDs.

Once an entry is added to a stream, it cannot be modified. This immutability is fundamental for maintaining an accurate, auditable log of events.

These properties make Streams excellent for event sourcing and ensuring data consistency.

Check Your Understanding

Which of the following are key benefits or characteristics of Redis Streams?

Recap: What are Redis Streams?

In this lesson, we learned that Redis Streams are a powerful, persistent, append-only log data structure.

  • They offer persistence, ordered delivery, and support for consumer groups, differentiating them from Pub/Sub.
  • We explored how to add entries with XADD and read them with XREAD.
  • Streams are ideal for event sourcing, real-time data processing, and building reliable message queues.

Next, we'll dive deeper into Stream commands and features!

Kostenlos starten

Lerne Redis Caching & Messaging (Pub/Sub, Streams) mit einem KI-Tutor — kostenlos

Schreibe und führe echten Code in deinem Browser aus, bekomme sofortige Hilfe von einem 24/7 KI-Tutor und setze dein Lernen im Web oder in der App fort.

Kurse
12
Lektionen
48

Häufig gestellte Fragen

Ist die Lektion „Was sind Redis Streams?“ kostenlos?

Ja — der vollständige Text von „Was sind Redis Streams?“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Redis Caching & Messaging (Pub/Sub, Streams)-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Redis Caching & Messaging (Pub/Sub, Streams)-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „Was sind Redis Streams?“?

Verstehen Sie die Architektur und Vorteile von Redis Streams als leistungsfähige, persistente Nachrichtenwarteschlange. Du übst Redis Caching & Messaging (Pub/Sub, Streams) mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um Redis Caching & Messaging (Pub/Sub, Streams) zu starten?

Keine Vorkenntnisse erforderlich. Redis Caching & Messaging (Pub/Sub, Streams) auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 1 von 4.

Wie lange dauert die Lektion „Was sind Redis Streams?“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser Redis Caching & Messaging (Pub/Sub, Streams)-Lektion Code schreiben und ausführen?

Ja. Jede Redis Caching & Messaging (Pub/Sub, Streams)-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. Was sind Redis Streams?
  2. Datenstrukturen und Befehle für Streams
  3. Persistenz von Stream-Nachrichten
  4. Streams begrenzen und kürzen
← Zurück zu Redis Caching & Messaging (Pub/Sub, Streams)