How does lifecycle differ for global vs path operation dependencies?

Per-request scoping in FastAPI.
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's really being asked
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.
The full answer
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.
The mistakes people make
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.
What usually comes next
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.
A 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.
Interview question
You move a dependency that loads a 2GB ML model from a global FastAPI app dependency to a single path operation. What is the actual effect?
- a.It runs once at startup for that path only, eliminating per-request overhead while keeping it local to the route.
- b.It runs on every request to any endpoint, but FastAPI automatically caches the loaded model across requests.
- c.It stops the model from loading on unrelated routes, so the performance issue is resolved without needing a lifespan event.
- d.It runs only when that specific path is hit, yet still reloads the model on every request to that path.Correct
Why? this is the answer
Path-local dependencies are resolved per-request, so the expensive load repeats on every call to that route; moving it merely limits blast radius. Distractor D is wrong because limiting the dependency to one route does not eliminate per-request overhead, which is why expensive initialization belongs in a lifespan event or cached singleton.
Just read this? Test yourself on what you have been reading.
Read the original → fastapi.tiangolo.com
- #fastapi
- #dependency-injection
- #python
- #performance
- #lifespan-events
You just looked this up. Could you explain it out loud?
That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on fastapi — each one lists the topics its interview covers.
See open roles