· 3 min read
Rust, Axum, and REST
Tradeoffs with a popular way to put rust on the web
Axum: A Fast and Efficient Framework
Axum is one of the fastest web frameworks available today, designed to leverage Rust’s strengths in performance and safety. In fact, it’s one of the fastest to date. Built on top of Tower
, an HTTP client, it offers a minimalistic approach to building web applications. However, it does come with some limitations.
Limitations of Axum
Asynchronous Operations Only: Since Axum is built on
Tokio
, it natively supports only asynchronous operations. Developers coming from synchronous environments may find this a learning curve.Limited Middleware Ecosystem: Axum has a limited selection of middleware options. Handling sessions, cookies, and other common tasks often requires manual implementation or reliance on external crates.
Manual Error Handling: Error handling must be implemented manually, often involving custom error types and responses, which can add complexity to your application.
Unopinionated Database Integration: Axum does not provide built-in database handling, necessitating the manual integration of an ORM (like SeaORM or Diesel) or direct SQL queries using
Sqlx
. This means more initial setup and boilerplate code.
Despite these limitations, Axum shines in its ability to deliver fine-grained, performant, async-native code.
Strengths of Axum
Type Safety: Axum leverages Rust’s strong type system, providing compile-time checks for routing, extractors, and middleware. This feature significantly reduces runtime errors and enhances code reliability. For instance, it can verify your database connections and SQL queries at compile time, which is incredibly beneficial.
Async-First Design: Built on
Tokio
, Axum is highly performant and excels in handling concurrent requests, making it suitable for high-throughput, low-latency applications.Flexible Routing: Axum offers a flexible routing system that supports complex nested routes, path parameters, and wildcard segments. Developers can define routes using both function-based handlers and closures, providing versatility in implementation.
Integration with Tower: Fully compatible with Tower’s middleware ecosystem, Axum allows developers to reuse middleware components for various tasks, such as request logging, compression, and authentication, fostering modularity and reusability across projects.
Minimal Boilerplate: The simplicity of Axum leads to a minimal amount of boilerplate code. It emphasizes handler functions that directly map to routes, streamlining the process of setting up new routes and handlers.
Custom Extractors: Axum supports custom request parsing and validation through its extractor system, facilitating the handling of incoming requests with complex data, such as JSON or URL parameters.
Extensibility and Middleware Support: Axum’s middleware design is highly extensible, enabling developers to easily plug in custom middleware for tasks like authentication, rate limiting, or logging without complicating route handlers.
Native WebSocket and SSE Support: Axum provides built-in support for WebSocket and Server-Sent Events (SSE), allowing real-time communication capabilities without the need for additional libraries.
Strong Ecosystem: Axum integrates seamlessly with other Rust crates, such as
Serde
for data serialization,Hyper
for HTTP operations, andTower
for middleware, enhancing its functionality and ease of use.Community and Documentation: Although relatively new, Axum boasts a rapidly growing community and well-structured documentation, providing practical examples and guidance for developers of all experience levels.
Small Footprint: Following Rust’s philosophy of zero-cost abstractions, Axum maintains a small performance overhead compared to other frameworks, while still providing high-level abstractions for web applications.
Extremely Fast: Axum’s design, centered around
Tokio
and Rust’s zero-cost abstractions, positions it as one of the fastest web frameworks available, capable of handling high-throughput applications efficiently.
If you’re frustrated with the ecosytem, try it out. I know that after years of python code and wrestling Pydantic and buggy code, having something just work was like a breath of fresh air.