Implementation
In distributed systems, nodes communicate by exchanging messages. The Maelstrom framework uses JSON messages over stdin/stdout for simplicity and language-agnosticism.
Your task is to implement a basic message parser that reads JSON messages from stdin. Each message has the following structure:
{
"src": "c1", // Source node ID
"dest": "n1", // Destination node ID
"body": { // Message payload
"type": "...", // Message type
"msg_id": 1 // Optional message ID
}
}Read messages from stdin (one JSON object per line), parse them, and print the parsed fields to stdout in the format: PARSED: src|dest|body_type. Also log detailed information to stderr for debugging.
Your node should continue reading until stdin is closed.
Sample Test Cases
{
"src": "c1",
"dest": "n1",
"body": {
"type": "echo",
"msg_id": 1
}
}PARSED: c1|n1|echo
Hints
Hint 1▾
Hint 2▾
Hint 3▾
Hint 4▾
Hint 5▾
Theoretical Hub
Message-Based Communication
Distributed systems communicate through messages because nodes cannot share memory. This is a fundamental constraint that shapes how we design distributed algorithms.
Why Messages?
In a single-machine program, threads can share memory directly. But in a distributed system:
- Nodes are on different machines - they have separate memory spaces
- Networks are unreliable - messages can be delayed, duplicated, or lost
- Failures are partial - some nodes may crash while others continue
Each message must be self-contained with enough information for the recipient to process it independently.
The Maelstrom Protocol
Maelstrom uses a simple JSON-based protocol with three required fields:
src- identifies who sent the messagedest- identifies the intended recipientbody- contains the actual payload with atypefield
Why stdin/stdout?
Using standard streams makes the protocol language-agnostic. Maelstrom can spawn your binary and communicate with it regardless of what language you wrote it in. This same pattern is used by many real systems for inter-process communication (IPC).
Message Flow Example
Client (c1) --> Node (n1)
{
"src": "c1",
"dest": "n1",
"body": {"type": "echo", "msg_id": 1, "echo": "hello"}
}
Node (n1) --> Client (c1)
{
"src": "n1",
"dest": "c1",
"body": {"type": "echo_ok", "msg_id": 0, "in_reply_to": 1, "echo": "hello"}
}