# Identifying Slow Queries

Slow commands can impact KeyDB performance, especially under high concurrency or when inefficient data access patterns are used. Whether you’re running KeyDB on Elestio via the dashboard, inside a Docker Compose setup, or accessing it through the CLI, KeyDB includes powerful introspection tools like the slow log and latency tracking.

This guide shows how to detect slow operations using KeyDB’s built-in slowlog, analyze latency issues, and optimize performance through configuration tuning and query best practices.

## **Inspecting Slow Commands from the Terminal**

KeyDB supports the Redis-compatible <span class="s3">SLOWLOG</span> feature to record commands that exceed a configured execution time threshold. These logs are useful to spot expensive operations and server stalls.

#### **Connect to Your KeyDB Instance via Terminal**

Use <span class="s3">keydb-cli</span> or <span class="s3">redis-cli</span> to connect to your KeyDB instance:

```bash
keydb-cli -h <host> -p <port> -a <password>
```

Replace <span class="s1">&lt;host&gt;</span>, <span class="s1">&lt;port&gt;</span>, and <span class="s1">&lt;password&gt;</span> with the credentials available in your Elestio dashboard.

#### **View the Slowlog Threshold**

Check what execution time (in microseconds) is considered “slow”:

```bash
CONFIG GET slowlog-log-slower-than
```

The default is <span class="s1">10000</span> (10 milliseconds). Commands slower than this will be logged.

#### **View the Slow Query Log**

To retrieve recent slow operations:

```bash
SLOWLOG GET 10
```

This shows the 10 most recent slowlog entries, each with:

- The command that was executed
- The timestamp
- Execution duration in microseconds
- Any arguments passed to the command

## **Analyzing Inside Docker Compose**

If you’re running KeyDB via Docker Compose, you can inspect slow queries from within the container environment.

#### **Access the KeyDB Container**

Launch a shell in your container:

```bash
docker-compose exec keydb bash
```

Connect to KeyDB using:

```bash
keydb-cli -a $KEYDB_PASSWORD
```

Ensure that <span class="s1">REDIS\_PASSWORD</span> (or <span class="s1">KEYDB\_PASSWORD</span>) is defined in your <span class="s1">.env</span> file or Compose environment variables.

#### **Adjust Slowlog Settings**

You can view or modify the slowlog threshold dynamically:

```bash
CONFIG SET slowlog-log-slower-than 5000
```

This temporarily changes the threshold to 5 milliseconds, which is useful for debugging under lower latency conditions.

#### **Increase the Number of Stored Entries**

Check how many slowlog entries are retained:

```bash
CONFIG GET slowlog-max-len
```

To store more slowlog entries:

```bash
CONFIG SET slowlog-max-len 256
```

This helps in long-running investigations or during load testing.

## **Using the Latency Monitoring Feature**

KeyDB inherits Redis’s latency monitoring tools, providing additional insights beyond command duration as fork stalls, I/O blocks, or memory pressure.

#### **Enable Latency Monitoring**

Latency tracking is often enabled by default. Run:

```bash
LATENCY DOCTOR
```

This provides a high-level diagnostic report of system latency spikes and potential root causes, including slow commands, AOF rewrites, and blocking operations.

#### **View Latency History for Specific Events**

Track the latency of specific operations like:

```bash
LATENCY HISTORY command
```

Other event categories include:

- <span class="s1">fork</span> – Background save or AOF rewrite delays
- <span class="s1">aof-write</span> – Append-only file sync lag
- <span class="s1">command</span> – General command execution delays

## **Understanding and Resolving Common Bottlenecks**

KeyDB performance can degrade due to specific patterns of usage, large keys, blocking commands, or non-optimized pipelines.

#### **Common Causes of Slowness**

- <span class="s1">**Large keys**</span>: Commands like <span class="s2">LRANGE</span>, <span class="s2">SMEMBERS</span>, or <span class="s2">HGETALL</span> on large datasets.
- <span class="s1">**Blocking commands**</span>: Such as <span class="s2">BLPOP</span>, <span class="s2">BRPOP</span>, or long-running Lua scripts.
- <span class="s1">**Forking delays**</span>: Caused by <span class="s2">SAVE</span> or AOF background rewriting.

#### **Best Practices for Performance**

- <span class="s1">**Use SCAN**</span> instead of <span class="s2">KEYS</span> to iterate large keyspaces safely.
- <span class="s1">**Limit range queries**</span>: Use <span class="s2">LRANGE 0 99</span> instead of fetching full lists.
- <span class="s1">**Enable pipelining**</span>: Reduce round trips by batching commands.
- <span class="s1">**Avoid multi-key ops**</span>: Especially in clustered deployments, where they can cause performance issues or fail.

## **Optimizing with Configuration Changes**

KeyDB performance can be significantly tuned by adjusting memory and persistence-related settings.

#### **Common Tuning Examples**

```bash
CONFIG SET maxmemory-policy allkeys-lru
CONFIG SET save ""
```

These adjust eviction and persistence behaviours. Use these with caution:

- Disabling RDB/AOF improves speed but removes durability.
- LRU/TTL policies control memory usage under load.