FAQ - Help and questions

Axum or actix-web: which Rust web framework should I choose?

#axum#actix-web#web#frameworkspublished 0 replies0 views
ReplyWatch
Share
Hacker NewsRedditLinkedInE-mail

Short answer

Both are mature, async, built on tokio and good for production. Axum is a thin layer over tower and hyper, so tower middleware works directly; actix-web has its own middleware system and a long track record. Pick the one whose handler style you prefer; both are shown below.

Checked with rustc 1.93.0, axum 0.8.9, actix-web 4.21.0, tokio 1.54.1, Windows 11; both examples compiled and answered a real request with curl

Frequently asked question, written and answered by the Ruststudio team. Replies are open: corrections are welcome.

#1

Answer#

Either one will do for a typical website or JSON API, and nothing in this answer is a speed claim: no benchmark was run for it. The practical differences are in how you write handlers and middleware.

axum 0.8.9actix-web 4.21.0
Handlersplain async fn; arguments are extractors (Path, Json, State)plain async fn, or functions with route attributes such as #[get("/")]
Middlewaretower Layers, so the tower and tower-http crates (CORS, tracing, compression, timeouts) plug in directlyactix-web's own middleware traits (wrap, wrap_fn)
Shared stateState<T> with Router::with_stateweb::Data<T> with App::app_data
Testscall the Router as a tower service (oneshot)actix_web::test helpers
Starting a serveraxum::serve(listener, app)`HttpServer::new(

The same hello page in both. Axum:

src/main.rsrust
use axum::{routing::get, Router};

#[tokio::main]
async fn main() {
    let app = Router::new().route("/", get(|| async { "hello from axum" }));
    let listener = tokio::net::TcpListener::bind("127.0.0.1:3001").await.unwrap();
    println!("listening on {}", listener.local_addr().unwrap());
    axum::serve(listener, app).await.unwrap();
}

actix-web:

src/main.rsrust
use actix_web::{web, App, HttpResponse, HttpServer};

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    let server = HttpServer::new(|| App::new().route("/", web::get().to(|| async { HttpResponse::Ok().body("hello from actix-web") })))
        .bind(("127.0.0.1", 3002))?
        .workers(2)
        .run();
    println!("listening on 127.0.0.1:3002");
    server.await
}

Both were run and answered GET / with status 200 and the text shown in the code.

Cargo.tomltoml
# axum version
axum = "0.8.9"
tokio = { version = "1.54.1", features = ["full"] }

# actix-web version (instead of the two lines above)
actix-web = "4.21.0"

Common mistakes#

  • Choosing by a benchmark you read once. Your own handlers, database and network decide the speed far more than the framework does; measure your own app if speed matters.
  • Mixing the two in one program without need. They use different request and response types, so middleware written for one does not plug into the other.

Can I use tower-http layers with actix-web?#

Not directly. tower-http layers are tower services and fit axum; actix-web has its own middleware interface.

Is it hard to switch later?#

The business logic (database code, validation, domain types) does not depend on either framework if you keep it in plain functions. The routing, extractors and error types do, so plan on rewriting that layer.

Sources#

    QuoteReply
    Share
    Hacker NewsRedditLinkedInE-mail

    No replies yet