0
0
FastAPIframework~8 mins

Including routers in main app in FastAPI - Performance & Optimization

Choose your learning style9 modes available
Performance: Including routers in main app
MEDIUM IMPACT
This affects the initial server startup time and the routing lookup speed during request handling.
Organizing API endpoints in a FastAPI application
FastAPI
from fastapi import FastAPI, APIRouter

items_router = APIRouter()

@items_router.get('/items')
def read_items():
    return {'items': []}

users_router = APIRouter()

@users_router.get('/users')
def read_users():
    return {'users': []}

app = FastAPI()
app.include_router(items_router)
app.include_router(users_router)
Routes are grouped logically in routers and included in the main app, improving code organization and allowing FastAPI to optimize routing internally.
📈 Performance GainRouting lookup is more efficient with modular routers; startup time is better managed; code is easier to maintain.
Organizing API endpoints in a FastAPI application
FastAPI
from fastapi import FastAPI
app = FastAPI()

@app.get('/items')
def read_items():
    return {'items': []}

@app.get('/users')
def read_users():
    return {'users': []}
All routes are defined directly on the main app, which can make the app large and routing slower as the number of endpoints grows.
📉 Performance CostRouting lookup time increases linearly with number of routes; can cause slower request handling for large apps.
Performance Comparison
PatternRouting Table SizeStartup TimeRequest Routing SpeedVerdict
All routes on main appLarge (all routes in one table)Longer (registers all routes at once)Slower (linear search through routes)[X] Bad
Routes organized in routersSmaller per router, combined efficientlyShorter (modular registration)Faster (optimized routing lookup)[OK] Good
Rendering Pipeline
When the FastAPI app starts, it registers all routes from included routers into its internal routing table. During a request, the routing system matches the URL path to the correct endpoint handler.
Startup Initialization
Routing Lookup
Request Handling
⚠️ BottleneckRouting Lookup stage can become slower if too many routes are registered directly without modular grouping.
Optimization Tips
1Group related routes using APIRouter to keep routing tables manageable.
2Include routers in the main app to improve routing lookup speed.
3Avoid defining all routes directly on the main app to prevent slower request handling.
Performance Quiz - 3 Questions
Test your performance knowledge
What is a main performance benefit of including routers in a FastAPI main app?
AImproves routing lookup speed by grouping routes
BReduces network latency between client and server
CDecreases database query time
DAutomatically caches responses
DevTools: Network and Logs
How to check: Use server logs to measure startup time and response time for requests; use profiling tools to check routing lookup performance.
What to look for: Look for lower startup time and faster response times when using routers; check logs for routing errors or delays.