What This System Solves: Processes high-volume e-commerce orders and inventory updates quickly without double-selling contested stock or losing background fulfillment events.
High-concurrency Rust backend engineered with Axum, compile-time verified SQLx queries, atomic transactional inventory deductions (
FOR UPDATE), multi-layered Redis caching, and asynchronous background job queues.
graph TD
Client["Client Traffic / k6 Load Tests"] -->|HTTP / TLS| NGINX["NGINX Reverse Proxy / Load Balancer"]
NGINX -->|Reverse Proxy :8080| Axum["Axum HTTP Engine (Tokio Threadpool)"]
subgraph Core_Services ["Core Domain Services"]
Axum -->|Extract & Verify Auth| Auth["JWT Auth Middleware"]
Axum -->|Cache-Aside Pattern| Redis[("Redis Connection Pool")]
Axum -->|Transactional ACID Writes| PG[("PostgreSQL Primary DB")]
Axum -->|Transactional Outbox Pattern| Outbox[("Outbox Table")]
end
subgraph Async_Workers ["Asynchronous Workers"]
Relay["Outbox Relay Worker"] -->|Poll Pending Events| Outbox
Relay -->|Publish Events| Kafka["Kafka Event Stream / Notification Pipeline"]
end
sequenceDiagram
autonumber
actor Customer as Shopper
participant API as Axum Gateway (:8080)
participant Cache as Redis Cluster (:6380)
participant DB as PostgreSQL Primary (:5434)
participant Outbox as Transactional Outbox
Customer->>API: POST /api/v1/orders/checkout (items, address)
Note over API: Begin ACID Transaction (pool.begin())
API->>DB: SELECT inventory_count FROM products WHERE id = $1 FOR UPDATE
Note over DB: Row-level exclusive lock prevents race conditions
alt Insufficient Inventory
API-->>Customer: 409 Conflict { error: "Item out of stock" }
else Stock Available
API->>DB: UPDATE products SET inventory_count = inventory_count - 1 WHERE id = $1
API->>DB: INSERT INTO orders (id, user_id, total, status) VALUES (...)
API->>Outbox: INSERT INTO outbox (aggregate_type, event_type, payload) VALUES (...)
API->>DB: COMMIT TRANSACTION
Note over API,DB: Atomically committed order + inventory + outbox event
API->>Cache: Invalidate / Evict product cache key
API-->>Customer: 201 Created { orderId, status: "confirmed" }
end
| Architectural Decision | Production Alternative Evaluated | Tradeoff Rationale |
|---|---|---|
Pessimistic Row Locking (FOR UPDATE) |
Optimistic Concurrency Control (Version checks) | Flash sale item contention causes high abort/retry rates under OCC. Row-level locks in PostgreSQL guarantee zero overselling with sub-millisecond lock hold times. |
Compile-time SQL verification (sqlx::query!) |
Traditional ORM (Diesel / SeaORM) | Eliminates runtime query syntax and type mismatch bugs before binary compilation without ORM abstraction overhead. |
| Transactional Outbox Pattern | Direct dual-writes (DB + Message Broker) | Eliminates distributed dual-write inconsistency where a database commit succeeds but broker write fails (or vice versa). |
| Connection Pooling with Deadpool & BB8 | Single multiplexed connection | Dynamically manages connection lifecycle, preventing socket exhaustion during traffic spikes while bounding PostgreSQL memory footprint. |
Validated via automated unit tests and high-concurrency simulation proving zero oversell and memory safety.
$ SQLX_OFFLINE=true cargo test -- --nocapture
running 6 tests
test models::order::tests::test_order_status_variants ... ok
test models::order::tests::test_order_subtotal_calculation ... ok
test models::product::tests::test_invalid_empty_name ... ok
test models::product::tests::test_valid_product_request ... ok
test models::product::tests::test_invalid_negative_inventory ... ok
test models::product::tests::test_invalid_negative_price ... ok
test result: ok. 6 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s| Benchmark Phase | Cluster Setup | Measured Result | Reference Doc |
|---|---|---|---|
V3 Peak Throughput (3,000 VUs) |
4 x Hetzner cx23 (nbg1), Nginx least_conn |
4,665.84 RPS across 307,483 requests (0.00% error) |
docs/v3_hetzner_benchmark_results.md |
V4 Mixed Workload (2,000 VUs) |
3x Axum + Redis async queue + PG | 28.38ms overall P95 (21ms catalog · 103ms order write) |
docs/v4_benchmark_results.md |
docker compose up -d postgres rediscp .env.example .env
cargo run --releaseServer binds to http://localhost:8080.
docker build -t rust-ecommerce-backend:latest .
docker run -p 8080:8080 --env-file .env rust-ecommerce-backend:latest