0
0
Redisquery~5 mins

XLEN for stream length in Redis - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: XLEN for stream length
O(1)
Understanding Time Complexity

We want to understand how the time it takes to get the length of a Redis stream changes as the stream grows.

Specifically, how does the XLEN command perform when the stream has more or fewer entries?

Scenario Under Consideration

Analyze the time complexity of the following Redis command.

XLEN mystream

This command returns the number of entries in the stream named "mystream".

Identify Repeating Operations

Look for any repeated work the command might do.

  • Primary operation: Accessing the stored length value of the stream.
  • How many times: Exactly once, no loops or traversals.
How Execution Grows With Input

The command simply reads a stored number, so the work stays the same no matter how big the stream is.

Input Size (n)Approx. Operations
101
1001
10001

Pattern observation: The operation count stays constant regardless of stream size.

Final Time Complexity

Time Complexity: O(1)

This means the time to get the stream length does not change even if the stream grows very large.

Common Mistake

[X] Wrong: "XLEN must scan all entries to count them, so it gets slower with bigger streams."

[OK] Correct: Redis keeps track of the stream length internally, so XLEN just reads that number instantly without scanning.

Interview Connect

Knowing that some commands like XLEN run in constant time helps you understand how Redis handles data efficiently behind the scenes.

Self-Check

"What if we used XRANGE to get all entries instead of XLEN? How would the time complexity change?"