rgoussu@goussu: ~/library/rust/exercises
~/library/rust/exercises cat tokio-mini-redis.md

Tokio mini-redis

# The Tokio tutorial's mini-redis — an async Redis server and client that make ownership across tasks, framing, channels, and cancellation-by-drop concrete.

Exercisesaved 2026-08-08source #exercise#rust#async#tokio#concurrency#redis

Goal

Build a Redis server and client subset on Tokio, following the official tutorial but writing every line yourself. This is the canonical bridge from "I understand ownership" to "I can write async Rust": every classic wall — Send bounds across .await, shared state between tasks, cancellation — appears here in its natural habitat.

Subject: full brief & instructions

Practices

  • Deep dive Rust — ownership across task boundaries, Arc<Mutex<T>>, Send/Sync in anger, and the async model the note warns is "harder Rust".
  • Networking fundamentals — TCP streams, framing, and a real wire protocol (RESP).
  • Cache management — you are building the world's most instructive cache server.

Milestones

  1. Hello Tokio. Runtime, async fn, .await; connect to your future server's port with the stock mini-redis client to see the target behavior.
  2. Accept loop. TCP listener spawning a task per connection — and your first compiler argument about what may cross into a spawned task, won via move and owned data.
  3. Shared state. GET/SET against an Arc<Mutex<HashMap>>; learn why the lock must not be held across an .await, and shard the map when you feel like it.
  4. Framing. A Connection type reading RESP frames from a buffered stream — partial reads, BytesMut cursor management, frame-not-ready-yet as a normal state.
  5. Channels. A client that multiplexes commands through one connection: mpsc for requests, oneshot per response — message passing as the alternative to locks.
  6. Select & shutdown. select! over connection I/O and a shutdown broadcast; graceful drain, and cancellation-by-drop seen doing its quiet, occasionally surprising work.

Stretch goals

  • Key expiration (EXPIRE) with tokio::time, and pub/sub channels.
  • Benchmark against real Redis with redis-benchmark; profile the gap.
  • Re-solve a Protohackers rung with this stack and compare the two languages' concurrency stories.

Related