Redis Slowlog

Difficulty: Intermediate

Question

What is the Redis SLOWLOG, how do you configure it, and how would you use it to debug latency?

Answer

SLOWLOG is an in-memory log of commands whose execution time exceeded a threshold, set by slowlog-log-slower-than in microseconds (default 10,000, which is 10 ms). You read it with SLOWLOG GET, and each entry tells you the command, its arguments, how long it ran and which client sent it. Because Redis runs commands on a single thread, one slow command delays every other client, so the slowlog is the first place to look when latency spikes.

What each entry contains

SLOWLOG GET [count] returns the most recent entries first (10 by default). Each entry has:

  1. A unique, incrementing ID.
  2. The UNIX timestamp when the command ran.
  3. The execution time in microseconds.
  4. The command and its arguments (long argument lists are truncated).
  5. The client's ip:port.
  6. The client name, if it was set with CLIENT SETNAME.

SLOWLOG LEN returns how many entries are stored, and SLOWLOG RESET clears them.

What it measures and what it does not

The duration is pure execution time inside Redis. It does not include network round trips, time spent waiting in the queue behind other commands, or the time to read the request and write the reply. So a client can see 200 ms latency while the slowlog is empty. In that case the cause is elsewhere: network issues, a fork for BGSAVE, swapping, or a client sending thousands of commands without pipelining. For those, use LATENCY DOCTOR and the latency monitor (enable it with latency-monitor-threshold).

Configuring it sensibly

SettingDefaultMeaning
slowlog-log-slower-than10000Threshold in microseconds. 0 logs every command, a negative value disables logging.
slowlog-max-len128Number of entries kept. Oldest entries are dropped once full.

On a busy production cache, many teams lower the threshold to 1,000 to 5,000 microseconds and raise slowlog-max-len to 1,000 or more. The log lives in memory, so even a few thousand entries cost very little.

Typical culprits you find in the log

  • KEYS * on a large keyspace. Replace it with SCAN.
  • HGETALL, SMEMBERS or LRANGE 0 -1 on a big key with hundreds of thousands of elements.
  • DEL on a huge collection. Use UNLINK, which frees memory in a background thread.
  • SORT, ZUNIONSTORE or SUNION over large sets.
  • Long-running Lua scripts or functions.

A real workflow: an orders service at peak shows p99 latency jumping from 2 ms to 80 ms. SLOWLOG GET 20 shows HGETALL cart:* calls taking 40 ms each because some carts store full product JSON. The fix is to fetch only needed fields with HMGET and keep payloads small. Then run redis-cli --bigkeys or --memkeys to find other oversized keys before they cause the next incident.

Where SLOWLOG timing starts and stops. Only the execution step is timed, so network and queueing delays never appear in the slowlog.
  1. Client sends command: Network time is not counted.
  2. Waits in event loop: Queueing behind other commands is not counted.
  3. Command executes: This duration is what SLOWLOG records.
  4. Reply written back: Output time is not counted.

Code examples

Configuring and reading the slowlog

# Log anything slower than 5 ms and keep 1000 entries
redis-cli CONFIG SET slowlog-log-slower-than 5000
redis-cli CONFIG SET slowlog-max-len 1000

# Show the 2 most recent slow commands
redis-cli SLOWLOG GET 2

# How many entries are stored, then clear after investigating
redis-cli SLOWLOG LEN
redis-cli SLOWLOG RESET

Entry 412 shows an HGETALL that took about 42 ms from the orders-api service. Entry 411 is a KEYS call from an unnamed client, which should be replaced with SCAN.

Key points

  • Threshold is in microseconds: default 10000 means 10 ms.
  • slowlog-max-len defaults to 128 entries; the log is in memory and not persisted.
  • It measures execution time only, not network or queueing time.
  • Common offenders: KEYS, HGETALL or SMEMBERS on big keys, DEL on huge collections, long Lua scripts.
  • Pair it with LATENCY DOCTOR and redis-cli --bigkeys for a full picture.

Common follow-up questions

  • The client sees 100 ms latency but SLOWLOG is empty. What else would you check?
  • Why is DEL on a large set slow, and how does UNLINK help?
  • How do you find big keys in production without blocking the server?
  • What does the latency monitor capture that SLOWLOG does not?

Interview tips

  • Say "microseconds" explicitly; mixing up units is a common slip that interviewers notice.
  • Explain why slow commands hurt everyone (single-threaded execution), not just the caller.
  • Point out that SLOWLOG excludes network time; it shows you understand its limits.

Concepts covered

SLOWLOG, slowlog-log-slower-than, slowlog-max-len, Latency, Big Keys

More Redis & Caching questions