Redis Performance Monitoring
Tags: redis, monitoring, performance
Published: 2023-07-07 12:59:57

Monitoring Redis, an in-memory data structure store, is crucial to ensure its performance, availability, and efficient resource utilization.

By tracking metrics such as command latency, memory usage, CPU utilization, and throughput, you can identify areas for optimization and fine-tune your Redis configuration for optimal performance.

## Redis metrics to monitor

You can use a monitoring solution or agent to collect key performance metrics from Redis, such as CPU usage, memory consumption, throughput, latency, and cache hit rates. These metrics provide insights into the overall health and performance of your Redis instances.

**Latency**. Measure the response time of Redis commands to assess the performance of your Redis instances. Track latency metrics for commonly used commands to identify potential bottlenecks and ensure acceptable response times.

**Memory usage**. Keep an eye on Redis memory usage to ensure it stays within acceptable limits. Monitor metrics like used memory, memory fragmentation, and eviction rates to identify potential memory leaks or inefficient memory utilization.

**CPU utilization**. Monitor CPU usage to ensure that your Redis instances are not under excessive load. High CPU usage can impact the responsiveness and performance of Redis. Track CPU metrics and identify any spikes or sustained high usage that may require optimization or scaling.

**Throughput**. Monitor the throughput of Redis by measuring the number of commands processed per second. Track metrics such as total commands, operations per second, or requests per second to understand the workload on your Redis instances and identify any sudden changes or spikes in traffic.

**Keyspace**. Monitor the size and distribution of keys within Redis to identify memory-consuming or frequently accessed keys. Analyze metrics like the number of keys, key sizes, and key eviction rates to optimize memory usage and performance.

**Network**. Monitor network traffic and bandwidth usage of Redis to identify any potential bottlenecks or network-related issues. Track metrics such as incoming and outgoing network traffic to ensure optimal network performance.

**Logs**. Monitor Redis logs for warnings, errors, or other important messages. Centralized log management solutions can help you analyze log data, detect patterns, and troubleshoot issues.

By monitoring these aspects of Redis, you can ensure optimal performance, troubleshoot issues promptly, and maintain the stability and reliability of your Redis infrastructure.

## Redis INFO

When you run the `INFO` command in Redis, it returns a comprehensive set of metrics and information about the Redis server, allowing you to monitor its performance, resource usage, and operational aspects.

[OpenTelemetry Redis](https://uptrace.dev/get/monitor/opentelemetry-redis.html) receiver periodically runs the `INFO` command to collect telemetry data from Redis and send it to your observability pipeline for analysis and monitoring. It integrates with OpenTelemetry, which is a set of standard APIs, libraries, and instrumentation for collecting and exporting telemetry data.

```
127.0.0.1:6379> info keyspace
# Keyspace
db0:keys=406783,expires=406783,avg_ttl=5427303
```

## Memory fragmentation

Memory fragmentation occurs when memory is allocated and deallocated, leading to unused gaps between allocated blocks of memory. This fragmentation can lead to inefficient memory usage and increased memory footprint.

Redis manages memory using its own memory allocator. When objects are created, Redis allocates memory blocks to store them. Over time, objects can be deallocated, resulting in gaps or holes in the allocated memory.

Fragmentation can lead to higher overall memory usage in Redis because fragmented memory blocks cannot be reused effectively. Allocating memory for new objects can become slower and more resource intensive because of the need to look for contiguous blocks of memory.

It is important to monitor memory fragmentation levels in Redis using tools like `INFO memory` command or third-party monitoring solutions. If fragmentation becomes a concern, you can take proactive measures such as periodically restarting Redis, adjusting memory policies, or utilizing external memory allocators.

```
127.0.0.1:6379> info memory
# Memory
mem_fragmentation_ratio:1.09
mem_fragmentation_bytes:3640288
```

### Active defragmentation

Defragmentation in Redis works by scanning the keyspace and moving Redis objects (such as strings, lists, sets, etc.) to new locations, thereby eliminating fragmentation. It works in the background, ensuring that the process does not interfere with normal Redis operations.

Active defragmentation is an optional feature and needs to be enabled in the Redis configuration. You can enable it by setting the `activedefrag` option to `yes` in the Redis configuration file or by using the `CONFIG SET` command.

```
CONFIG SET activedefrag yes
```

Note that enabling active defragmentation may introduce some additional CPU and I/O overhead due to the memory relocation process. Therefore, it's essential to monitor the overall system performance when using this feature.

## Redis Latency Monitor

Redis [latency monitoring](https://redis.io/docs/reference/optimization/latency-monitor/) is crucial for assessing the performance of your Redis instance and identifying potential bottlenecks or issues.

You can track Redis command latency using the `LATENCY` command in Redis. The `LATENCY DOCTOR` command provides a summary of the latency distribution, while the `LATENCY HISTORY` command provides historical information about latency measurements.

You can enable the latency monitor at runtime with the following command:

```
CONFIG SET latency-monitor-threshold 100
```

## Redis SlowLog

Redis [slowlog](https://redis.io/commands/slowlog/) allows you to track and analyze Redis commands that exceed a certain execution time threshold. When enabled, Redis will log these slow commands in a dedicated slowlog, providing valuable insights into the performance of your Redis instance.

To enable the slowlog feature, you can set the `slowlog-log-slower-than' configuration parameter in your Redis configuration file or use the `CONFIG SET' command to configure it dynamically. This parameter specifies the threshold execution time in microseconds, and any command that exceeds this threshold will be logged to the slowlog.

You can retrieve slowlog entries using the `SLOWLOG GET' command. This command allows you to retrieve a specified number of slowlog entries or to retrieve all entries. Each slowlog entry is represented as an array of values containing relevant information about the slow command.

By default, Redis stores the last 128 entries, but you can configure the maximum number of entries using the `slowlog-max-len` configuration parameter.

Redis slowlog provides a valuable tool for identifying and analyzing slow commands, helping you optimize the performance of your Redis instance and troubleshoot potential performance issues. It is particularly useful for understanding and addressing bottlenecks in your Redis workload.

## Uptrace

Uptrace is [open source APM](https://uptrace.dev/get/open-source-apm.html) that provides comprehensive monitoring capabilities for Redis, allowing you to gain visibility into your Redis infrastructure and make informed decisions based on the collected data.

Uptrace uses [OpenTelemetry](https://uptrace.dev/opentelemetry/) to instrument code and collect traces, metrics, and logs. OpenTelemetry specifies how to collect and export telemetry data. With OpenTelemetry, you can instrument your application once and then add or change vendors without changing the instrumentation.

## Conclusion

Redis monitoring plays a vital role in maintaining the stability, performance, and security of your Redis infrastructure. It enables you to optimize resource utilization, troubleshoot issues efficiently, and make data-driven decisions to ensure the reliable and efficient operation of your Redis deployment.

- [OpenTelemetry vs OpenTracing](https://uptrace.dev/blog/opentelemetry-vs-opentracing.html)
- [Jaeger vs Prometheus](https://uptrace.dev/blog/jaeger-vs-prometheus.html)