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?"
Practice
used_memory field in the INFO memory command represent in Redis?Solution
Step 1: Understand the
TheINFO memoryoutputINFO memorycommand shows various memory stats about Redis.Step 2: Identify
used_memorymeaningused_memoryspecifically shows how many bytes Redis is currently using in RAM.Final Answer:
The amount of memory Redis is currently using in bytes -> Option BQuick Check:
used_memory= current memory used [OK]
- Confusing 'used_memory' with total keys count
- Mixing 'used_memory' with peak memory
- Thinking it shows fragmentation ratio
Solution
Step 1: Recall Redis commands for memory info
The Redis command to get detailed memory info isINFO memory.Step 2: Check other options
MEMORY USAGEexists but returns usage for a single key, not overall memory info. The others are invalid commands.Final Answer:
INFO memory -> Option DQuick Check:
UseINFO memoryto get memory stats [OK]
- Using MEMORY USAGE to get overall memory info
- Typing invalid commands like SHOW MEMORY
- Confusing command names
used_memory:1048576 used_memory_peak:2097152 mem_fragmentation_ratio:1.5
What does the
mem_fragmentation_ratio value of 1.5 indicate?Solution
Step 1: Understand
This ratio shows how much extra memory Redis uses due to fragmentation.mem_fragmentation_ratioStep 2: Interpret value 1.5
A value of 1.5 means Redis uses 1.5 times the memory it actually needs, indicating fragmentation.Final Answer:
Redis memory is fragmented, using 1.5 times more memory than needed -> Option AQuick Check:
Fragmentation ratio > 1 means extra memory used [OK]
- Thinking ratio shows number of keys
- Assuming ratio below 1 means fragmentation
- Confusing fragmentation with peak memory
INFO memory and see used_memory_peak is lower than expected despite high used_memory. What is the likely cause?Solution
Step 1: Understand
This shows the highest memory Redis used since last restart.used_memory_peakStep 2: Compare with
If currentused_memoryused_memoryis higher, it means memory usage increased after peak was reset (likely after restart).Final Answer:
Redis memory peak resets after restart, but current usage is higher -> Option CQuick Check:
Peak resets on restart, current usage can be higher [OK]
- Assuming memory leak without evidence
- Thinking command syntax is wrong
- Believing no keys means memory usage
mem_fragmentation_ratio is consistently above 2.0. What is the best action to reduce memory fragmentation?Solution
Step 1: Understand high fragmentation impact
A ratio above 2 means Redis uses twice the memory needed, hurting performance.Step 2: Identify Redis command to reduce fragmentation
MEMORY PURGEtriggers allocator to release unused memory and reduce fragmentation.Step 3: Evaluate other options
Restarting helps temporarily but is disruptive. Increasing keys worsens memory. Deleting all keys is drastic and unnecessary.Final Answer:
RunMEMORY PURGEcommand to defragment memory -> Option AQuick Check:
UseMEMORY PURGEto reduce fragmentation [OK]
- Restarting Redis as first fix
- Adding more keys to fix memory
- Deleting all keys unnecessarily
Ready to prove it?
Pass the test and this pattern moves unknown
