Slow log for performance analysis in Redis - Time & Space Complexity
When using Redis's slow log, we want to understand how the time to record slow commands changes as more commands run.
We ask: How does logging slow commands affect performance as the number of commands grows?
Analyze the time complexity of this Redis slow log usage.
SLOWLOG GET 10
SLOWLOG LEN
SLOWLOG RESET
SLOWLOG GET
This code fetches the last 10 slow commands, checks the slow log length, resets it, and fetches all slow commands.
Look for repeated actions in slow log commands.
- Primary operation: Retrieving slow log entries involves scanning stored entries.
- How many times: Up to the number requested (like 10 or all entries), which depends on slow log size.
Getting slow log entries grows with how many entries you ask for.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 operations to fetch entries |
| 100 | 100 operations to fetch entries |
| 1000 | 1000 operations to fetch entries |
Pattern observation: The work grows linearly with the number of slow log entries requested.
Time Complexity: O(n)
This means the time to get slow log entries grows directly with how many entries you want to see.
[X] Wrong: "Fetching slow log entries is always fast and constant time regardless of size."
[OK] Correct: Because the command must read each entry requested, so asking for more entries takes more time.
Knowing how slow log commands scale helps you understand Redis performance monitoring and how to keep your system responsive.
"What if we limited the slow log size to a fixed number? How would that affect the time complexity of fetching entries?"
Practice
Solution
Step 1: Understand what slow log tracks
The slow log records commands that exceed a certain execution time threshold.Step 2: Differentiate from other Redis features
It does not store all commands, clear cache, or backup data; it only logs slow commands.Final Answer:
To record commands that take longer than a set time to execute -> Option CQuick Check:
Slow log = records slow commands [OK]
- Thinking slow log stores all commands
- Confusing slow log with cache clearing
- Assuming slow log is for backups
Solution
Step 1: Recall the Redis slow log retrieval command
The official command to get slow log entries isSLOWLOG GET.Step 2: Verify other options
Commands likeSLOWLOG FETCH,SLOWLOG SHOW, andSLOWLOG LISTdo not exist in Redis.Final Answer:
SLOWLOG GET -> Option DQuick Check:
Retrieve slow log = SLOWLOG GET [OK]
- Using non-existent commands like SLOWLOG FETCH
- Confusing with LIST or SHOW commands
- Misspelling the command
SLOWLOG RESET SLOWLOG LEN SLOWLOG GET 2
What will be the output of
SLOWLOG LEN immediately after the reset?Solution
Step 1: Understand SLOWLOG RESET effect
TheSLOWLOG RESETcommand clears all slow log entries, so the log becomes empty.Step 2: Check SLOWLOG LEN after reset
Since the log is empty,SLOWLOG LENreturns 0, indicating no entries.Final Answer:
0 -> Option AQuick Check:
Reset clears log, length = 0 [OK]
- Expecting old entries after reset
- Confusing SLOWLOG LEN with SLOWLOG GET output
- Thinking reset causes error
SLOWLOG GET but receive an empty list even though you expect slow commands. What is the most likely cause?Solution
Step 1: Understand slow log threshold role
Redis only logs commands slower than the configured threshold (default 10ms). If threshold is too high, no commands get logged.Step 2: Eliminate other causes
Redis server down would cause connection errors, not empty logs. Restart is not needed to enable slow log. Slow log size is limited but overwrites old entries, so it won't be empty due to fullness.Final Answer:
The slow log threshold is set too high, so no commands qualify -> Option AQuick Check:
High threshold = no slow commands logged [OK]
- Assuming Redis must restart to enable slow log
- Thinking slow log fills up and stops logging
- Confusing server down with empty slow log
Solution
Step 1: Set threshold to 50 milliseconds
Redis uses microseconds, so 50 ms = 50000 μs. UseCONFIG SET slowlog-log-slower-than 50000. A and C correct; B and D use 50 μs (too low).Step 2: Limit slow log length to 5 entries
UseCONFIG SET slowlog-max-len 5. Only A and D do this; B and C do not.Step 3: Reset slow log and get last 5 entries
SLOWLOG RESETclears old entries;SLOWLOG GET 5retrieves last 5. All options include this.Step 4: Choose correct option
Only A sets both max-len to 5 and threshold to 50000 μs (50 ms).Final Answer:
CONFIG SET slowlog-max-len 5 CONFIG SET slowlog-log-slower-than 50000 SLOWLOG RESET SLOWLOG GET 5 -> Option BQuick Check:
Threshold 50ms = 50000μs, max len 5, reset, get 5 [OK]
- Confusing milliseconds with microseconds in threshold
- Not setting max length to limit entries
- Forgetting to reset slow log before analysis
Ready to prove it?
Pass the test and this pattern moves unknown
