0
0
FastAPIframework~8 mins

Async HTTP client calls in FastAPI - Performance & Optimization

Choose your learning style9 modes available
Performance: Async HTTP client calls
HIGH IMPACT
This affects how quickly the server can handle multiple HTTP requests without blocking, improving response time and throughput.
Making multiple HTTP requests to external APIs in a FastAPI endpoint
FastAPI
import httpx
import asyncio
from fastapi import FastAPI

app = FastAPI()

@app.get('/data')
async def get_data():
    async with httpx.AsyncClient() as client:
        response1, response2 = await asyncio.gather(
            client.get('https://api.example.com/one'),
            client.get('https://api.example.com/two')
        )
    return {'one': response1.json(), 'two': response2.json()}
Async HTTP client allows concurrent requests without blocking, improving response time and server throughput.
📈 Performance GainNon-blocking calls, parallel requests reduce total wait time, improves INP
Making multiple HTTP requests to external APIs in a FastAPI endpoint
FastAPI
import requests
from fastapi import FastAPI

app = FastAPI()

@app.get('/data')
def get_data():
    response1 = requests.get('https://api.example.com/one')
    response2 = requests.get('https://api.example.com/two')
    return {'one': response1.json(), 'two': response2.json()}
The requests library is synchronous and blocks the server while waiting for each HTTP call, causing slow response and poor concurrency.
📉 Performance CostBlocks event loop, increases response time linearly with each request, reduces throughput
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
Synchronous HTTP calls (requests)N/A (server-side)Blocks event loopDelays response rendering[X] Bad
Async HTTP calls with httpx.AsyncClientN/A (server-side)Non-blocking event loopFaster response rendering[OK] Good
Rendering Pipeline
Async HTTP calls in FastAPI do not block the event loop, allowing the server to handle other requests while waiting for external responses. This reduces server-side waiting time and improves responsiveness.
Server Request Handling
Event Loop
Network I/O
⚠️ BottleneckBlocking synchronous HTTP calls cause the event loop to wait, delaying all other tasks.
Core Web Vital Affected
INP
This affects how quickly the server can handle multiple HTTP requests without blocking, improving response time and throughput.
Optimization Tips
1Avoid synchronous HTTP calls in async FastAPI endpoints to prevent blocking.
2Use httpx.AsyncClient with asyncio.gather for concurrent HTTP requests.
3Monitor server event loop responsiveness to ensure non-blocking behavior.
Performance Quiz - 3 Questions
Test your performance knowledge
Why are synchronous HTTP client calls in FastAPI endpoints bad for performance?
AThey use too much memory.
BThey block the event loop, delaying other requests.
CThey cause layout shifts in the browser.
DThey increase CSS selector complexity.
DevTools: Network and Performance panels
How to check: Use the Network panel to observe request timing and the Performance panel to see if the main thread is blocked during HTTP calls.
What to look for: Look for long blocking times or queued tasks in the Performance panel indicating synchronous blocking; shorter wait times and parallel requests indicate good async usage.