Mastering Redis Replication in Java: How Redis‑Replicator Works and Extends
Redis‑replicator is a lightweight Java library that implements Redis master‑slave synchronization, parses RDB and AOF files, supports Redis 2.6‑4.0 (including modules), and offers extensible command and module hooks, making it suitable for embedding in backend systems.
Overview
Redis‑replicator is a Java library that implements the Redis master‑slave synchronization protocol, allowing a client to act as a slave and receive data from a master. It also parses RDB files, splits and merges them, and parses AOF data for remote backup.
Supported Versions
Since version 2.3.0, the library supports all Redis versions from 2.6.x up to 4.0.x, including modules introduced in Redis 4.0.
Advantages and Disadvantages
Pros : a single 297 KB JAR with only a commons‑logging dependency, easy to embed in any project without jar conflicts.
Cons : only point‑to‑point synchronization; any additional features must be implemented manually, lacking an all‑in‑one solution.
Comparable Projects
redis‑rdb‑tool – parses RDB but does not support data synchronization.
x‑pipe – not lightweight; offers better HA and monitoring.
redis‑migrate‑tool – written in C, does not support Redis 4.0 modules but can sync clusters.
Quick Start Example
Replicator replicator = new RedisReplicator("127.0.0.1", 6379, Configuration.defaultSetting());
replicator.addRdbListener(new RdbListener.Adaptor() {
@Override
public void handle(Replicator replicator, KeyValuePair<?> kv) {
System.out.println(kv);
}
});
replicator.addCommandListener(new CommandListener() {
@Override
public void handle(Replicator replicator, Command command) {
System.out.println(command);
}
});
replicator.open();Extending Commands
When a new Redis command appears that the library does not recognize, developers can add custom command classes, parsers, and listeners as shown below.
// Implement an unrecognized command
public class YourAppendCommand implements Command {
public final String key;
public final String value;
public YourAppendCommand(String key, String value) {
this.key = key;
this.value = value;
}
}
// Parser for the command
public class YourAppendParser implements CommandParser<YourAppendCommand> {
@Override
public YourAppendCommand parse(Object command) {
return new YourAppendCommand(new String((byte) command[1], UTF_8),
new String((byte) command[2], UTF_8));
}
}
// Register parser and handle events
Replicator replicator = new RedisReplicator("127.0.0.1",6379,Configuration.defaultSetting());
replicator.addCommandParser(CommandName.name("APPEND"), new YourAppendParser());
replicator.addCommandListener(new CommandListener() {
@Override
public void handle(Replicator replicator, Command command) {
if (command instanceof YourAppendCommand) {
YourAppendCommand append = (YourAppendCommand) command;
// your code here
}
}
});Module Extension
Redis 4.0 introduced module support, allowing users to define custom commands and RDB storage formats. Module extensions require implementing the Module and ModuleParser interfaces and registering them via Replicator.addModuleParser.
Adoption
Companies and projects such as Dongfang Caifu, 51 Credit Card, and incubator‑rocketmq‑externals have used or plan to use Redis‑replicator.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
21CTO
21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
