Why streams handle event logs in Redis - Performance Analysis
When using Redis streams to handle event logs, it's important to understand how the time it takes to process events changes as the log grows.
We want to know how the cost of adding and reading events grows with the number of events stored.
Analyze the time complexity of adding and reading events in a Redis stream.
XADD mystream * event "login" user "alice"
XADD mystream * event "logout" user "bob"
XRANGE mystream - + COUNT 10
XREAD COUNT 5 STREAMS mystream 0
This code adds events to a stream and reads a range or a count of events from it.
Look for repeated actions that affect performance.
- Primary operation: Adding events with XADD and reading events with XRANGE or XREAD.
- How many times: Each event is added once; reading can request multiple events at once.
Adding an event takes about the same time no matter how many events are in the stream.
Reading events takes time proportional to how many events you ask for, not the total stream size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 events | Adding: constant time; Reading 5 events: about 5 operations |
| 100 events | Adding: constant time; Reading 10 events: about 10 operations |
| 1000 events | Adding: constant time; Reading 100 events: about 100 operations |
Pattern observation: Adding events stays fast regardless of stream size; reading scales with how many events you request.
Time Complexity: O(k)
This means the time depends on the number of events you read or write at once, not the total number stored.
[X] Wrong: "Adding events gets slower as the stream grows because it has to check all previous events."
[OK] Correct: Redis streams add events efficiently without scanning old events, so adding stays fast even with many events.
Understanding how Redis streams handle event logs helps you explain efficient data handling in real systems, showing you know how to manage growing data smoothly.
"What if we read events without specifying a count limit? How would that affect the time complexity?"