PyPI version Status GitHub stars Downloads

What is TurboRouter?

Architecture Context

TurboRouter is an optional optimization layer on top of the Rust pipeline (which is always active). The Rust pipeline handles JSON streaming; TurboRouter adds query pre-registration to skip GraphQL parsing.

TurboRouter pre-registers GraphQL queries with their SQL templates, bypassing GraphQL parsing and validation for known queries. Combined with the Rust pipeline's JSON passthrough, this eliminates virtually all overhead between database and client.

Stage Basic Python GraphQL TurboRouter + Table Views
Query Parsing 0.5ms ✅ 0ms (pre-registered)
Schema Validation 0.3ms ✅ 0ms (pre-validated)
Query Planning 0.2ms ✅ 0ms (pre-planned)
Hash Lookup (APQ) - 0.05ms
PostgreSQL Query 15-30ms (JOINs) 0.3-0.8ms (table view)
Deserialize → Python Objects 2-8ms ✅ 0ms (skipped)
Serialize Python Objects → JSON 2-6ms ✅ 0ms (skipped)
Rust Field Selection + Response - ✅ ~0.1ms
Total 20-45ms ✅ ~0.5-1ms

Performance depends on query complexity and data size. The key speedup comes from table views (pre-computed JSONB) combined with TurboRouter's query pre-registration. Against optimized stacks with Redis caching, expect 2-3x improvement.

How It Works

1. Register Your Query

from fraiseql.fastapi import TurboQuery, TurboRegistry

registry = TurboRegistry()

user_query = TurboQuery(
    graphql_query="""
        query GetUser($id: ID!) {
            user(id: $id) {
                id
                name
                email
                posts {
                    title
                    publishedAt
                }
            }
        }
    """,
    sql_template="""
        SELECT jsonb_build_object(
            'user', jsonb_build_object(
                'id', u.id,
                'name', u.data->>'name',
                'email', u.data->>'email',
                'posts', COALESCE(
                    jsonb_agg(...)
                )
            )
        ) as result
        FROM tb_user u
        LEFT JOIN tb_post p ON p.fk_user = u.pk_user
        WHERE u.id = %(id)s::uuid
        GROUP BY u.id
    """,
    param_mapping={"id": "id"}
)

registry.register(user_query)

2. JSON Streams Directly to Client

# PostgreSQL returns JSONB:
{
  "user": {
    "id": 123,
    "name": "Alice",
    "email": "alice@example.com",
    "posts": [...]
  }
}

# TurboRouter: Stream it directly!
# ❌ No Python object creation
# ❌ No serialization overhead
# ✅ Zero-copy passthrough

# Application overhead: ~0.1ms ⚡

Benefits

4-10x Faster Responses

Eliminate Python object instantiation and JSON serialization overhead

🎯

Zero-Copy Architecture

PostgreSQL JSONB flows directly to client without intermediate transformations

💾

Lower Memory Footprint

No temporary Python objects allocated—reduces GC pressure significantly

🏎️

Maximum Throughput

Handle 2-5x more requests per second with the same hardware

Configuration

# Enable in production
app = create_fraiseql_app(
    database_url="postgresql://...",
    types=[User, Post],
    production=True,  # Enables TurboRouter
)

# Configure via environment
FRAISEQL_ENABLE_TURBO_ROUTER=true
FRAISEQL_TURBO_ROUTER_CACHE_SIZE=2000

# Or via config object
config = FraiseQLConfig(
    enable_turbo_router=True,
    turbo_router_cache_size=5000
)

Perfect For

Ready for Production Speed?

TurboRouter is a core feature of FraiseQL