Overview
ValKeyper supports Redis-compatible master-slave replication, allowing you to create replicas of your data across multiple nodes. This enables horizontal scaling for read operations and provides data redundancy.Master-Slave Architecture
In ValKeyper’s replication model:- Master nodes accept write operations and propagate changes to slaves
- Slave nodes replicate data from the master and serve read requests
- Replication is asynchronous by default
Configuring Replication
Starting a Slave Node
To configure a ValKeyper instance as a slave, use the--replicaof flag:
127.0.0.1:6379.
Source reference: store.go:654-659
Starting a Master Node
By default, any ValKeyper instance runs as a master:Replication Handshake Process
When a slave connects to a master, it performs a three-phase handshake implemented inHandleReplication() and SendHandshake().
Phase 1: PING
The slave sends a PING command to verify the master is responsive.Source:
store.go:568-573Phase 2: REPLCONF
The slave sends two REPLCONF commands to exchange configuration:Source:
- Listening port: Informs the master of the slave’s port
- Capabilities: Declares support for PSYNC2 protocol
store.go:575-585PSYNC Protocol
Master-Side PSYNC Handling
When the master receives a PSYNC command, it responds with a full resynchronization:store.go:264-277
The master:
- Sends the FULLRESYNC response with replication ID and offset
- Transmits an RDB snapshot as a bulk string
- Registers the connection as a slave
RDB Transfer
After the handshake, the master sends an RDB file containing the current dataset:store.go:601-622
Command Propagation
After initial sync, the master propagates write commands to all connected slaves:store.go:174-178
Only write commands (like SET) are propagated. Read commands are not forwarded to slaves.
Replication Offset Tracking
Slaves track their replication offset to maintain synchronization state:store.go:161-171
The offset is incremented by the byte length of each command received from the master.
REPLCONF GETACK
Masters can query slaves for their current replication offset:store.go:254-257
This is used by the WAIT command to ensure writes have been replicated.
WAIT Command
The WAIT command blocks until a specified number of slaves acknowledge a write:store.go:278-303
Monitoring Replication
Use the INFO command to check replication status:store.go:247-253
Architecture Flow
Best Practices
Network Configuration
Network Configuration
Ensure slaves can reach the master on the specified IP and port. Firewall rules should allow TCP connections on the replication port.
Monitoring Lag
Monitoring Lag
Regularly check replication offsets on slaves to detect lag. Use the INFO command to compare master and slave offsets.
Failover Planning
Failover Planning
ValKeyper does not provide automatic failover. You’ll need to manually promote a slave to master during failures.