Caching strategies
The fastest request is the one that never reaches the database. I design caching layer by layer and decide per endpoint how fresh the data must be.
Where a request gets its answer
Client
Azure Front Door (CDN)passes on
API · output cachepasses on
Azure Cache for Redispasses on
Azure SQLanswers
Illustrative example
HTTP caching
Leverage browser and CDN caching with HTTP headers:
Cache-Control: max-age=3600- Cache for 1 hourETag- Conditional requests withIf-None-MatchLast-Modified- Time-based validation- Vary header for content negotiation caching
Server-side caching
- In-memory cache: IMemoryCache for single-instance APIs
- Distributed cache: Redis for multi-instance deployments
- Output caching: Cache entire responses
- Data caching: Cache database query results
Database optimization
- Connection pooling: Reuse database connections
- Async queries: Non-blocking database operations
- Projection: Select only required columns
- Indexing: Create indexes on frequently queried columns
- N+1 prevention: Use eager loading or batching
- Read replicas: Scale reads with database replicas
Compression & minification
- Response compression: Gzip or Brotli compression middleware
- JSON minification: Remove whitespace from JSON responses
- Image optimization: Compress and resize images on-the-fly
Horizontal scaling
- Stateless design: Store session state externally (Redis, Cosmos DB)
- Load balancing: Azure Load Balancer or Application Gateway
- Auto-scaling: Scale out based on CPU, memory, or request count
- Container orchestration: Azure Kubernetes Service (AKS) for microservices