Backend / Web frameworks / FastAPI / 00_fastapi_overview.md

FastAPI

Updated 5 interview angles 4 min read source
On this page7
  1. What it is built on
  2. The shape of a handler
  3. Why async, and the trap
  4. Lifespan, not startup events
  5. Where it fits against the alternatives
  6. This folder
  7. Interview angle

FastAPI

An ASGI framework whose one idea is that the type hints are the contract. Validation, serialisation, dependency injection and the OpenAPI schema are all derived from the signature, which is why a FastAPI handler is short and why a sloppy signature produces a sloppy public API.

What it is built on

Layer Does
Starlette ASGI, routing, middleware, WebSockets, TestClient
Pydantic validation, serialisation, the JSON Schema
FastAPI dependency injection, OpenAPI, the glue

Worth knowing, because half of “FastAPI” questions are really Starlette questions — middleware, background tasks and Request all come from there — and the other half are Pydantic questions.

The shape of a handler

python
@app.post("/orders", response_model=OrderOut, status_code=201)
async def create(
    body: OrderIn,
    session: Annotated[AsyncSession, Depends(get_session)],
    user: Annotated[User, Depends(current_user)],
) -> OrderOut:
    order = await service.create(session, user, body)
    return order

Four things happened without you writing them: the body was parsed and validated into OrderIn, the session and user were constructed and injected, the return value was validated against OrderOut, and the whole thing appeared in /docs with a schema. That is the entire pitch.

Where each parameter comes from is inferred, which is the rule to state:

Parameter Source
Named in the path path parameter
A Pydantic model request body
A scalar not in the path query parameter
Depends(...) dependency injection
Request, Response the raw Starlette objects

Why async, and the trap

FastAPI runs on an event loop, so a def handler goes to a threadpool and an async def handler runs on the loop. Blocking inside async def stalls every concurrent request, which is the single most common FastAPI performance bug and has its own note: FastAPI and the event loop.

Lifespan, not startup events

python
@asynccontextmanager
async def lifespan(app: FastAPI):
    app.state.http = httpx.AsyncClient(timeout=5)
    yield
    await app.state.http.aclose()

app = FastAPI(lifespan=lifespan)

@app.on_event("startup") is deprecated. Lifespan is one function with the teardown next to the setup, which is what makes it hard to leak a connection pool. Everything expensive and long-lived is built here — see Writing a client for an external API.

Where it fits against the alternatives

Reach for
JSON API, async I/O, typed FastAPI
Admin, ORM, auth, templates included Django + DRF
Small, sync, minimal Flask
Layered DI, msgspec, one codebase Litestar

The honest comparison with DRF is not performance — it is that Django brings a batteries-included ecosystem you either need or carry, while FastAPI brings a type-driven core you assemble around. See Django vs Django REST Framework (DRF) Guide and Litestar.

This folder

# File Covers
01 FastAPI Basics and Setup - Interview Questions routing, parameters, first app
02 Pydantic models in FastAPI request and response models
03 Dependencies and injection Depends in depth
04 Async and performance concurrency and throughput
05 Security and authentication OAuth2, JWT, scopes
06 Testing a FastAPI app TestClient, async tests, overrides
07 Middleware ordering, ASGI vs BaseHTTP
08 FastAPI project structure laying out a real service
09 FastAPI — Common Interview Questions and Answers assorted follow-ups
10 FastAPI and the event loop the event-loop model
11 Outbound auth, logging and caching outbound auth, logging, caching
13 Exception handling one error shape
14 Writing a client for an external API writing the client
15 FastAPI: sync code in an async route, run_in_threadpool escaping the loop safely
16 BackgroundTasks vs Celery — Decision Matrix when the request ends
17 response_model — Advanced Patterns what the contract guarantees
18 OpenAPI and the generated docs the generated schema
19 Deployment uvicorn, workers, containers

Interview angle 5

  • “What is FastAPI built on?” - Starlette for ASGI, routing, middleware and WebSockets; Pydantic for validation and serialisation. FastAPI adds dependency injection and OpenAPI generation. Half of “FastAPI” questions are really Starlette or Pydantic questions.
  • “How does it know where a parameter comes from?” - by inference from the signature: named in the path is a path parameter, a Pydantic model is the body, a bare scalar is a query parameter, and Depends is injection. The type hints are the contract, which is the whole design.
  • “Why is FastAPI fast?” - it is ASGI, so an async def handler that awaits I/O frees the worker to serve another request. Not because Python got faster — and the benefit disappears the moment you block inside async def.
  • “How do you manage a connection pool across requests?” - a lifespan context manager: build it before yield, close it after. @app.on_event("startup") is deprecated, and lifespan keeps the teardown next to the setup so it is hard to leak.
  • “FastAPI or Django REST Framework?” - DRF when you want the Django ecosystem: admin, ORM, auth, migrations, templates. FastAPI when the service is a typed JSON API over async I/O and you would rather assemble the pieces than carry the ones you do not use.