- Server role selection on startup.
cmd/server/main.goreads args.- Default role is
master. - If started with
--replicaof <host> <port>, role becomesslave.
- Replica initiates connection to master.
- In slave mode,
go helper.ConnectToMaster(st)is started. - Replica dials
masterHost:masterPort.
- Replica performs handshake on the same TCP connection.
- Sends
PING. - Sends
REPLCONF listening-port <replica-port>. - Sends
REPLCONF capa psync2. - Sends
PSYNC ? -1.
- Master handles
PSYNCand registers replica connection.
- On
PSYNC, master sends+FULLRESYNC <replid> <offset>. - Sends an RDB bulk payload (currently
emptyRDB). - Appends that same
connintost.Replicas.
- Replica reads snapshot then enters stream loop.
- Reads FULLRESYNC line.
- Reads RDB bulk header and exact binary payload.
- Enters loop reading RESP arrays from the master connection.
- Applies incoming commands via
commands.ApplyReplicaCommand(st, parts).
- Master propagates commands to registered replicas.
- After command execution in normal request flow, master calls
helper.PropagateToReplicas(st, parts). PropagateToReplicasserializes the command to RESP and writes to eachst.Replicassocket.
- Effective replicated command support today.
- Replica apply path currently handles only
SET. - Other propagated commands are ignored by
ApplyReplicaCommand.
master_repl_offsetis not incremented during propagation.- Dead/disconnected replica sockets are not removed when writes fail.
- Replica command apply is limited (currently only
SET).
go build -o go_redis_server ./cmd/server./go_redis_server --port 6380./go_redis_server --port 6381 --replicaof 127.0.0.1 6380redis-cli -p 6380 SET mykey hello
redis-cli -p 6380 GET mykeyExpected:
- Master returns
OKforSETandhelloforGET.
redis-cli -p 6381 GET mykeyExpected:
- Replica returns
hello.
redis-cli -p 6380 INFO replication
redis-cli -p 6381 INFO replicationExpected:
- Master info contains
role:master. - Replica info contains
role:slave.
redis-cli -p 6380 SET c 1
redis-cli -p 6380 INCR c
redis-cli -p 6380 GET c
redis-cli -p 6381 GET cExpected right now:
- Master
GET cis2. - Replica
GET cmay still be1because replica apply path currently handles onlySET.
printf '*3\r\n$3\r\nSET\r\n$5\r\nmykey\r\n$5\r\nhello\r\n' | nc 127.0.0.1 6380
printf '*2\r\n$3\r\nGET\r\n$5\r\nmykey\r\n' | nc 127.0.0.1 6381Expected:
- Replica returns
helloformykey.