ARCHIVED from builddistributedsystem.com on 2026-04-28 — URL: https://builddistributedsystem.com/tracks/sharder/tasks/task-8-1-shard-controller
TASK

Implementation

Build a shard controller that manages shard assignment:

  1. Maintain configuration: which replica group owns which shards
  2. Support operations: Join (add group), Leave (remove group), Move (reassign shard)
  3. Replicate controller state with Raft for fault tolerance
  4. Distribute shards evenly across groups
  5. Provide configuration query API

The controller is the source of truth for shard ownership.

Sample Test Cases

Join new groupTimeout: 5000ms
Input
{"src":"c0","dest":"controller","body":{"type":"init","msg_id":1,"node_id":"controller","node_ids":["controller"]}}
{"src":"c1","dest":"controller","body":{"type":"join","msg_id":2,"gid":"g1","servers":["s1","s2"]}}
{"src":"c1","dest":"controller","body":{"type":"query","msg_id":3,"num":-1}}
Expected Output
{"src":"controller","dest":"c0","body":{"type":"init_ok","in_reply_to":1,"msg_id":0}}
{"src":"controller","dest":"c1","body":{"type":"join_ok","in_reply_to":2,"msg_id":1}}
{"src":"controller","dest":"c1","body":{"type":"query_ok","in_reply_to":3,"msg_id":2,"version":1,"groups":{"g1":["s1","s2"]},"shards":{"0":"g1","1":"g1","2":"g1","3":"g1","4":"g1","5":"g1","6":"g1","7":"g1","8":"g1","9":"g1"}}}

Hints

Hint 1
Controller manages shard-to-group mapping
Hint 2
Use Raft for controller replication
Hint 3
Support join, leave, move operations
OVERVIEW

Theoretical Hub

Sharding

When data exceeds one machine's capacity, split it across multiple machines (shards). Each shard handles a subset of keys. Sharding provides horizontal scalability.

Shard Controller

The controller decides which shard goes where. It is typically a small Raft group for high availability. Configuration changes are versioned to coordinate migrations.

Key Concepts

shardingconfigurationcoordination
main.py
python
Implement Shard Controller - The Sharder | Build Distributed Systems