Skip to content
tezvyn:

All bites

The whole library, newest first. Filter by what you are here for, or pick a topic if you already know.

8664 bites

Page 11

Python & FastAPI2 min read

Why use an ASGI server like Uvicorn instead of the dev server?

Tests: dev vs prod environment distinction. Answer: Uvicorn is a prod server program; contrast dev server's constant restart/break/fix cycle with prod needs for performance, stability, and uninterrupted access.

What are the key responsibilities for Nginx versus Uvicorn?
Python & FastAPI2 min read

What are the key responsibilities for Nginx versus Uvicorn?

This tests the reverse-proxy versus ASGI-server boundary. A strong answer gives Nginx TLS termination, static files, buffering, and load balancing, while Uvicorn runs the Python app and async workers.

Python & FastAPI2 min read

How do you manage configuration and secrets for a containerized FastAPI app?

Tests 12-factor config separation and Docker secret hygiene. A strong answer uses pydantic-settings with runtime env vars, lru_cache, and keeps .env out of the image. Red flag: baking credentials into Dockerfile layers or committing .env files.

Python & FastAPI2 min read

Mocking with Pytest's monkeypatch

Pytest's monkeypatch is a temporary stunt double for your code, safely swapping out functions or environment variables for a single test. Use it to isolate tests from network calls or filesystem access.

Python & FastAPI2 min read

Explain multi-stage Docker builds for Python and builder vs runtime

Tests separation of build-time and runtime concerns. A strong answer contrasts the builder stage (gcc, headers, wheels) with the runtime stage (slim base, copied artifacts, no compiler). Red flag: citing size alone while ignoring security and caching.

Testing WebSockets in FastAPI
Python & FastAPI2 min read

Testing WebSockets in FastAPI

Test a WebSocket conversation by scripting both sides. FastAPI's TestClient provides a websocket_connect context manager to send messages and assert responses sequentially, which is crucial for testing chats or live data feeds.

Python & FastAPI2 min read

Diagnosing a slow FastAPI endpoint under load

Add timing and tracing to isolate the slow span, watch CPU vs wait time and event-loop lag, then use profilers like py-spy or cProfile and DB EXPLAIN.

FastAPI: Configure API Metadata for Better Docs
Python & FastAPI2 min read

FastAPI: Configure API Metadata for Better Docs

Think of FastAPI metadata as your project's business card. It sets the title, version, and description in your auto-generated docs, making your API professional and discoverable. The main footgun is forgetting to update the version string after a release.

Python & FastAPI1 min read

Limits of BackgroundTasks vs Celery or ARQ

BackgroundTasks run in the same worker with no persistence, retries, or visibility, and die with the process; Celery or ARQ add durability, retries, scheduling, and separate workers.

Python & FastAPI1 min read

Zero-downtime blue-green deploys on Kubernetes

Run blue and green deployments, switch a Service or ingress selector to the new color after readiness probes pass, drain old pods gracefully, and handle backward-compatible DB migrations.

Python & FastAPI1 min read

Secure refresh token flow for access renewal

Short-lived access token, longer-lived refresh token stored server-side, a refresh endpoint that validates and rotates the refresh token issuing a new pair.

FastAPI: Documenting Additional API Responses
Python & FastAPI2 min read

FastAPI: Documenting Additional API Responses

Document every possible API response, not just the happy path. The responses decorator parameter lets you define alternative status codes and schemas, like a 404 error model, making your OpenAPI docs complete.

Python & FastAPI1 min read

Testing a DB endpoint via dependency override

Use app.dependency_overrides to swap the real get_db for one yielding a test database session, run against a disposable SQLite or test Postgres, and assert through TestClient.

Python & FastAPI1 min read

Pydantic BaseModel vs dataclasses in FastAPI

BaseModel validates and coerces data at runtime, parses and serializes JSON, integrates with OpenAPI schema generation, and supports rich validators; dataclasses only store data with no validation.

Python & FastAPI1 min read

Mapping camelCase JSON to snake_case Pydantic fields

Set an alias_generator (to_camel) plus populate_by_name in model config, accept aliases on input, and serialize with by_alias=True so responses come out camelCase.

Exclude a FastAPI Endpoint from OpenAPI Docs
Python & FastAPI1 min read

Exclude a FastAPI Endpoint from OpenAPI Docs

Hide an endpoint from your API docs by setting include_in_schema=False. Use this for internal or deprecated endpoints. The footgun: this only hides the endpoint from documentation; it remains fully functional and accessible if the URL is known.

Python & FastAPI1 min read

Pydantic computed fields in response models

A @computed_field decorated property is excluded from input and validation but included in serialization and the OpenAPI schema, ideal for values like full_name derived from other fields.

Python & FastAPI1 min read

Streaming large file downloads efficiently

Use StreamingResponse with a generator that yields chunks (or FileResponse for an on-disk file), set media_type and a Content-Disposition header, so memory stays flat regardless of file size.

Python & FastAPI1 min read

Feature-based vs layer-based project structure

Layer-based groups by technical role and is simple early but scatters a feature across folders; feature-based groups by domain, improving cohesion and ownership at the cost of some duplication and…

Python & FastAPI2 min read

Overriding FastAPI's OpenAPI Generator

FastAPI lets you swap app.openapi to reshape its generated schema without forking. Use this for vendor extensions, filtered operations, or merging external schemas. Forgetting to cache the result means every docs request rebuilds it and destroys performance.