How to Mirror Redis Traffic to Another Redis Node with Redis-Mirror

Introduction

In some production or development scenarios, you might need to mirror Redis traffic to another node for investigation or debugging purposes. Redis-Mirror is a simple yet powerful tool that can help you achieve this task. This tutorial will guide you through the process of installing and using Redis-Mirror to mirror traffic between two Redis instances.

What is Redis-Mirror?

Redis-Mirror is a real-time traffic mirroring tool for Redis instances. It reads the STDOUT from the redis-cli monitor command and mirrors the keys to another instance. The tool is easy to install and use, and provides several useful options for managing the mirroring process.

Installation

You can install Redis-Mirror using pip:

1
pip install redismirror

Alternatively, you can install it from the source code:

1
2
3
4
git clone https://github.com/alivx/redis-mirror.git
cd redis-mirror
pip install -r requirements.txt
pip install setup.py

Usage

To mirror Redis traffic, you can use the redis-cli monitor command to output the Redis traffic, then pipe it into the redismirror command. Here’s an example:

1
redis-cli monitor | redismirror --shost localhost --sport 6379 --dhost 127.0.0.1 --dport 6379

This command will mirror traffic from the source Redis instance at localhost:6379 to the destination Redis instance at 127.0.0.1:6379.

Options

Redis-Mirror provides several options to customize the mirroring process. You can view them using the --help flag:

1
redismirror --help

Some of the options include:

Options:

  • --shost: Source redis host/IP.
  • --sport: Source redis port.
  • --sdb: Source redis DB.
  • --sauth: Source redis auth info.
  • --dhost: Destination redis host/IP.
  • --dport: Destination redis port.
  • --ddb: Destination redis DB.
  • --dauth: Destination redis auth info.
  • --limit: Stop mirror process at limit X.
  • --replace: Replace key if exists.
  • --ttl: Enable to mirrored the TTL value for each key if exist
  • --ttle: Increase the TTL value of the key with custom value.
  • --help: Show this message and exit.

Examples

Example 1: Basic Usage

The most basic usage of redis-mirror involves mirroring traffic from one Redis instance to another on the same machine.

1
redis-cli monitor | redismirror --sport 6379 --shost localhost --dhost 127.0.0.1 --dport 6379

Example 2: Limiting the Number of Mirrored Keys

In some cases, you may want to limit the number of keys being mirrored. To do this, use the --limit option:

1
redis-cli monitor | redismirror --shost localhost --dport 6377 --limit 100

Example 3: Replacing Keys in the Destination Instance

By default, redis-mirror will not replace existing keys in the destination Redis instance. To allow key replacement, use the --replace option:

1
redis-cli monitor | redismirror --sport 6379 --shost localhost --dhost 127.0.0.1 --dport 6378 --replace

Example 4: Mirroring Keys with TTL

To mirror keys along with their Time to Live (TTL) values, use the --ttl option:

1
redis-cli monitor | redismirror --sport 6379 --shost localhost --dhost 127.0.0.1 --dport 6378 --ttl

Example 5: Mirroring Keys with Custom TTL

If you want to increase the TTL value for each key, use the --ttle option followed by the custom value:

1
redis-cli monitor | redismirror --sport 6379 --shost localhost --dhost 127.0.0.1 --dport 6378 --ttl --ttle 10

Example 6: Mirroring Keys and Stopping After a Limit

To combine multiple options, such as replacing keys, mirroring TTL values, increasing TTL, and stopping after a certain number of keys:

1
redis-cli monitor | redismirror --sport 6379 --shost localhost --dhost 127.0.0.1 --dport 6378 --replace --ttl --ttle 10 --limit 10

Conclusion

In this tutorial, we covered the basics of using the redis-mirror tool to mirror traffic from one Redis instance to another. We demonstrated six practical examples to help you understand how to use this powerful tool effectively. By leveraging redis-mirror, you can efficiently debug and investigate issues in your Redis instances without disrupting the normal functioning of your application.