Implementation
Before processing any workload, Maelstrom sends an init message to each node. This message tells your node its identity and the full cluster membership.
The init message looks like:
{
"type": "init",
"msg_id": 1,
"node_id": "n1",
"node_ids": ["n1", "n2", "n3"]
}Your task is to handle the init message by storing the node_id (your identity) and node_ids (all cluster members). Then respond with an init_ok message:
{
"type": "init_ok",
"in_reply_to": 1
}The in_reply_to field must match the msg_id from the init request.
Sample Test Cases
{
"src": "c0",
"dest": "n1",
"body": {
"type": "init",
"msg_id": 1,
"node_id": "n1",
"node_ids": [
"n1"
]
}
}{"src": "n1", "dest": "c0", "body": {"type": "init_ok", "in_reply_to": 1, "msg_id": 0}}
Hints
Hint 1▾
Hint 2▾
Hint 3▾
Theoretical Hub
Node Initialization
Every distributed system needs a bootstrap phase where nodes learn about themselves and their peers. The init message serves this purpose in Maelstrom.
Understanding Identity
The node_id field gives your node its unique identity. This is critical because:
- All messages you send must use this as the
srcfield - Other nodes will address messages to you using this ID
- Your identity distinguishes you from other nodes in the cluster
Cluster Topology
The node_ids array tells you about all nodes in the cluster. This information becomes essential for:
- Broadcast algorithms - knowing who to send messages to
- Consensus protocols - calculating quorums
- Leader election - participating in voting
Request-Response Pattern
The in_reply_to field establishes a correlation between requests and responses. This pattern is fundamental in distributed systems where you need to match responses to outstanding requests.
Request: { "type": "init", "msg_id": 1, ... }
Response: { "type": "init_ok", "in_reply_to": 1 }
This correlation allows the sender to:
- Track which requests have been answered
- Implement timeouts for unresponsive nodes
- Handle out-of-order message delivery