Mini 3: Micro-Redis Cluster
In Mini 2, you built a Redis. Now you'll make it scale out. Your task is to turn Micro-Redis into a cluster: keys are spread across several storage nodes using consistent hashing, clients keep talking to it with plain redis-cli, and nodes can join or leave while traffic is flowing. As before, you'll write it in Rust, on top of tokio.
The interesting part isn't the hash ring. (That's an afternoon, maybe less with an AI assistant.) It's moving keys from one node to another without losing a write or stopping the world while you do it.
Core Design Requirements
- Three kinds of processes. Storage nodes hold the data; each is essentially a Mini 2 server with a few extra internal commands. Routers speak RESP to clients and forward each command to the node that owns its key. A single coordinator keeps the authoritative ring and runs membership changes. All of them are separate processes that talk over TCP, and it shouldn't matter whether they run on one machine or several.
- Consistent hashing with virtual nodes. Keys are placed on a hash ring, and each storage node owns many positions on it (virtual nodes). The number of virtual nodes per storage node should be configurable. Don't use Redis Cluster's fixed hash slots; the point of this mini is the ring.
- More than one router. It must be possible to run several routers at once, with clients connected to any of them. A router's view of the ring may fall behind, for instance because it was slow to hear about a change, and the cluster must stay correct anyway.
- Live membership changes. Using a small CLI (see below), an operator can add a storage node to the cluster or remove one from it while clients keep sending commands. Adding a node moves the keys it now owns onto it; removing one drains its keys to their new owners first. Membership changes happen one at a time: while one is in progress, the coordinator may queue or reject others.
- No lost writes. Once a write is acknowledged, it must survive, including while its key is being moved: when the cluster goes quiet, every read of that key sees it (or a later write). No key is ever served by two nodes at once. We don't require linearizability, so a read in the middle of a migration may return a slightly old value.
- Check ownership where the data lives. Routers can be wrong about who owns a key, so storage nodes can't just trust them. Version the ring (an epoch number works well), and have a node refuse commands for keys it doesn't own under its current view. A router that gets refused should catch up and retry. This is the heart of the mini, so think carefully about what each party knows and when.
- Don't stop the world. Commands on keys that aren't moving should be unaffected by a membership change. Commands on keys that are moving may stall briefly, but only briefly. You'll measure how briefly (see Experiments).
- Failure model. Assume storage nodes, routers and the coordinator don't crash, and that the network delivers messages, though perhaps slowly. A router may be paused for a while and then resume. The only things that can go wrong are slowness and our own code.
Hash function. So that everyone's experiments are comparable, a key's position on the ring is the 64-bit xxh3 hash of its raw bytes, with seed 0 (the xxhash-rust crate has it). How you place virtual nodes on the ring is up to you.
Moving keys. There are several reasonable ways to migrate a range of keys. You could freeze just that range, copy it, and flip ownership. You could copy it while it's live and then catch up on the writes that happened during the copy. Or you could move keys one at a time and redirect requests for keys that have already left, as Redis Cluster does with ASK. There are others. Choose whichever you can argue is correct, and argue it in your README.
Starting point. You're encouraged to build on your own Mini 2. There's also a reference Micro-Redis in micro-redis, part of our tools repository. It already supports this mini's commands and includes a small client for when one of your processes needs to talk to another. Use it as a fallback if your Mini 2 isn't in good shape, or borrow whatever bits of it you like.
API Specifications
Clients talk to a router using RESP2, exactly as they talked to Micro-Redis. Use the official Redis command reference for each command's behavior. Routers support the following commands:
- GET, SET: as in Mini 2.
- RPUSH, LPUSH: as in Mini 2.
- LRANGE, LLEN: as in the real spec. (These are new. Our checker needs them to read lists back.)
- EXISTS: as in the real spec. When given several keys, the router may send each key to its owner and add up the answers.
- PING: as in Mini 2, answered by the router itself.
- SELECT: only database 0 exists, as in Redis Cluster.
SELECT 0succeeds, and anything else is an error.
How routers, nodes and the coordinator talk to each other is your call. Extra RESP commands on the storage nodes are one natural choice; an RPC library such as tarpc is another.
The CLI. The operator controls the cluster with a program called cluster-ctl, which talks to the coordinator. It needs at least these subcommands:
cluster-ctl add-node <addr>: add a running, empty storage node to the cluster. It returns once the change has finished, meaning all keys are where they now belong.cluster-ctl remove-node <addr>: drain a storage node and remove it. It also returns once the change has finished, after which the node can be shut down safely.cluster-ctl status: show the current epoch, the storage nodes and how many keys each one holds, and the progress of any change underway.
The Checker
We'll ship a checker, mrc-check, that tests your cluster as a black box. It runs many concurrent clients against your routers, mixing SET/GET on some keys with RPUSH/LRANGE on others, and records every request and response. While the workload runs, it adds and removes storage nodes by running cluster-ctl, and it may pause one of your routers (with SIGSTOP) and resume it later. Afterward, it checks the history for lost writes and lists that don't add up.
You'll find it in mrc-check, in the same tools repository. A typical run looks like this:
mrc-check run \
--router 127.0.0.1:6379 --router 127.0.0.1:6380 \
--node 127.0.0.1:7001 --node 127.0.0.1:7002 --node 127.0.0.1:7003 \
--start-node './scripts/start-node.sh' \
--ctl './target/release/cluster-ctl --coordinator 127.0.0.1:7000' \
--router-pid @run/router-6380.pidHere's what it expects from you:
- Routers (
--router, one per router) answer the commands above over RESP2. - Starting a node (
--start-node) is a shell command that starts a fresh, empty storage node in the background, prints itshost:portas the last line of its output, and exits. It getsMRC_NODE_SEQ(1, 2, …) in its environment, in case you want it to pick a port. - Changing membership (
--ctl) is the start of a command line. The checker appendsadd-node <addr>orremove-node <addr>and runs it, and expects it to exit with status 0 once the change has finished. - Initial nodes (
--node) are the storage nodes in the cluster at the start, so the checker knows what it can remove. - A router to pause (
--router-pid) is a PID, or@fileto read one from a pid file.
By default, the checker pauses that router, adds a node while it's paused, resumes it, and later removes a node. --schedule changes that, and --schedule "" turns membership changes off, for when you run cluster-ctl yourself. On a correct cluster, clients never see an error or a timeout, so the checker counts those as failures too. When a run fails, it shows the requests that prove it and saves the full history, so you can dig in later with mrc-check check <file>. If you're curious whether your cluster is also linearizable, --strict adds checks for stale reads. Its --help and README cover the rest.
Passing the checker doesn't prove your cluster is correct, but failing it proves it isn't. We'll run it on demo day.
You should still write your own tests. The checker tells you that something went wrong; your tests should help you find out why.
Experiments
Run a few small experiments and report the results in your README (a half page plus a plot or two is plenty):
- Balance. How evenly are keys spread across nodes as the number of virtual nodes varies, say 1, 10, 100 and 1,000? Report something like the ratio of the fullest node to the average.
- Movement. When you add or remove a node, what fraction of keys moves? Compare with the ideal (roughly 1/N) and with naive
hash mod N. - Latency during migration. What do p50 and p99 latencies look like for keys that are moving, and for keys that aren't, while a node is being added?
- Router overhead. How much throughput do you lose going through a router compared with talking to a single Micro-Redis directly? (
redis-benchmarkworks for this, as long as you stick to the commands we support.)
Exclusionary
You don't need replication, persistence, or recovery from crashes. You don't need BLPOP/BRPOP (they're gone from this mini entirely), multiple databases, or pipelining. The coordinator can be a single process that isn't fault-tolerant, and you don't need to handle two membership changes running at the same time.
Using AI
You're welcome, even encouraged, to use AI assistants for this mini. A lot of it, such as RESP plumbing, the CLI, load generators and plotting scripts, is exactly the kind of code they're good at. The migration protocol is where they tend to be confidently wrong, so that part you'll need to understand and own yourself. Your README should include a short section, Where AI got it wrong, describing at least one concrete bug or flawed design suggestion an assistant gave you and how you caught it.
Suggested Pace
There's nothing to hand in along the way, but here's a pace that should get you to the finish line with time to spare:
- By day 2: a static cluster. A fixed set of nodes, a router that routes, and
cluster-ctl status. - By day 3: a written plan for migration: which messages are sent, in what order, and why no write can be lost. Sketch the sequence for a single key moving from one node to another. It's much cheaper to find a bug here than in code.
- By day 6: live
add-nodeandremove-node, with epochs and refusals, passing the checker with one router. - By day 7: passing the checker with several routers, one of them paused.
- By day 8: experiments done.
- Day 9: hardening and the README.
How to hand in
- Your code, along with its build configuration (
Cargo.tomland friends). - Your tests, and a way to run them (
cargo testis perfect). - A script that starts a small cluster on one machine (say, a coordinator, three storage nodes and two routers), plus whatever
mrc-checkneeds to start a fresh storage node. - A README that explains your migration protocol and why it never loses an acknowledged write, how ownership is checked and how stale routers catch up, your experiment results, and the Where AI got it wrong section. It should also give step-by-step instructions to start the cluster, talk to it with
redis-cli, usecluster-ctl, and runmrc-checkagainst it. We'll use it to run your cluster ourselves after demo day.
Getting Started & Hand-in Instructions
This mini is individual. You'll work in your own GitHub repository.
- Create a repository on GitHub for this mini, and keep it private until after your demo.
- Follow good commit and branching practices.
- Tag the version you want graded as
1.0.0. - Submit the repository's URL on the course LMS.
Demo day. You'll demo your cluster live. We'll run mrc-check against it while you add and remove nodes on our cue. Find another person to help you on the day. Once you've demoed, make the repository public so we can grade it.
Tips and Pointers:
- The original consistent hashing paper by Karger et al. is short and readable, and Amazon's Dynamo paper shows how virtual nodes are used in practice.
- For more on consistent hashing, see the lecture slides and the Dynamo presentation from class, both on the course LMS.
- The Redis Cluster specification describes how real Redis moves keys between nodes. It uses hash slots rather than a ring, but its
MOVED/ASKredirections are worth reading before you design your own. - hdrhistogram makes the latency percentiles painless.