Replication: same script, two roles

Two phases: SYNC bulk transfer, then live streaming. RESP all the way down.

08-replication/server.rb
ruby
def replicate(args)
  return if $replicas.empty?
  payload = encode_array_strings(args)
  $rep_mu.synchronize do
    live = []
    $replicas.each do |rep|
      begin; rep.write(payload); live << rep; rescue IOError, Errno::EPIPE; rep.close rescue nil; end
    end
    $replicas = live
  end
end

$replicas is a thread-safe Array of TCPSockets. replicate fans out and drops dead replicas.

Replica refuses client SET and DEL with -READONLY. The from_replica: true kwarg lets the master-streamed writes through. Two booleans (log_to_aof, from_replica) thread through handle_args - typed flags would be cleaner; Ruby uses kwargs.

Quiz: Quiz

Loading practice…