Lesson 4
Utilizing Redis Streams for Event Logging in Java Using Lettuce
Utilizing Redis Streams for Event Logging

Welcome! In this unit, we will explore how to use Redis streams for event logging in Java with the Lettuce library. This is an important part of our Redis-based backend system project. By the end of this lesson, you will know how to log events and retrieve them using Redis streams. Remember, you've already learned how to manage user data and handle transactions. Now, we're adding another layer to our system by using streams.

What You'll Build

In this unit, we will focus on the following tasks:

  1. Adding entries to a stream: We will log user activities in a Redis stream.
  2. Reading entries from a stream: You will see how to read the logged events from the stream.

Let's start by refreshing what we've learned about adding data. This time, we will use streams instead of simple keys. Here's a snippet to show how you can add an entry to a stream and read it back:

Java
1import io.lettuce.core.RedisClient; 2import io.lettuce.core.api.StatefulRedisConnection; 3import io.lettuce.core.api.sync.RedisStreamCommands; 4import io.lettuce.core.StreamMessage; 5import io.lettuce.core.XReadArgs.StreamOffset; 6 7import java.util.HashMap; 8import java.util.List; 9import java.util.Map; 10 11public class Main { 12 13 public static void main(String[] args) { 14 // Connect to Redis 15 RedisClient redisClient = RedisClient.create("redis://localhost:6379/"); 16 StatefulRedisConnection<String, String> connection = redisClient.connect(); 17 RedisStreamCommands<String, String> syncCommands = connection.sync(); 18 19 // Example usage 20 String streamName = "user_activity_stream"; 21 22 // Add entries to the stream 23 Map<String, String> messageBody1 = new HashMap<>(); 24 messageBody1.put("event", "login"); 25 messageBody1.put("username", "alice"); 26 syncCommands.xadd(streamName, messageBody1); 27 28 Map<String, String> messageBody2 = new HashMap<>(); 29 messageBody2.put("event", "login"); 30 messageBody2.put("username", "bob"); 31 syncCommands.xadd(streamName, messageBody2); 32 33 // Read entries from the stream 34 List<StreamMessage<String, String>> messages = syncCommands.xread( 35 StreamOffset.from(streamName, "0")); 36 37 for (StreamMessage<String, String> message : messages) { 38 System.out.println("Stream entry: " + message.getBody()); 39 } 40 41 // Close the connection 42 connection.close(); 43 redisClient.shutdown(); 44 } 45}

In this code, we read the entries from the user_activity_stream and print each one. The streamName and offset parameters help control the stream reading operation. In the xread method:

  • streamName specifies which stream you want to read from.
  • offset determines the starting point in the stream from which to read entries. For example, "0" means reading from the beginning.

Feel ready to give it a try? Let's jump into the practice section and start working on logging events using Redis streams. Happy coding!

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