Peer-to-Peer Replication

July 14, 2025
See the code for this post on the shreder repository.

Peer-to-Peer Replication

Replication ensures data availability even when nodes fail.

Implementation

// shreder/replication.go
func (cs *CacheServer) replicaset(key string, value string) {
cs.mu.Lock()
defer cs.mu.Unlock()
req := struct {
Key string `json:"key"`
Value string `json:"value"`
}{
Key: key,
Value: value,
}
data, _ := json.Marshal(req)
for _, peer := range cs.peers {
if peer != "" && peer != cs.selfID {
go func(peer string) {
peerURL := peer
if !strings.HasPrefix(peerURL, "http://") {
peerURL = "http://" + peerURL
}
client := &http.Client{}
req, err := http.NewRequest("POST", peerURL+"/set", bytes.NewReader(data))
if err != nil {
log.Printf("Failed to create replication request: %v", err)
return
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set(replicationHeader, "true")
resp, err := client.Do(req)
if err != nil {
log.Printf("Failed to replicate to peer %s: %v", peer, err)
return
}
defer resp.Body.Close()
}(peer)
}
}
}

Replication Strategy

  • Asynchronous replication to all peer nodes
  • Special header prevents infinite replication loops
  • Graceful error handling for failed replications
See the code for this post on the shreder repository.