What Makes FastAPI Fast?
What Is a Web API? (Before FastAPI)
A web API is simply a way for one program to talk to another program over the internet.
Example:
- Your mobile app asks a server: "Give me my profile"
- The server runs some Python code
- The server sends back JSON data
You are not opening a web page. You are exchanging data.
So an API does three basic things:
- Receives a request
- Runs code
- Sends a response
FastAPI exists to help you do exactly this.
What Is FastAPI?
FastAPI is a Python library for building web APIs.
At the simplest level, it lets you write code like this:
@app.get("/hello")
def hello():
return {"message": "Hello"}
And that code becomes an API endpoint that:
- Listens on
/hello - Runs your function
- Returns JSON to the client
So far, this looks very similar to Flask or Django.
So the natural question is:
Why do we need FastAPI if other frameworks already do this?
To answer that, we must understand how requests are handled internally.
How a Python Server Handles Requests (Conceptually)
When 1 user sends a request, life is easy.
But when 1000 users send requests at the same time, the server must decide:
- Who runs first?
- Who waits?
- What happens when code is waiting for a database or another service?
This is where web frameworks differ.
The Old Model — Blocking Servers (WSGI)
Older Python frameworks (Flask, Django) are built on a standard called WSGI.
You do not need to remember the name yet. Just remember how it behaves.
How WSGI Servers Work
- The server has a fixed number of workers
- Each worker handles one request at a time
- The worker is busy until the request finishes
Example:
def get_profile():
user = get_user_from_db() # waits 200 ms
data = call_external_api() # waits 800 ms
return data
Total waiting time ≈ 1 second.
During this time:
- The CPU is mostly idle
- But the worker cannot do anything else
This is called blocking.
Why Blocking Is a Problem
If you have:
- 4 workers
- 100 users
Then:
- Only 4 users are processed at a time
- 96 users are waiting
The server is wasting time while waiting for I/O.
The Modern Model — Non-Blocking Servers (ASGI)
Modern frameworks use a different model called non-blocking execution.
FastAPI is built on this model.
The Core Idea
When your code is waiting for something:
- Pause this request
- Work on another request
This is possible using async programming.
What “async” Really Means
async does not mean faster code.
It means:
- "This function may wait"
- "While waiting, do other work"
Example:
async def get_profile():
user = await get_user_from_db()
data = await call_external_api()
return data
When await is reached:
- The function pauses
- Control goes back to the server
The server immediately switches to another request.
This model is called ASGI.
Why FastAPI Is Fast (Now It Makes Sense)
At this point, you finally have enough context to answer the question:
Why is FastAPI fast?
FastAPI is not fast because of clever tricks or micro-optimizations.
FastAPI is fast because it is built entirely on the non-blocking model you just learned.
Let’s connect the dots.
1. FastAPI Does Not Block While Waiting
In a FastAPI application:
- Requests are written as
asyncfunctions - I/O operations use
await - When waiting, the request pauses instead of blocking
This means:
- The server never sits idle during database or network waits
- One worker can handle many requests at the same time
This alone removes the biggest performance bottleneck of older frameworks.
2. FastAPI Uses an Event Loop Correctly
FastAPI runs on an event loop.
The event loop:
- Starts a request
- Pauses it at
await - Switches to another request
- Resumes it later
This switching is extremely lightweight compared to threads or processes.
Result:
- Low memory usage
- High concurrency
- Stable latency under load
3. FastAPI Is Built on Minimal Layers
FastAPI is built on Starlette, a very small and fast ASGI toolkit.
Starlette handles:
- Routing
- Middleware
- Request and response objects
FastAPI adds:
- Data validation
- Dependency injection
- Documentation generation
There is no heavy framework machinery in the request path.
What you write is very close to what actually runs.
4. FastAPI Avoids Manual, Repetitive Work
In many frameworks, developers write:
- JSON parsing
- Validation logic
- Error formatting
FastAPI uses Pydantic to do this efficiently and consistently.
This means:
- Data is parsed once
- Types are enforced centrally
- Errors are generated automatically
This reduces both:
- CPU work
- Programmer mistakes
The Key Insight
FastAPI feels fast because:
- It does not waste time waiting
- It does not waste memory on idle workers
- It does not add unnecessary layers
FastAPI is fast by architecture, not by accident.
What Is an Event Loop?
An event loop is the component that manages this switching.
Think of it like a scheduler:
- It runs one request
- Pauses it when it waits
- Resumes it later
One thread. Many requests. No blocking.
This is how FastAPI handles thousands of users efficiently.
Where FastAPI Fits
FastAPI sits on top of this system.
A real request flow looks like this:
Client → Server → Event Loop → FastAPI → Your Function
FastAPI is responsible for:
- Mapping URLs to functions
- Reading request data
- Validating input
- Calling your code
- Returning responses
It does not manage networking or threads.
What FastAPI Is Built On
FastAPI uses other tools internally.
You do not need to learn them immediately, but understanding their roles helps.
- Uvicorn → the server that accepts connections
- Starlette → low-level request/response handling
- Pydantic → data validation
FastAPI combines them into a single, clean interface.
Automatic Data Validation (Why This Matters)
FastAPI uses Python type hints as rules, not comments.
Example:
class User(BaseModel):
id: int
name: str
@app.post("/users")
def create_user(user: User):
return user
FastAPI automatically:
- Parses JSON
- Validates types
- Rejects invalid requests
- Returns clear error messages
You do not write validation code.
Automatic API Documentation
Because FastAPI understands your data:
- It generates API documentation automatically
- It creates an OpenAPI specification
- It provides an interactive UI
Your documentation stays correct by construction.
Sync and Async Together
FastAPI allows both:
def sync_fn():
time.sleep(2)
async def async_fn():
await asyncio.sleep(2)
Internally:
- Sync code runs safely in a thread pool
- Async code runs on the event loop
You do not block the server accidentally.
When FastAPI Is the Right Choice
FastAPI is ideal for:
- APIs
- Microservices
- ML model serving
- High-concurrency systems
Avoid it if:
- You want server-rendered HTML pages
- You need a built-in admin panel
- Your work is purely CPU-heavy
Final Mental Model
FastAPI is:
- A Python API framework
- Built on non-blocking execution
- Designed for modern workloads
It feels fast because:
- It does not waste time waiting
- It uses async correctly
- It enforces clear data contracts
Once you understand this model, everything else in FastAPI makes sense.