Lesson 1
Redis Streams with Jedis
Introduction to Redis Streams

Welcome back! In this lesson, we explore Redis Streams, a powerful data structure for processing real-time data efficiently. Streams are ideal for use cases like:

  • Chat Applications: Handling messages in real-time.
  • Monitoring Systems: Processing logs and events.
  • User Activity Tracking: Tracking user actions as they happen.

By the end of this lesson, you’ll know how to add entries to a Redis Stream and read from it using Jedis in Java.

Using Redis Streams

Redis Streams are an append-only log of events where each entry is uniquely identified and stores data as key-value pairs. They are highly versatile for real-time data processing.

Key characteristics of Redis Streams include:

  • Append-Only: New entries are always added at the end of the stream.
  • Unique Identifiers: Each entry has a unique ID, which can be generated automatically or manually specified.
  • Field-Value Pairs: Entries store data as key-value pairs, offering flexibility in structure.
  • Efficient Reads and Writes: Streams support high-performance operations, even with large data volumes.

These features make Redis Streams ideal for building scalable, real-time systems like message queues, event logs, and analytics pipelines.

Adding Entries to a Stream

The XADD command is used to append entries to a Redis Stream. If the stream does not exist, it will be created automatically.

Java
1// Adding entries to a stream (using XAddParams.xAddParams() to specify the default behavior) 2jedis.xadd("mystream", XAddParams.xAddParams(), Map.of("event", "login", "user", "Alice")); 3jedis.xadd("mystream", XAddParams.xAddParams(), Map.of("event", "purchase", "user", "Bob", "amount", "100")); 4jedis.xadd("mystream", XAddParams.xAddParams(), Map.of("event", "add_to_cart", "user", "Alice", "product", "laptop"));

In this snippet, events like "login," "purchase," and "add_to_cart" are added to the mystream stream along with associated user details. Each entry is stored as a set of key-value pairs, using XAddParams.xAddParams() to ensure the default behavior, making it easy to track various types of data in real time.

Reading Entries from a Stream

The XREAD command retrieves entries from a Redis Stream, starting from a specified ID. This is how you consume and process the stored events.

Java
1// Create XReadParams (1 message, block indefinitely = 0 ms) 2XReadParams xReadParams = XReadParams.xReadParams().count(1).block(0); 3 4// Reading entries from the stream: must pass Map<String, StreamEntryID> 5List<Map.Entry<String, List<StreamEntry>>> messages = 6 jedis.xread(xReadParams, Map.of("mystream", new StreamEntryID("0-0"))); 7 8System.out.println("Stream messages:"); 9for (Map.Entry<String, List<StreamEntry>> stream : messages) { 10 for (StreamEntry entry : stream.getValue()) { 11 System.out.println("ID: " + entry.getID() + " Fields: " + entry.getFields()); 12 } 13}

Here, XREAD fetches messages with the specified XReadParams, starting from the beginning of the mystream stream (0-0 is the starting ID). Note that although we start reading from ID 0-0, the actual ID of the first entry in the stream will differ, as IDs are stored as timestamps by default.

The .block(0) parameter makes the XREAD command wait indefinitely for new entries if none are available, which is useful in real-time processing scenarios. Each entry is identified by a unique ID and contains its fields and values. This allows you to process events sequentially while maintaining order and reliability.

Why It Matters

Redis Streams are essential for applications that require:

  1. Real-Time Data Processing: Streams handle high-frequency events like logs, user activities, and messaging.
  2. Scalability: The append-only nature ensures efficient writes, even with high throughput.
  3. Flexibility: Streams store data as key-value pairs, making them adaptable to various real-time data needs.

By leveraging Redis Streams, you can create robust, low-latency systems that process and manage massive amounts of data effectively.

Recap and Next Steps

In this lesson, you learned how to:

  1. Add Entries: Use XADD with XAddParams.xAddParams() to append entries to a Redis Stream.
  2. Read Entries: Use XREAD with XReadParams to retrieve and process entries.
  3. Understand Streams: Recognize their append-only structure, unique identifiers, and key-value flexibility.

Redis Streams are a foundational tool for building real-time applications such as chat systems, event monitoring, and activity tracking. Now it’s time to put these concepts into action with hands-on practice!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.