DEV Community

Rijul Rajesh
Rijul Rajesh

Posted on

How Redis Slow Log Helps You Debug Laggy Commands

If you're working with Redis in production, performance is everything. Redis is known for its speed, but what happens when certain commands start taking too long to execute? That’s where Redis Slow Log comes in.

What Is Redis Slow Log?

The Redis Slow Log is a built-in feature that helps you monitor slow-running commands. It logs any command that takes longer than a certain amount of time to execute.

This is useful for spotting performance bottlenecks—especially in high-traffic systems where even small delays can have a big impact.

Redis only measures the execution time of the command, not the time taken to read from or write to the network. So it's strictly about how long Redis itself took to process it.

How Does It Work?

The Slow Log operates based on two settings:

  1. slowlog-log-slower-than This sets the threshold (in microseconds) for what Redis considers "slow".

If a command takes longer than this value, it gets logged.

Setting it to 0 logs all commands.

Setting it to -1 disables the Slow Log completely.

  1. slowlog-max-len This defines how many slow log entries Redis will keep in memory. Older entries are removed when this limit is reached.

You can configure both in your Redis config file or at runtime using:

CONFIG SET slowlog-log-slower-than 10000
CONFIG SET slowlog-max-len 128
Enter fullscreen mode Exit fullscreen mode

This would log commands slower than 10 milliseconds and store the 128 most recent ones.

Viewing the Slow Log

Redis provides a simple command to view the log:

SLOWLOG GET [number]
Enter fullscreen mode Exit fullscreen mode

For example:

SLOWLOG GET 5
Enter fullscreen mode Exit fullscreen mode

This will show the 5 most recent slow log entries. Each entry contains:

  • A unique ID

  • Timestamp when it was logged

  • Execution time in microseconds

  • The command and its arguments

Example output:

1) 1) (integer) 15              # Unique slow log ID
   2) (integer) 1625403941      # Unix timestamp
   3) (integer) 20015           # Execution time in microseconds
   4) 1) "HMGET"                # Actual command
      2) "user:1234"
      3) "name"
      4) "email"

Enter fullscreen mode Exit fullscreen mode

Clearing the Slow Log

You can clear all slow log entries with:

SLOWLOG RESET
Enter fullscreen mode Exit fullscreen mode

Note: This removes all entries and cannot be undone.

Why Use It?

  • Spotting inefficient patterns, like using KEYS or SMEMBERS on large datasets.

  • Diagnosing issues during traffic spikes or high CPU usage.

  • Understanding how custom scripts (like Lua scripts) are performing.

If you're a software developer who enjoys exploring different technologies and techniques like this one, check out LiveAPI. It’s a super-convenient tool that lets you generate interactive API docs instantly.

LiveAPI helps you discover, understand and use APIs in large tech infrastructures with ease!

So, if you’re working with a codebase that lacks documentation, just use LiveAPI to generate it and save time!

You can instantly try it out here! 🚀

Top comments (0)