tezvyn:

How does lifecycle differ for global vs path operation dependencies?

AI-drafted, machine-checkedSource: fastapi.tiangolo.comadvanced
How does lifecycle differ for global vs path operation dependencies?
WHAT IT TESTS

Per-request scoping in FastAPI.

ANSWER OUTLINE

Global deps run on every request to any route; path-local deps run only for that route; expensive setup belongs in a lifespan event or cached singleton, not a dependency.

WHAT THIS TESTS: This question probes your mental model of FastAPI's dependency injection scoping and lifecycle. Interviewers want to see that you know dependencies are resolved per-request by default, not at import time or startup, and that global versus path-local declarations change which requests trigger that work. They also care whether you can spot a performance anti-pattern and know the idiomatic FastAPI solution.

A GOOD ANSWER COVERS: A strong answer starts by stating that both global and path-local dependencies execute fresh on every request unless external caching is used. It then contrasts the two: a global dependency declared on the FastAPI app instance runs for every incoming request to every endpoint in the application, while a path-local dependency runs only when that specific path operation is hit. Next, it flags the resource-intensive setup as problematic in either location because it blocks the event loop or worker thread per request. Finally, it proposes the correct architecture: perform the expensive initialization in a lifespan event or a module-level cached singleton, and use the dependency only to inject a lightweight handle or client that was already created.

COMMON WRONG ANSWERS: Candidates sometimes claim that global dependencies run once at application startup, confusing them with lifespan events or module imports. Others assume FastAPI caches or memoizes dependency return values across requests automatically. Another red flag is suggesting that moving the dependency from global to path-local fixes the performance issue; while it limits blast radius, it still repeats the expensive work on every call to that route.

LIKELY FOLLOW-UPS: The interviewer may ask how you would share an expensive database connection pool or ML model across requests. They might also ask about dependencies with yield and whether teardown differs between global and path-local scopes, or how to test a dependency that relies on a lifespan-managed resource.

ONE CONCRETE EXAMPLE: Imagine a dependency that loads a two-gigabyte machine-learning model into memory. If you attach it as a global dependency on the app, every single request to any health check, metrics, or API endpoint pays the load cost and memory spike. If you attach it to one prediction endpoint, only that route suffers, but it still reloads the model on every prediction call. The right approach is to load the model in a lifespan event or an LRU-cached factory function at module import, then inject a lightweight predictor class that simply references the already-loaded weights.

Source: fastapi.tiangolo.com

Read the original → fastapi.tiangolo.com

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.