Single-Node Broadcast

In this challenge, you’ll need to implement a broadcast system that gossips messages between all nodes in the cluster. Gossiping is a common way to propagate information across a cluster when you don’t need strong consistency guarantees.

You can find the problem here

The problem specification — Broadcast RPC
The problem specification — Read RPC
The problem specification — Topology RPC

Understanding the Problem

The problem is simple — we need to implement a broadcast system. For this, we need to implement 3 RPCs:

  1. Broadcast — It provides a new message value to a node. This value must be broadcasted to all other nodes in the system.
  2. Read — It requests to fetch all the message seen in the system, i.e. all the broadcasted value up until this point.
  3. Topology — This provides information about the arrangement of the nodes in the cluster.

Problem 3a — Single Node System

The solution to this part is quite straightforward. The objective of this part of the problem is to setup the basic functionality for our broadcaster service.

Considering a single node system:

  • We can ignore the topology rpc call since arrangement doesn’t matter
  • The broadcast rpc simply appends the received value to a store/map/list located locally on the node
  • The read rpc can return all the values present in the local store/map/list.
n.Handle("topology", func(msg maelstrom.Message) error {
// topology doesn't matter in a single node system
body := map[string]any{
"type": "topology_ok",
}
n.Reply(msg, body)
return nil
})
n.Handle("broadcast", func(msg maelstrom.Message) error {
var body map[string]any

if err := json.Unmarshal(msg.Body, &body); err != nil {
return err
}

message := body["message"].(float64)
logger.Infof("Received message %f", message)
store.addMessage(message)

delete(body, "message")
body["type"] = "broadcast_ok"

n.Reply(msg, body)
return nil
})
n.Handle("read", func(msg maelstrom.Message) error {
messages := store.getMessages()

body := map[string]any{
"type": "read_ok",
"messages": messages,
}

n.Reply(msg, body)
return nil
})

You can view the entire source code here

Pretty simple right?

Now we will extend this solution to a multi node cluster. You can checkout that solution here, but give it a try yourself first :)

Until next time…

Burnerlee

--

--