How does FastAPI cache dependencies within a single request?
Tests if you know FastAPI caches a dependency after the first call in a request and reuses it across the tree. A strong answer covers default use_cache=True, request-scoped lifetime, and disabling it.
What's really being asked
Whether you understand FastAPI's dependency injection resolution model beyond basic decorator usage. Specifically, it checks if you know that dependencies are resolved per request and that FastAPI avoids redundant work by caching the result of a dependency after its first execution within a single request's dependency tree. The interviewer wants to see that you grasp the difference between declaration and execution, and that you understand the framework optimizes dependency resolution automatically without manual intervention.
The full answer
First, state that by default FastAPI executes a dependency once per request and caches the return value for reuse within that request's dependency tree. Second, mention that this behavior is controlled by the use_cache parameter on Depends which defaults to True. Third, clarify that the cache is strictly scoped to a single request and is not shared across concurrent or subsequent requests, so each incoming request gets its own fresh resolution. Fourth, note that you can force re-execution by setting use_cache=False when a dependency is not idempotent or must run fresh each time it is injected, such as for certain audit or timestamp operations.
The mistakes people make
A major red flag is claiming the dependency runs every time it is declared in the tree. Another mistake is asserting the cache is global across all requests or threads. Some candidates forget that caching only applies within one request lifecycle and does not persist between requests. Suggesting manual memoization or singleton patterns instead of FastAPI's built-in request-scoped mechanism is also a sign of weak framework knowledge. Finally, confusing dependency caching with Python default argument mutability shows a shallow understanding.
What usually comes next
An interviewer might ask when to set use_cache to False and what performance trade-offs that creates. They may ask how this interacts with async dependencies and the event loop. They might probe whether caching differs for dependencies in path operation decorators versus function parameters. You could be asked how caching affects side effects like logging or auditing, or whether it can hide bugs in non-idempotent dependencies. Finally, expect a question about how to test or verify that a dependency executes only once per request.
A concrete example
Imagine a get_current_user dependency that queries a database to validate a JWT and fetch a user record. If both a path operation function and a sub-dependency require get_current_user, FastAPI calls it once during request resolution, caches the user object, and injects the same instance into both places. This means one database query per request instead of two. If use_cache were set to False, the database query would execute twice in the same request, adding unnecessary latency and load. If the cache were mistakenly thought to be global, you might accidentally serve one user's data to another, which is why understanding request-scoped isolation matters.
Interview question
In FastAPI, a database-querying dependency is injected into both a path operation and a sub-dependency. By default, what happens during a single request?
- a.The dependency runs twice, once per declaration, because FastAPI resolves each Depends() independently
- b.The dependency runs fresh each time because caching must be explicitly enabled with use_cache=True
- c.The dependency runs once and the cached value is reused only within that request's dependency treeCorrect
- d.The dependency runs once and its cached result is shared across all concurrent requests
Why? this is the answer
FastAPI defaults to use_cache=True, so it executes the dependency once per request and reuses that cached value throughout the same request's dependency tree. Option A is a common misconception: FastAPI does not independently resolve every Depends() declaration; it avoids redundant work by caching within the request lifecycle.
Just read this? Test yourself on what you have been reading.
Read the original → fastapi.tiangolo.com
- #fastapi
- #python
- #dependency-injection
- #caching
- #web-frameworks
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
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