How to Mirror Redis Traffic to Another Redis Node with Redis-Mirror
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.
You can install Redis-Mirror using pip:
Alternatively, you can install it from the source code:
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:
This command will mirror traffic from the source Redis instance at
localhost:6379 to the destination Redis instance at
Redis-Mirror provides several options to customize the mirroring process. You can view them using the
Some of the options include:
--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.
Example 1: Basic Usage
The most basic usage of
redis-mirror involves mirroring traffic from one Redis instance to another on the same machine.
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
Example 3: Replacing Keys in the Destination Instance
redis-mirror will not replace existing keys in the destination Redis instance. To allow key replacement, use the
Example 4: Mirroring Keys with TTL
To mirror keys along with their Time to Live (TTL) values, use the
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:
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:
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.