Lesson 4
Using Redis Streams for Event Logging in C++ with Hiredis
Using Redis Streams for Event Logging in C++ with Hiredis

Welcome! In this unit, we will explore how to use Redis streams for event logging in C++ with the hiredis 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, handle transactions, and work with sorted sets. 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 using C++ with hiredis:

Adding and Reading Stream Entries
C++
1#include <iostream> 2#include <hiredis/hiredis.h> 3 4int main() { 5 // Connect to Redis 6 redisContext* context = redisConnect("127.0.0.1", 6379); 7 if (context == NULL || context->err) { 8 std::cerr << "Error: " << (context ? context->errstr : "Can't allocate redis context") << std::endl; 9 if (context) redisFree(context); 10 return 1; 11 } 12 13 // Example usage 14 const char* stream_name = "user_activity_stream"; 15 16 // Add entries to the stream 17 redisCommand(context, "XADD %s * event login username alice", stream_name); 18 redisCommand(context, "XADD %s * event login username bob", stream_name); 19 20 // Read entries from the stream 21 redisReply* reply = (redisReply*)redisCommand(context, "XREAD COUNT 2 STREAMS %s 0", stream_name); 22 23 if (reply->type == REDIS_REPLY_ARRAY) { 24 for (size_t i = 0; i < reply->elements; ++i) { 25 redisReply* stream = reply->element[i]; 26 redisReply* entries = stream->element[1]; 27 for (size_t j = 0; j < entries->elements; ++j) { 28 redisReply* entry = entries->element[j]; 29 // Stream entry ID 30 std::cout << "Stream entry ID: " << entry->element[0]->str << std::endl; 31 32 // Stream entry data (key-value pairs) 33 redisReply* entry_data = entry->element[1]; 34 if (entry_data->type == REDIS_REPLY_ARRAY) { 35 for (size_t k = 0; k < entry_data->elements; k += 2) { 36 std::cout << "Key: " << entry_data->element[k]->str 37 << ", Value: " << entry_data->element[k+1]->str << std::endl; 38 } 39 } 40 } 41 } 42 } 43 44 freeReplyObject(reply); 45 redisFree(context); 46 47 return 0; 48} 49/* Output: 50Stream entry ID: 1702312847534-0 51Key: event, Value: login 52Key: username, Value: alice 53Stream entry ID: 1702312847535-0 54Key: event, Value: login 55Key: username, Value: bob 56*/

In this code, we read the entries from the user_activity_stream and print each one. The XREAD command with the COUNT parameter helps control the stream reading operation, specifying how many entries you want to retrieve.

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

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