How tokio-rs/mini-redis works
Project type and purpose
• mini‑redis is a networked server application *plus* an async client library (with a CLI and example clients) written in Rust using Tokio.
• Its purpose is pedagogical: show how to build a Redis‑style in‑memory database server (with GET, SET, PING, PUBLISH/SUBSCRIBE) and a matching client.
File structure
• Top‑level binaries (src/bin):
• mini‑redis‑server (server.rs)
• mini‑redis‑cli (cli.rs)
• Core library (src/lib.rs), whose modules break down into:
• Networking and protocol framing: connection.rs, frame.rs, parse.rs
• Command dispatch: src/cmd/*.rs
• Shared state: db.rs (in‑memory store + pub/sub)
• Server orchestration: server.rs, shutdown.rs, connection limiting (Semaphore)
• Client library (src/clients): blocking_client.rs, buffered_client.rs, client.rs
• Examples (examples/*.rs) and tests (tests/*.rs)
README for architectural insights
• Uses Tokio runtime for async TCP server and client
• Implements “one task per connection,” a semaphore to limit connections, graceful shutdown via tokio::signal
• Db uses a std::sync::Mutex for state and tokio::sync::broadcast for pub/sub
• Tracing instrumentation, optional OpenTelemetry integration
Main components & interactions (to represent
a. Clients
– Example clients (hello_world, pub/sub chat)
– CLI (mini‑redis‑cli)
– All connect via async TCP to the server’s listening socket
b. Server front‑end
– TCP Listener (accept loop)
– Semaphore guard for max concurrent connections
– Per‑connection tasks
• Connection abstraction: handles reading/writing raw TCP frames
• Frame parser / serializer (Frame IR)
• Command dispatcher (cmd module): routes Frames to handlers (get, set, ping, publish, subscribe, unknown)
c. Shared state & backend
– Db module:
• In‑memory key/value store behind a std::sync::Mutex
• Pub/sub engine: one tokio::sync::broadcast channel per channel name, StreamMap per client task
d. Control & observability
– Graceful shutdown manager (listens for SIGINT, notifies all connection tasks)
– Tracing & optional OpenTelemetry exporter
e. Testing harness
– Uses Tokio’s time mocking (in tests/server.rs)
Architectural patterns & principles
• Asynchronous, non‑blocking I/O with Tokio
• “Task-per-connection” concurrency model
• Shared mutable state synchronization (std Mutex + broadcast channels)
• Framing layer abstraction (Frame IR, Connection)
• Graceful shutdown coordination
• Back‑pressure / connection limiting with Semaphore
• Modular separation: core lib vs. binaries vs. examples/tests