Skip to content
tezvyn:
Menu

What are startup and shutdown events in FastAPI?

Source: fastapi.tiangolo.comEasyHow cards are made

Tests app lifespan hooks and resource lifecycle. Startup creates DB pools before traffic arrives; shutdown closes them after the last request. These decorators are deprecated; prefer lifespan context managers. Red flag: per-request middleware.

What's really being asked

This question checks whether you understand the application lifespan boundary in ASGI applications and can distinguish one-time initialization from per-request logic. Interviewers want to see that you know where to place expensive setup and cleanup code so it does not run on every request.

The full answer

First, define startup as logic that executes exactly once before the application begins receiving requests, and shutdown as logic that executes exactly once after the application finishes handling requests. Second, give concrete use cases: startup is commonly used to create a database connection pool, initialize an HTTP client session, or load a large machine learning model into memory; shutdown is used to close that pool, flush telemetry buffers, or release file handles gracefully. Third, mention that FastAPI now prefers the lifespan async context manager over the older startup and shutdown event decorators because the context manager naturally pairs setup and teardown and is easier to test. Fourth, note that these hooks are application-level, not route-level, and that they should be kept non-blocking or async when possible to avoid delaying server readiness.

The mistakes people make

A major red flag is describing startup or shutdown as per-request middleware or route dependencies. Another mistake is suggesting heavy synchronous I/O inside a startup event without acknowledging that it blocks the event loop and delays the server from accepting traffic. Some candidates also forget to mention shutdown cleanup entirely, which signals poor operational awareness. Finally, claiming that startup and shutdown events are the modern recommended approach without mentioning their deprecation in favor of lifespan context managers shows outdated knowledge.

What usually comes next

The interviewer may ask how you would test lifespan events, how they differ from dependency with yield, or what happens if a startup event raises an exception. They might also ask how sub-applications handle lifespan events or how you would manage a shared Redis connection across workers.

A concrete example

A practical startup use case is creating an async SQLAlchemy engine and sessionmaker when the app boots so that every route can reuse the same pool instead of creating connections per request. The matching shutdown use case is calling engine.dispose() to close all database connections cleanly after the last request is served, preventing connection leaks during deployments or container restarts.

Interview question

Where should you place code to initialize a database pool once at app startup and close it at shutdown?

  • a.In a route dependency with yield so it runs around every request
  • b.At the top level of the module so it executes once on import
  • c.In a lifespan context manager or startup/shutdown event handlersCorrect
  • d.In an async middleware function that runs before each request
Why?

Lifespan and startup/shutdown hooks execute exactly once per application lifecycle, making them the right place for expensive shared resources. Middleware and route dependencies run per request, which would wastefully recreate the pool on every call.

Just read this? Test yourself on what you have been reading.

Read the original → fastapi.tiangolo.com

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.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on fastapi — each one lists the topics its interview covers.

See open roles