Skip to main content

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.
// src/lib.rs
use pyo3::prelude::*;

#[pymodule(gil_used = false)]
fn fastrapi(m: &Bound<'_, PyModule>) -> PyResult<()> {
    m.add_class::<FastrAPI>()?;
    responses::register(m)?;
    request::register(m)?;
    // ... more module registrations
    Ok(())
}
The #[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
// src/server.rs
use axum::{
    routing::*,
    Router,
};

fn build_router(app_state: AppState, app_config: &FastrAPI) -> Router {
    let mut app = Router::new();
    
    // Register routes from Python decorators
    for (route_key, handler) in ROUTES.iter() {
        app = register_route(app, method, path, route_key, app_state);
    }
    
    app
}

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
// src/server.rs
static PYTHON_RUNTIME: Lazy<tokio::runtime::Runtime> = Lazy::new(|| {
    tokio::runtime::Builder::new_multi_thread()
        .worker_threads(num_cpus::get().max(4).min(16))
        .thread_name("python-handler")
        .enable_all()
        .build()
        .expect("Failed to create Python runtime")
});
The runtime automatically scales based on available CPU cores (4-16 threads) and uses a dedicated thread pool for Python handler execution.

Request lifecycle

Here’s how a request flows through FastrAPI:
  1. Request arrives: Tokio receives the TCP connection
  2. Axum routing: Request is matched to a route using O(1) hashmap lookup
  3. Middleware: Request passes through middleware layers (CORS, sessions, etc.)
  4. Handler execution: Python function is called in a dedicated thread pool
  5. Response conversion: Python return value is converted to HTTP response
  6. Response sent: Axum sends the response back to the client

Route storage

FastrAPI uses a concurrent hashmap for blazing-fast route lookups:
// src/lib.rs
pub static ROUTES: Lazy<PapayaHashMap<String, RouteHandler>> =
    Lazy::new(|| PapayaHashMap::with_capacity(128));

pub struct RouteHandler {
    pub func: Py<PyAny>,
    pub is_async: bool,
    pub is_fast_path: bool,
    pub param_validators: Vec<(String, Py<PyAny>)>,
    pub response_type: ResponseType,
    pub dependencies: Vec<DependencyInfo>,
    // ... more fields
}
Routes are stored with keys like "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:
@app.get("/")
def hello():  # Fast path: no parameters
    return {"Hello": "World"}

@app.get("/user/{id}")
def get_user(id: int):  # Slow path: has parameters
    return {"id": id}
Fast-path routes bypass validation and dependency resolution for maximum speed.

Pre-computed dependency graphs

Dependencies are analyzed once at decorator time, not on every request:
// Computed when @app.get() is applied
let (dependencies, is_async, is_fast_path) = 
    parse_route_metadata(py, func_bound, &path);

let handler = RouteHandler {
    dependencies,  // Pre-computed injection plan
    // ...
};

Zero-copy operations

Where possible, FastrAPI avoids copying data between Rust and Python, using references and shared memory.

Comparison with FastAPI

ComponentFastAPIFastrAPI
Web serverUvicorn (Python)Axum (Rust)
RuntimeasyncioTokio
Route lookupRegex matchingHashmap O(1)
Dependency resolutionRuntime inspectionPre-computed at decorator time
JSON serializationPython json moduleRust serde_json
Concurrencyasyncio event loopNative 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
This makes FastrAPI more reliable and secure than pure Python frameworks.

Next steps

Routing

Learn how routes are defined and matched

Request handling

Understand request processing and async execution

Build docs developers (and LLMs) love