Replication: SYNC + broadcast feed
Two phases: bulk transfer via SYNC, then live streaming over a broadcast feed. The wire format is RESP, identical to what redis-cli sends. Same protocol your clients already speak.
if !args.is_empty() && args[0].to_uppercase() == "SYNC" {
let snap: Vec<(String, String)> = {
let s = store.lock().await;
s.store.iter().map(|(k, v)| (k.clone(), v.clone())).collect()
};
for (k, v) in &snap {
let frame = encode_array_strings(&["SET".to_string(), k.clone(), v.clone()]);
socket.write_all(&frame).await?;
}
socket.write_all(&encode_simple("SYNCED")).await?;
rx = Some(repl.subscribe());
is_replica_sync = true;
}When SYNC arrives, master snapshots keys as SET frames, writes them, then sets the conn into replica-streaming mode by holding a broadcast Receiver.
A replica refuses client writes. The replica connection handler matches on the command name and returns -READONLY for SET and DEL. The boolean dance from Python becomes a separate handler in Rust, kept tidy by the type system.