All posts
Tag

python

33 posts

Tutorial

Clean Code Python: Full-Stack DI with dependency-injector, FastAPI, and SQLAlchemy

FastAPI's Depends() handles per-request wiring. dependency-injector handles everything else — configuration, singletons, factories, and a declarative container that makes your entire object graph visible in one file.

15 min read
Tutorial

Clean Code Python: Connection Pooling and Database Resilience Under Load

Your pool_size=5 default just met Black Friday traffic. 200 concurrent requests, 5 connections, 195 coroutines waiting on pool checkout — then timeouts cascade into a full outage. Here is how to configure connection pooling, read replicas, and circuit breakers for production traffic.

14 min read
Tutorial

Clean Code Python: Configuration, Feature Flags, and Secrets That Scale

Hardcoded config breaks the moment your 200 tenants need different rate limits, feature access, and API keys. Here is a hierarchical configuration system with per-tenant overrides, percentage-based feature rollouts, and encrypted secrets — all hot-reloadable without restarts.

14 min read
Tutorial

Clean Code Python: Rate Limiting and Noisy Neighbor Prevention

Without rate limiting, one tenant's bulk import consumes all your database connections and API throughput. Here is how to build tiered, sliding-window rate limiting in Python with Redis — protecting every tenant from every other tenant.

11 min read
Tutorial

Clean Code Python: Locking, Idempotency, and Sagas

At 1M transactions per day, two requests modifying the same inventory record is not an edge case. Here is how to prevent lost updates with optimistic locking, double charges with idempotency keys, and partial failures with the saga pattern.

13 min read
Tutorial

Clean Code Python: Multi-Tenant Migrations Without Downtime

A naive ALTER TABLE on a 100M-row table locks it for minutes. Multi-tenant makes it worse: one failed migration across 200 schemas leaves tenants on inconsistent versions. Here is the expand-contract pattern and the tooling to migrate safely.

14 min read
Tutorial

Clean Code Python: API Versioning and Backward-Compatible Evolution

A breaking API change that 'only' affects 5% of tenants still breaks real businesses. Without versioning, you can never evolve your API. Here is how to version a multi-tenant FastAPI backend with per-tenant version pinning, deprecation workflows, and contract testing.

12 min read
Tutorial

Clean Code Python: From git init to Production Traffic

Theory without deployment is fiction. This capstone assembles all 22 prior patterns into a deployed, monitored, incident-ready multi-tenant Python backend — from Docker Compose to runbooks to your first production incident.

22 min read
Tutorial

FastAPI Auth: The Security Mental Model You Need First

Authentication and authorization are two different problems that most tutorials conflate. Here is the mental model, threat landscape, and FastAPI security toolkit you need before writing a single line of auth code.

9 min read
Tutorial

FastAPI Auth: API Key Authentication for Machine Clients

API keys are the most misused auth mechanism in production. Here is how to generate, scope, rotate, and rate-limit API keys in FastAPI — with the patterns that separate toy projects from production systems.

9 min read
Tutorial

FastAPI Auth: JWT Tokens — Stateless Authentication

JWTs eliminate session storage but introduce revocation challenges. Here is how to build access tokens, refresh tokens, and token rotation in FastAPI — with the security pitfalls that tutorials never mention.

10 min read
Tutorial

FastAPI Auth: Password Authentication Done Right

Password hashing is not security — it is one layer. Here is how to build registration, login, session management, and brute-force protection in FastAPI without the mistakes that lead to credential breaches.

12 min read
Tutorial

FastAPI Auth: OAuth 2.0 — The Authorization Framework

OAuth 2.0 is not authentication — it is authorization. Here are the four grant types, why PKCE is now mandatory, and how to implement the authorization code flow in FastAPI with working code and sequence diagrams.

9 min read
Tutorial

FastAPI Auth: OpenID Connect and Single Sign-On

OAuth 2.0 does not tell you who the user is. OpenID Connect adds the identity layer. Here is how to implement Google SSO, validate ID tokens, and understand when OIDC beats SAML.

9 min read
Tutorial

FastAPI Auth: Authorization Patterns — RBAC, ABAC, and Beyond

Authentication answers who. Authorization answers what. Here are three authorization models — RBAC, ABAC, and ReBAC — implemented as FastAPI dependencies with decision flows, comparison tables, and production patterns.

10 min read
Tutorial

Clean Code Python: Type Safety with Protocols Over ABCs

Python's ABC machinery adds inheritance overhead you do not need. Protocols give you structural subtyping — type-safe duck typing with zero coupling between your repository interfaces and implementations.

8 min read
Tutorial

Clean Code Python: Structured Error Handling Across the Stack

Scattered try/except blocks and generic 500 errors signal a system that was not designed. Here is how to define domain exceptions, translate them to HTTP responses, and give your API clients errors they can act on.

7 min read
Tutorial

Clean Code Python: Dependency Injection Without Magic

FastAPI's Depends() is the most underused tool in the framework. Here is how to use it to wire repositories, services, and authentication into your route handlers with no global state and no service locator pattern.

7 min read
Tutorial

Clean Code Python: Async Patterns That Actually Scale

asyncio.gather() is not always the answer. Here is when to parallelize database calls, when NOT to, and how to avoid the most common SQLAlchemy async session mistakes that cause subtle production bugs.

9 min read
Tutorial

Clean Code Python: Testing Strategies for the Full Stack

Unit tests, integration tests, and API tests — each has a role in a Python backend. Here is a complete testing strategy for a FastAPI + SQLAlchemy project, showing what to test at each layer and how the patterns from this series make testing effortless.

10 min read