##About
A demo project for the purposes of benchmarking a Rust web CRUD API, with a backing MongoDB database.
##Running the application
###Database
There is a simple docker-compose configuration included in the project that will run a MongoDB container to serve as a local test database.
Open a terminal and run docker-compose up to run the container.
###Server
Using cargo-watch we are able to run our server locally with hot-reloading. Install cargo-watch as a global cargo dependency by running cargo install cargo-watch
Once installed, open a terminal and run cargo watch -q -c -w src/ -x run to reload actix_web when code changes are made in the src directory.
To run the server without hot-reloading, run cargo run.
##Web Framework
###actix-web
After doing some research into the Rust web framework ecosystem, it seems to me that actix-web is the most sensible option. It has a similar style to other frameworks in its class, with focus on simple and concise code that leverages macros to condense things like route handlers into a few lines.
It wins out over frameworks like Axum and Rocket in all of the benchmarking tests I was able to find, with more requests per second, lower CPU and memory usage, and higher data transfer rates.
It has a few great selling points:
-
Simplicity: with syntax similar to Express, actix-web makes writing routes and middleware a breeze - especially when you consider the Extractors it provides that exposes request data to request handlers automatically
-
Multi-threaded by default: actix-web automatically configures a number of worker threads equal to the number of physical cores of the host machine - this can be easily configured to suite the needs of the application
-
Asynchronous: built on top of tokio, the best performing asynchronous Rust runtime, actix-web recommends a pattern in which request handlers are completely non-blocking - allowing for worker threads that are awaiting some work (e.g. database operations) to begin handling new requests while they wait
-
Graceful shutdown default: when a critical failure occurs, actix-web allows active request handlers some time (default 30 seconds) to complete their requests before exiting the process
Overall, it seems to have the best balance between maturity, durability, performance, and accessibility.
- Luke
##Dependencies
###mongodb Standard MongoDB driver for Rust. Documentation can be found here.
Note: this driver uses connection pooling by default (default pool of 20 connections).
###serde + serde_json
The standard of serialization and deserialization in Rust. Documentation can be found here.