๐ŸŽ“
MASTERCLASS๋‚œ์ด๋„: ์ค‘๊ธ‰ โž” ์‹ค์ „โฑ๏ธ ์•ฝ 4์‹œ๊ฐ„๐Ÿš€ 5๋‹จ๊ณ„ ์ปค๋ฆฌํ˜๋Ÿผ

Rust & Axum ์ดˆ๊ณ ์„ฑ๋Šฅ ์‹œ์Šคํ…œ ๋ฐฑ์—”๋“œ ์ž…๋ฌธ

์†Œ์œ ๊ถŒ(Ownership)๊ณผ Tokio ๋น„๋™๊ธฐ ๋Ÿฐํƒ€์ž„์„ ์ดํ•ดํ•˜๊ณ  Axum 0.8 + SQLx 0.9๋กœ ๋ฉ”๋ชจ๋ฆฌ ์•ˆ์ „ํ•˜๊ณ  ๊ฐ€๋น„์ง€ ์ปฌ๋ ‰ํ„ฐ(GC) ์ง€์—ฐ์ด ์—†๋Š” ์ดˆ๊ณ ์† REST API ์„œ๋ฒ„๋ฅผ ๊ตฌ์ถ•ํ•˜๋Š” ํ•ธ์ฆˆ์˜จ ํŠœํ† ๋ฆฌ์–ผ

--views
#rust#axum#tokio#sqlx#backend#rest-api#systems-programming#postgres#tutorial์ตœ์ข… ์—…๋ฐ์ดํŠธ: 2026-11-14

๐Ÿ“‹ Rust ๋ฐฑ์—”๋“œ ์ƒํƒœ๊ณ„ยท๋ฌธ๋ฒ•์ด ๊ถ๊ธˆํ•˜๋‹ค๋ฉด?
์‹ค์ „ ๋ ˆํผ๋Ÿฐ์Šค๋ฅผ ์œ„ํ•œ Rust & Cargo ๋„๊ตฌ ๋ชจ์Œ ๋ฐ”๋กœ๊ฐ€๊ธฐ โž”

0. ๐ŸŒŸ ์™œ ๋ฐฑ์—”๋“œ์— โ€˜Rust & Axumโ€™์„ ์„ ํƒํ•˜๋‚˜์š”?

Java/Go/Node.js๋Š” **๊ฐ€๋น„์ง€ ์ปฌ๋ ‰ํ„ฐ(GC)**๋กœ ์ธํ•ด ํŠธ๋ž˜ํ”ฝ ๊ธ‰์ฆ ์‹œ ๊ฐ„ํ—์ ์ธ Stop-the-World ์ง€์—ฐ(Tail Latency)์ด ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ˜๋ฉด Rust๋Š” ์ปดํŒŒ์ผ ํƒ€์ž„ ์†Œ์œ ๊ถŒ(Ownership)ยท๋ผ์ดํ”„ํƒ€์ž„ ๊ฒ€์ฆ์„ ํ†ตํ•ด **GC ์—†์ด๋„ ๋ฉ”๋ชจ๋ฆฌ ์•ˆ์ „์„ฑ(Memory Safety)**์„ ๋ณด์žฅํ•˜๋ฉฐ, C/C++ ์ˆ˜์ค€์˜ ์˜ˆ์ธก ๊ฐ€๋Šฅํ•œ p99 ์‘๋‹ต ์†๋„์™€ ๊ทน๋„๋กœ ๋‚ฎ์€ ๋ฉ”๋ชจ๋ฆฌ ์ ์œ ์œจ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.
**Axum(v0.8.9)**์€ Tokio ํŒ€์ด ์ง์ ‘ ๊ฐœ๋ฐœํ•œ ์›น ํ”„๋ ˆ์ž„์›Œํฌ๋กœ, ๋งคํฌ๋กœ ๋งˆ๋ฒ• ์—†์ด Rust ํƒ€์ž… ์‹œ์Šคํ…œ๊ณผ Tower ๋ฏธ๋“ค์›จ์–ด๋งŒ์œผ๋กœ ๊น”๋”ํ•˜๊ณ  ์•ˆ์ „ํ•œ API ์„œ๋ฒ„๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋น„๊ต ํ•ญ๋ชฉSpring Boot (Java)Node.js / ExpressRust + Axum (v0.8.9)
๋ฉ”๋ชจ๋ฆฌ ๊ด€๋ฆฌJVM GC (์ˆ˜๋ฐฑ MB ~ ์ˆ˜ GB)V8 GC (์ˆ˜๋ฐฑ MB)์†Œ์œ ๊ถŒ ๊ธฐ๋ฐ˜ ์ปดํŒŒ์ผ ํƒ€์ž„ ํ•ด์ œ (์ˆ˜ MB)
๋™์‹œ์„ฑ ๋ชจ๋ธThread / Virtual Thread๋‹จ์ผ ์Šค๋ ˆ๋“œ ์ด๋ฒคํŠธ ๋ฃจํ”„Tokio M:N ๋น„๋™๊ธฐ Work-Stealing ๋Ÿฐํƒ€์ž„
SQL ์•ˆ์ „์„ฑ๋Ÿฐํƒ€์ž„ ๋˜๋Š” JPA ๋งคํ•‘๋Ÿฐํƒ€์ž„ ์ฟผ๋ฆฌ ์‹คํ–‰SQLx ์ปดํŒŒ์ผ ํƒ€์ž„ SQLยทํƒ€์ž… ๊ฒ€์ฆ

1. ํ•„์ˆ˜ ์‚ฌ์ „ ์š”๊ฑด & ์‹œ์Šคํ…œ ์š”๊ตฌ์‚ฌ์–‘ (Prerequisites & System Requirements)

๐Ÿ’ป ์‹œ์Šคํ…œ ์„ค์น˜ ๋ฐ ์‹คํ–‰ ์š”๊ตฌ์‚ฌ์–‘ (System Requirements)

ํ•ญ๋ชฉ์ตœ์†Œ ์‚ฌ์–‘ (Minimum)๊ถŒ์žฅ ์‚ฌ์–‘ (Recommended)์ฐธ๊ณ  ๋ฐ ๋น„๊ณ 
์šด์˜์ฒด์ œ (OS)Windows 10/11 (MSVC/WSL2), macOS 13+, LinuxUbuntu 24.04 LTS / macOS Apple SiliconWindows๋Š” Visual Studio C++ Build Tools ํ•„์š”
CPU ์•„ํ‚คํ…์ฒ˜2์ฝ”์–ด ์ด์ƒ (x86_64 / ARM64)4์ฝ”์–ด ~ 8์ฝ”์–ด ์ด์ƒCargo ์ฆ๋ถ„ ์ปดํŒŒ์ผ ์†๋„ ํ–ฅ์ƒ
๋ฉ”๋ชจ๋ฆฌ (RAM)์ตœ์†Œ 4 GB ์ด์ƒ8 GB ~ 16 GB ์ด์ƒrust-analyzer + Cargo ๋ณ‘๋ ฌ ๋นŒ๋“œ
๋””์Šคํฌ (Storage)10 GB ์ด์ƒ์˜ ์—ฌ์œ  ๊ณต๊ฐ„20 GB+ NVMe SSDtarget/ ๋นŒ๋“œ ์•„ํ‹ฐํŒฉํŠธ ์ €์žฅ ๊ณต๊ฐ„
๋Ÿฐํƒ€์ž„ / ํˆด์ฒด์ธRust 1.100.0+ (rustup), Docker 26+cargo-watch, sqlx-cliAxum 0.8.9, SQLx 0.9.0, PostgreSQL 16+

๐Ÿ› ๏ธ Rust ํˆด์ฒด์ธ ์„ค์น˜ ๋ฐ ํ™•์ธ

# 1) rustup์œผ๋กœ ์ตœ์‹  ์•ˆ์ • Rust(1.100.0+) ์„ค์น˜
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
source "$HOME/.cargo/env"

# 2) ๋ฒ„์ „ ํ™•์ธ
rustc --version   # rustc 1.100.0
cargo --version

# 3) SQLx ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ CLI ์„ค์น˜ (์„ ํƒ)
cargo install sqlx-cli --no-default-features --features postgres

2. ๐Ÿง  ์•„ํ‚คํ…์ฒ˜ & ๋ฉ˜ํƒˆ ๋ชจ๋ธ (Mental Model)

HTTP Request โ”€โ”€> [Hyper HTTP/1.1 & HTTP/2 Server]
                        โ”‚
                        โ–ผ
                 [Tower Middleware Stack]  (TraceLayer, CORS, Timeout)
                        โ”‚
                        โ–ผ
                 [Axum Router]  (/api/orders/{id})
                        โ”‚
          โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
          โ–ผ             โ–ผ             โ–ผ
    [State<AppState>] [Path<i64>] [Json<CreateOrder>]   <-- Extractor (ํƒ€์ž… ์•ˆ์ „ ํŒŒ์‹ฑ)
          โ”‚             โ”‚             โ”‚
          โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                        โ–ผ
                 [Async Handler fn] โ”€โ”€> [SQLx PgPool] โ”€โ”€> [PostgreSQL 16]
                        โ”‚
                        โ–ผ
                 [Result<impl IntoResponse, AppError>]
  • Extractor ํŒจํ„ด: ํ•ธ๋“ค๋Ÿฌ ํ•จ์ˆ˜ ํŒŒ๋ผ๋ฏธํ„ฐ์— State(...), Path(...), Json(...)์„ ์„ ์–ธํ•˜๋ฉด Axum์ด ์š”์ฒญ์„ ์—ญ์ง๋ ฌํ™”ยท๊ฒ€์ฆํ•ด ์ฃผ์ž…ํ•˜๋ฉฐ, ์‹คํŒจ ์‹œ ์ž๋™์œผ๋กœ 400/422 ์‘๋‹ต์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

3. [Phase 1] ์†Œ์œ ๊ถŒ & Tokio ๋น„๋™๊ธฐ ํ•ต์‹ฌ 3๋ถ„ ์ •๋ฆฌ

Rust ๋ฐฑ์—”๋“œ์—์„œ ๊ฐ€์žฅ ์ž์ฃผ ๋งˆ์ฃผ์น˜๋Š” Arc<T>์™€ ๋น„๋™๊ธฐ ํƒœ์Šคํฌ ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค:

use std::sync::Arc;
use tokio::sync::RwLock;

#[derive(Clone)]
struct AppState {
    // ์—ฌ๋Ÿฌ Tokio ์›Œ์ปค ์Šค๋ ˆ๋“œ ๊ฐ„ ์•ˆ์ „ํ•˜๊ฒŒ ๊ณต์œ ๋˜๋Š” ๋ถˆ๋ณ€/๊ฐ€๋ณ€ ์ƒํƒœ
    counter: Arc<RwLock<u64>>,
}
  • Arc<T>(Atomic Reference Counting): ์—ฌ๋Ÿฌ ์š”์ฒญ ํ•ธ๋“ค๋Ÿฌ๊ฐ€ ๋™์ผํ•œ DB ํ’€์ด๋‚˜ ์„ค์ •์„ ๋ณต์‚ฌ ๋น„์šฉ ์—†์ด ๊ณต์œ ํ•  ๋•Œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
  • tokio::spawn: ๋น„๋™๊ธฐ ์ž‘์—…์„ ๋ฐฑ๊ทธ๋ผ์šด๋“œ ํƒœ์Šคํฌ๋กœ ์‹คํ–‰ํ•˜๋ฉฐ, 'static ์ˆ˜๋ช…์„ ์š”๊ตฌํ•˜๋ฏ€๋กœ Arc::clone()์œผ๋กœ ์†Œ์œ ๊ถŒ์„ ๋„˜๊น๋‹ˆ๋‹ค.

4. [Phase 2] Axum 0.8.9 ํ”„๋กœ์ ํŠธ ์ดˆ๊ธฐํ™” & ์ฒซ REST API ์„œ๋ฒ„

cargo new axum-order-api
cd axum-order-api

Cargo.toml์— ์ตœ์‹  ์•ˆ์ • ๋ฒ„์ „์„ ์„ ์–ธํ•ฉ๋‹ˆ๋‹ค:

[package]
name = "axum-order-api"
version = "0.1.0"
edition = "2024"

[dependencies]
axum = "0.8.9"
tokio = { version = "1", features = ["full"] }
serde = { version = "1", features = ["derive"] }
serde_json = "1"
sqlx = { version = "0.9.0", features = ["runtime-tokio", "postgres", "uuid", "chrono"] }
tower-http = { version = "0.6", features = ["trace", "cors"] }
tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }

์ฒซ ๋ฒˆ์งธ Axum 0.8 ์„œ๋ฒ„ (src/main.rs)

โš ๏ธ Axum 0.8 ๋ฌธ๋ฒ• ๋ณ€๊ฒฝ ์ฃผ์˜: ๊ฒฝ๋กœ ํŒŒ๋ผ๋ฏธํ„ฐ๋Š” ๊ธฐ์กด :id๊ฐ€ ์•„๋‹ˆ๋ผ {id} ์ค‘๊ด„ํ˜ธ ๋ฌธ๋ฒ•์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

use axum::{
    extract::{Path, State},
    http::StatusCode,
    response::IntoResponse,
    routing::{get, post},
    Json, Router,
};
use serde::{Deserialize, Serialize};
use std::sync::{Arc, atomic::{AtomicU64, Ordering}};

#[derive(Clone)]
struct AppState {
    req_count: Arc<AtomicU64>,
}

#[derive(Deserialize)]
struct CreateOrderReq {
    item: String,
    qty: u32,
}

#[derive(Serialize)]
struct OrderRes {
    id: u64,
    item: String,
    qty: u32,
    status: &'static str,
}

async fn health() -> &'static str {
    "OK"
}

async fn get_order(Path(id): Path<u64>) -> impl IntoResponse {
    Json(OrderRes { id, item: "mechanical-keyboard".into(), qty: 1, status: "PAID" })
}

async fn create_order(
    State(state): State<AppState>,
    Json(payload): Json<CreateOrderReq>,
) -> impl IntoResponse {
    let next_id = state.req_count.fetch_add(1, Ordering::Relaxed) + 1;
    (
        StatusCode::CREATED,
        Json(OrderRes { id: next_id, item: payload.item, qty: payload.qty, status: "CREATED" }),
    )
}

#[tokio::main]
async fn main() {
    let state = AppState { req_count: Arc::new(AtomicU64::new(0)) };

    let app = Router::new()
        .route("/health", get(health))
        .route("/api/orders", post(create_order))
        .route("/api/orders/{id}", get(get_order))
        .with_state(state);

    let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
    println!("Listening on http://0.0.0.0:3000");
    axum::serve(listener, app).await.unwrap();
}
cargo run
# ๋‹ค๋ฅธ ํ„ฐ๋ฏธ๋„์—์„œ ํ…Œ์ŠคํŠธ:
curl -s http://localhost:3000/health
curl -s -X POST http://localhost:3000/api/orders -H "Content-Type: application/json" -d '{"item":"ssd-2tb","qty":2}'

5. [Phase 3] SQLx 0.9.0 PostgreSQL ์—ฐ๋™ & ์ปค์Šคํ…€ ์—๋Ÿฌ ์ฒ˜๋ฆฌ

AppError ์—ด๊ฑฐํ˜•์— IntoResponse๋ฅผ ๊ตฌํ˜„ํ•˜๋ฉด ํ•ธ๋“ค๋Ÿฌ์—์„œ ? ์—ฐ์‚ฐ์ž๋งŒ์œผ๋กœ DB ์˜ค๋ฅ˜๋ฅผ ๊น”๋”ํ•œ JSON HTTP ์—๋Ÿฌ๋กœ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

use axum::{http::StatusCode, response::{IntoResponse, Response}, Json};
use serde_json::json;

pub enum AppError {
    NotFound,
    Db(sqlx::Error),
}

impl From<sqlx::Error> for AppError {
    fn from(err: sqlx::Error) -> Self {
        match err {
            sqlx::Error::RowNotFound => AppError::NotFound,
            other => AppError::Db(other),
        }
    }
}

impl IntoResponse for AppError {
    fn into_response(self) -> Response {
        let (status, msg) = match self {
            AppError::NotFound => (StatusCode::NOT_FOUND, "๋ฆฌ์†Œ์Šค๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."),
            AppError::Db(_) => (StatusCode::INTERNAL_SERVER_ERROR, "๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ฒ˜๋ฆฌ ์˜ค๋ฅ˜"),
        };
        (status, Json(json!({ "error": msg }))).into_response()
    }
}

6. [Phase 4~5] Graceful Shutdown & ์ดˆ๊ฒฝ๋Ÿ‰ Distroless Docker ๋ฐฐํฌ

์ฟ ๋ฒ„๋„คํ‹ฐ์Šค ๋กค๋ง ์—…๋ฐ์ดํŠธ ์‹œ ์ง„ํ–‰ ์ค‘์ธ ์š”์ฒญ์„ ๋Š์ง€ ์•Š๋„๋ก with_graceful_shutdown์„ ์—ฐ๊ฒฐํ•˜๊ณ , ๋‹ค๋‹จ๊ณ„(Multi-stage) ๋นŒ๋“œ๋กœ ~20MB ๋‹จ์ผ ์ •์  ๋ฐ”์ด๋„ˆ๋ฆฌ ์ด๋ฏธ์ง€๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

// main.rs ๋‚ด ์„œ๋ฒ„ ์ข…๋ฃŒ ์‹œ๊ทธ๋„ ์ฒ˜๋ฆฌ
axum::serve(listener, app)
    .with_graceful_shutdown(async {
        let _ = tokio::signal::ctrl_c().await;
        println!("Shutting down gracefully...");
    })
    .await
    .unwrap();
# Dockerfile (Multi-stage ์ดˆ๊ฒฝ๋Ÿ‰ ๋ฐฐํฌ)
FROM rust:1.100-slim-bookworm AS builder
WORKDIR /app
COPY . .
RUN cargo build --release

FROM gcr.io/distroless/cc-debian12
COPY --from=builder /app/target/release/axum-order-api /usr/local/bin/server
EXPOSE 3000
CMD ["/usr/local/bin/server"]

โœ… ํ”„๋กœ๋•์…˜ ์ฒดํฌ๋ฆฌ์ŠคํŠธ

  1. ๋น„๋™๊ธฐ ๋ธ”๋ก ๋‚ด ๋ธ”๋กœํ‚น ๊ธˆ์ง€: ๋ฌด๊ฑฐ์šด CPU ์—ฐ์‚ฐ์ด๋‚˜ ๋™๊ธฐ I/O๋Š” tokio::task::spawn_blocking์œผ๋กœ ๋ถ„๋ฆฌํ•ด ์ด๋ฒคํŠธ ๋ฃจํ”„ ์Šคํ†จ์„ ๋ฐฉ์ง€ํ•˜์„ธ์š”.
  2. Release ๋นŒ๋“œ: ๋ฒค์น˜๋งˆํฌ ๋ฐ ๋ฐฐํฌ ์‹œ ๋ฐ˜๋“œ์‹œ --release ํ”Œ๋ž˜๊ทธ๋กœ ์ตœ์ ํ™” ์ปดํŒŒ์ผํ•˜์„ธ์š”(๋””๋ฒ„๊ทธ ๋นŒ๋“œ๋Š” 10~30๋ฐฐ ๋А๋ฆฝ๋‹ˆ๋‹ค).
  3. Timeout & ConcurrencyLimit: tower_http::timeout::TimeoutLayer๋ฅผ ์ ์šฉํ•ด ๋А๋ฆฐ ํด๋ผ์ด์–ธํŠธ๋กœ๋ถ€ํ„ฐ ์›Œ์ปค ์ž์›์„ ๋ณดํ˜ธํ•˜์„ธ์š”.
๐Ÿ’ฌ

์˜๊ฒฌ ๋ฐ ์งˆ๋ฌธ

0
0 / 1500