ARCHIVED from builddistributedsystem.com on 2026-04-28 — URL: https://builddistributedsystem.com/tracks/store/tasks/task-7-1-kv-interface
TASK

Implementation

Implement the key-value store interface on top of Raft:

  1. GET(key) - Return current value or null
  2. PUT(key, value) - Set key to value
  3. CAS(key, expected, new) - Compare-and-swap

Each write operation:

  1. Leader receives request
  2. Append to Raft log
  3. Wait for commitment
  4. Apply to state machine
  5. Return result to client

Sample Test Cases

Put and get keyTimeout: 5000ms
Input
{"src":"c0","dest":"n1","body":{"type":"init","msg_id":1,"node_id":"n1","node_ids":["n1"]}}
{"src":"c1","dest":"n1","body":{"type":"write","msg_id":2,"key":"x","value":1}}
{"src":"c1","dest":"n1","body":{"type":"read","msg_id":3,"key":"x"}}
Expected Output
{"src":"n1","dest":"c0","body":{"type":"init_ok","in_reply_to":1,"msg_id":0}}
{"src":"n1","dest":"c1","body":{"type":"write_ok","in_reply_to":2,"msg_id":1}}
{"src":"n1","dest":"c1","body":{"type":"read_ok","in_reply_to":3,"msg_id":2,"value":1}}

Hints

Hint 1
Support get, put, cas operations
Hint 2
Each operation becomes a log entry
Hint 3
Wait for commit before responding
OVERVIEW

Theoretical Hub

Building on Consensus

Raft provides ordered, replicated log. We build a KV store by interpreting log entries as operations. All nodes apply the same operations in order, producing the same state.

Maelstrom KV Workloads

Maelstrom tests lin-kv (linearizable KV) and lww-kv (last-write-wins). Linearizable requires waiting for Raft commit. LWW can accept local writes immediately.

Key Concepts

key-valueAPIoperations
main.py
python
Implement Key-Value Interface - The Store | Build Distributed Systems