|
1 | 1 | # mini-redis
|
2 | 2 |
|
3 |
| -`mini-redis` is a lightweight, idiomatic implementation of [Redis](https://redis.io) built with [tokio](https://tokio.rs). |
| 3 | +`mini-redis` is an incomplete, idiomatic implementation of a |
| 4 | +[Redis](https://redis.io) client and server built with |
| 5 | +[Tokio](https://tokio.rs). |
4 | 6 |
|
5 |
| -## Logging |
| 7 | +The intent of this project is to provide a larger example of writing a Tokio |
| 8 | +application. |
6 | 9 |
|
7 |
| -`mini-redis` uses [`tracing`](https://github.com/tokio-rs/tracing) to provide structured logs. Debug logs can be displayed by running: |
| 10 | +**Disclaimer** Don't even think about trying to use this in production... just |
| 11 | +don't. |
8 | 12 |
|
9 |
| -```console |
| 13 | +## Running |
| 14 | + |
| 15 | +The repository provides a server, client library, and some client executables |
| 16 | +for interacting with the server. |
| 17 | + |
| 18 | +Start the server: |
| 19 | + |
| 20 | +``` |
10 | 21 | RUST_LOG=debug cargo run --bin server
|
11 | 22 | ```
|
12 | 23 |
|
13 |
| -Logs will appear when commands are sent to the server from a client. |
| 24 | +The [`tracing`](https://github.com/tokio-rs/tracing) crate is used to provide structured logs. |
| 25 | +You can substitute `debug` with the desired [log level][level]. |
| 26 | + |
| 27 | +[level]: https://docs.rs/tracing-subscriber/latest/tracing_subscriber/filter/struct.EnvFilter.html#directives |
| 28 | + |
| 29 | +Then, in a different terminal window, the various client [examples](examples) |
| 30 | +can be executed. For example: |
| 31 | + |
| 32 | +``` |
| 33 | +cargo run --example hello_world |
| 34 | +``` |
| 35 | + |
| 36 | +Additionally, a CLI client is provided to run arbitrary commands from the |
| 37 | +terminal. With the server running, the following works: |
| 38 | + |
| 39 | +``` |
| 40 | +cargo run --bin cli set foo bar |
| 41 | +
|
| 42 | +cargo run --bin cli get foo |
| 43 | +``` |
| 44 | + |
| 45 | +## Supported commands |
| 46 | + |
| 47 | +`mini-redis` currently supports the following commands. |
| 48 | + |
| 49 | +* [GET](https://redis.io/commands/get) |
| 50 | +* [SET](https://redis.io/commands/set) |
| 51 | +* [PUBLISH](https://redis.io/commands/publish) |
| 52 | +* [SUBSCRIBE](https://redis.io/commands/subscribe) |
| 53 | + |
| 54 | +The Redis wire protocol specification can be found |
| 55 | +[here](https://redis.io/topics/protocol). |
| 56 | + |
| 57 | + |
| 58 | +## Tokio patterns |
| 59 | + |
| 60 | +The project demonstrates a number of useful patterns, including: |
| 61 | + |
| 62 | +### TCP server |
| 63 | + |
| 64 | +[`server.rs`](src/server.rs) starts a TCP server that accepts connections, |
| 65 | +and spawns a new task per connection. It gracefully handles `accept` errors. |
| 66 | + |
| 67 | +### Client library |
| 68 | + |
| 69 | +[`client.rs`](src/client.rs) shows how to model an asynchronous client. The |
| 70 | +various capabilities are exposed as `async` methods. |
| 71 | + |
| 72 | +### State shared across sockets |
| 73 | + |
| 74 | +The server maintains a [`Db`] instance that is accessible from all connected |
| 75 | +connections. The [`Db`] instance manages the key-value state as well as pub/sub |
| 76 | +capabilities. |
| 77 | + |
| 78 | +[`Db`]: src/db.rs |
| 79 | + |
| 80 | +### Framing |
| 81 | + |
| 82 | +[`connection.rs`](src/connection.rs) and [`frame.rs`](src/frame.rs) show how to |
| 83 | +idiomatically implement a wire protocol. The protocol is modeled using an |
| 84 | +intermediate representation, the `Frame` structure. `Connection` takes a |
| 85 | +`TcpStream` and exposes an API that sends and receives `Frame` values. |
| 86 | + |
| 87 | +### Graceful shutdown |
| 88 | + |
| 89 | +The server implements graceful shutdown. [`tokio::signal`] is used to listen for |
| 90 | +a SIGINT. Once the signal is received, shutdown begins. The server stops |
| 91 | +accepting new connections. Existing connections are notified to shutdown |
| 92 | +gracefully. In-flight work is completed, and the connection is closed. |
| 93 | + |
| 94 | +[`tokio::signal`]: https://docs.rs/tokio/*/tokio/signal/ |
| 95 | + |
| 96 | +### Concurrent connection limiting |
| 97 | + |
| 98 | +The server uses a [`Semaphore`] limits the maximum number of concurrent |
| 99 | +connections. Once the limit is reached, the server stops accepting new |
| 100 | +connections until an existing one terminates. |
| 101 | + |
| 102 | +[`Semaphore`]: https://docs.rs/tokio/*/tokio/sync/struct.Semaphore.html |
| 103 | + |
| 104 | +### Pub/Sub |
| 105 | + |
| 106 | +The server implements non-trivial pub/sub capability. The client may subscribe |
| 107 | +to multiple channels and update its subscription at any time. The server |
| 108 | +implements this using one [broadcast channel][broadcast] per channel and a |
| 109 | +[`StreamMap`] per connection. Clients are able to send subscription commands to |
| 110 | +the server to update the active subscriptions. |
| 111 | + |
| 112 | +[broadcast]: https://docs.rs/tokio/*/tokio/sync/broadcast/index.html |
| 113 | +[`StreamMap`]: https://docs.rs/tokio/*/tokio/stream/struct.StreamMap.html |
| 114 | + |
| 115 | +## Contributing |
| 116 | + |
| 117 | +Contributions to `mini-redis` are welcome. Keep in mind, the goal of the project |
| 118 | +is **not** to reach feature parity with real Redis, but to demonstrate |
| 119 | +asynchronous Rust patterns with Tokio. |
| 120 | + |
| 121 | +Commands or other features should only be added if doing so is useful to |
| 122 | +demonstrate a new pattern. |
| 123 | + |
| 124 | +Contributions should come with extensive comments targetted to new Tokio users. |
| 125 | + |
| 126 | +## FAQ |
| 127 | + |
| 128 | +#### Should I use this in production? |
| 129 | + |
| 130 | +No. |
| 131 | + |
| 132 | +## License |
| 133 | + |
| 134 | +This project is licensed under the [MIT license](LICENSE). |
| 135 | + |
| 136 | +### Contribution |
14 | 137 |
|
15 |
| -More documentation on enabling different log levels and filtering logs can be found [here](https://docs.rs/tracing-subscriber/latest/tracing_subscriber/filter/struct.EnvFilter.html#directives). |
| 138 | +Unless you explicitly state otherwise, any contribution intentionally submitted |
| 139 | +for inclusion in `mini-redis` by you, shall be licensed as MIT, without any |
| 140 | +additional terms or conditions. |
0 commit comments