Memory usage analysis (INFO memory) in Redis - Time & Space Complexity
When we check Redis memory usage with INFO memory, we want to know how the time to get this info changes as data grows.
We ask: Does checking memory take longer if Redis holds more data?
Analyze the time complexity of the following Redis command.
INFO memory
This command returns memory usage details of the Redis server at the moment.
Look for repeated steps inside the INFO memory command.
- Primary operation: Gathering memory stats from Redis internal data structures.
- How many times: Once per INFO memory call; no loops over all keys.
Checking memory info takes about the same time no matter how much data Redis holds.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 keys | Small fixed number |
| 1000 keys | Same small fixed number |
| 100000 keys | Still same small fixed number |
Pattern observation: The time to get memory info stays about the same regardless of data size.
Time Complexity: O(1)
This means checking memory info takes a constant time no matter how much data Redis stores.
[X] Wrong: "INFO memory takes longer as Redis stores more keys because it scans all data."
[OK] Correct: INFO memory reads pre-calculated stats, so it does not scan all keys each time.
Understanding constant time operations like INFO memory shows you know how Redis efficiently manages data and reports stats.
"What if INFO memory had to calculate memory usage by scanning every key? How would the time complexity change?"