💻 Programming

Actix

A high-performance web framework for Rust

📖 Detailed Explanation

Actix is a high-performance web framework written in the Rust language, designed based on the Actor Model. Nikolay Kim began developing it in 2017, and it has consistently ranked near the top in the TechEmpower web framework benchmarks, earning recognition as one of the fastest web frameworks in the world.

Actix-web is a web framework built on top of the Actix actor framework. It uses the Tokio runtime for asynchronous I/O, and leverages Rust's ownership system and type safety to provide memory safety while delivering C/C++-level performance. It supports both HTTP/1.x and HTTP/2, and WebSocket is also built in.

The actor model is a concurrency model in which each actor has its own independent state and communicates only through messages. This approach fundamentally prevents data races caused by shared state, and can efficiently handle thousands of concurrent connections. Through this, Actix is ideal for building highly scalable microservices.

In practice, Actix is used for API servers that need high throughput, real-time data streaming, high-performance proxy servers, and more. Companies that handle large-scale traffic, such as Discord and Cloudflare, have adopted Actix for their Rust-based services, and especially in environments that must handle hundreds of thousands of requests per second, it shows significantly lower memory usage and latency compared to Go or Node.js.

💻 Code Example

use actix_web::{web, App, HttpServer, HttpResponse, Responder};
use serde::{Deserialize, Serialize};

// 1. JSON 요청/응답을 위한 구조체 정의
#[derive(Deserialize)]
struct CreateUser {
    name: String,
    email: String,
}

#[derive(Serialize)]
struct User {
    id: u64,
    name: String,
    email: String,
}

// 2. 핸들러 함수 정의
async fn hello() -> impl Responder {
    HttpResponse::Ok().body("안녕하세요! Actix-web입니다.")
}

async fn create_user(user: web::Json<CreateUser>) -> impl Responder {
    let new_user = User {
        id: 1,
        name: user.name.clone(),
        email: user.email.clone(),
    };
    HttpResponse::Created().json(new_user)
}

async fn get_user(path: web::Path<u64>) -> impl Responder {
    let user_id = path.into_inner();
    let user = User {
        id: user_id,
        name: "홍길동".to_string(),
        email: "hong@example.com".to_string(),
    };
    HttpResponse::Ok().json(user)
}

// 3. 서버 시작
#[actix_web::main]
async fn main() -> std::io::Result<()> {
    println!("서버 시작: http://127.0.0.1:8080");

    HttpServer::new(|| {
        App::new()
            .route("/", web::get().to(hello))
            .route("/users", web::post().to(create_user))
            .route("/users/{id}", web::get().to(get_user))
    })
    .bind("127.0.0.1:8080")?
    .run()
    .await
}

🗣️ Say It Like This in Practice

💬 In a Meeting
"This API server has to handle 100,000 requests per second. With Node.js the memory usage gets too high, but if we switch to Actix-web, we can expect more than 3 times the throughput and 70% lower memory usage on the same specs."
💬 In an Interview
"Actix-web is based on the actor model and handles asynchronous I/O on top of the Tokio runtime. Thanks to Rust's ownership system, it can implement safe concurrency without data races, and it shows top-tier performance in the TechEmpower benchmarks."
💬 In a code review
"You must not call blocking I/O directly in this handler. Wrap it with web::block to run it on a separate thread pool, or use an asynchronous version of the library. Otherwise, the entire runtime will be blocked."

Take a mock interview on this topic → WIA SOOM AI Interview · Start for free

⚠️ Common Mistakes & Cautions

❌
Performing blocking work in async handlers

Calling a sync database driver or file I/O directly will freeze the entire runtime. Use `web::block()`, or use an async driver such as sqlx or tokio-postgres.

❌
Missing error handling

If you do not handle Rust's Result type properly and overuse `.unwrap()`, panics will occur in production. Use the `?` operator and custom error types to implement graceful error handling.

✅
Sharing state with App::app_data()

When sharing state between handlers (such as a DB connection pool), use `App::app_data()` and `web::Data<T>`. Wrapping it in Arc allows safe access from multiple worker threads.

🔗 Related Terms

📚 Learn More