# tinyredis Defining the Replication adn Election Algorithm features of `tinyredis`. ## Replication ### How does the Master know about which Replicas to send to? The master does not maintain an active list of replicas. It simply sends out updates to all clients connected to its replica (slave) ports. Any client connected to the master's replica ports will receive the update streams. When a replica wants to connect to a master, it simply connects to the master's replica port, just like any standard client. The master does not distinguish between replicas and normal clients. ### How does a Replica join the "network"? The Replica need only send a `PSYNC` message to a `replication port` (explained below), and the Master will begin the sync process. Read the "How Redis replication works" and "Replication ID explained" sections at [this link](https://redis.io/docs/management/replication/) for the details on how to implement syncing. ### What if the Master and Replica become out of sync? If the connection is lost at any point, the replica can issue a `PSYNC` command to do a partial resynchronization of data and continue replication from where it left off. ### How does the Replica leave the "network"? There is no "unregister" process for replicas. They simply disconnect from the master's replica port to stop receiving updates. The master does not keep track of replicas that go offline. ### What ports does the Master send replicated data to? Port `6379` is the port that the master uses to listen for client requests. All ports following that port are for a single replica that wants to get updates from the master (i.e. ports `6380`, `6381`, and onwards). Then the question becomes, how does the Master know which range of ports to send to, it could be infinite, and how does it know which of the ports have active connections? Well, Redis has a complicated spec for this, which I propose we ignore and simply limit the range to ports `6380` to `6390`, and make the Master send `PING` messages to each of these ports and maintain a list of active connections based on whether or not it got a `PONG` as a reply. ## Election Algorithm - Add functionality to `server.rs` to send `PING` to the pimrary port every `x` seconds, if no reply is recieved in `y` seconds, call the relection function - `relection()` -