How FastrAPI works
FastrAPI is a high-performance Python web framework that combines the speed of Rust with the simplicity of Python. It achieves up to 33x faster performance than traditional Python frameworks by leveraging Rust’s runtime efficiency while maintaining a familiar Python API.Core components
Python-Rust bridge via PyO3
FastrAPI uses PyO3 to create seamless Python bindings for Rust code. This allows you to write Python code that executes Rust functions with minimal overhead.#[pymodule] macro creates a Python module from Rust code, and the gil_used = false flag indicates that FastrAPI minimizes Python’s Global Interpreter Lock (GIL) usage for better concurrency.
Axum web framework
Under the hood, FastrAPI uses Axum, a fast and ergonomic Rust web framework built on top of Hyper and Tower. Axum handles:- HTTP request parsing
- Routing to handlers
- Response serialization
- Middleware execution
Tokio async runtime
FastrAPI runs on Tokio, Rust’s premier async runtime. This enables:- High concurrency: Handle thousands of concurrent connections
- Non-blocking I/O: Efficient resource utilization
- Thread pool management: Optimal CPU usage
Request lifecycle
Here’s how a request flows through FastrAPI:- Request arrives: Tokio receives the TCP connection
- Axum routing: Request is matched to a route using O(1) hashmap lookup
- Middleware: Request passes through middleware layers (CORS, sessions, etc.)
- Handler execution: Python function is called in a dedicated thread pool
- Response conversion: Python return value is converted to HTTP response
- Response sent: Axum sends the response back to the client
Route storage
FastrAPI uses a concurrent hashmap for blazing-fast route lookups:"GET /users/{id}" for O(1) lookup time, regardless of how many routes your application has.
Performance optimizations
Fast path detection
FastrAPI automatically detects simple routes that don’t need parameter validation or dependency injection:Pre-computed dependency graphs
Dependencies are analyzed once at decorator time, not on every request:Zero-copy operations
Where possible, FastrAPI avoids copying data between Rust and Python, using references and shared memory.Comparison with FastAPI
| Component | FastAPI | FastrAPI |
|---|---|---|
| Web server | Uvicorn (Python) | Axum (Rust) |
| Runtime | asyncio | Tokio |
| Route lookup | Regex matching | Hashmap O(1) |
| Dependency resolution | Runtime inspection | Pre-computed at decorator time |
| JSON serialization | Python json module | Rust serde_json |
| Concurrency | asyncio event loop | Native async/await with thread pool |
FastrAPI maintains API compatibility with FastAPI while delivering 33x better performance through Rust’s systems-level optimizations.
Memory safety
Rust’s ownership system ensures:- No data races: Compile-time prevention of concurrent access bugs
- No null pointer dereferences: Option types prevent null errors
- No buffer overflows: Bounds checking on all array access
Next steps
Routing
Learn how routes are defined and matched
Request handling
Understand request processing and async execution