Designing API Performance Optimization
1. Designing Response Compression
| Algorithm | Use | Notes |
|---|---|---|
| gzip | Universal default | Good ratio, low CPU |
| brotli | HTTPS responses | ~20% better than gzip; modern browsers |
| zstd | Internal RPC | Fast, tunable |
| Threshold | Compress >1KB only | Avoid CPU on small bodies |
2. Designing API Caching Strategy
| Header | Effect |
|---|---|
| Cache-Control: max-age=N | Client caches N seconds |
| s-maxage | Shared cache (CDN) overrides max-age |
| stale-while-revalidate | Serve stale, refresh in bg |
| ETag / If-None-Match | 304 Not Modified |
| Vary | Cache key dimensions (Accept-Encoding, Accept-Language) |
3. Designing Efficient Payload Structures
| Practice | Detail |
|---|---|
| Compact JSON | Drop nulls, short keys for hot endpoints |
| Binary formats | Protobuf, MessagePack, CBOR for internal |
| Streaming | NDJSON / chunked transfer for large lists |
| Pagination | Don't return full collections |
4. Designing Field Selection
| Approach | Example |
|---|---|
| Sparse fieldsets (JSON:API) | ?fields[user]=name,email |
| GraphQL | Client picks exact fields |
| Google APIs style | ?fields=name,email,address(city) |
5. Using HTTP/2 and HTTP/3
| Version | Wins | Notes |
|---|---|---|
| HTTP/1.1 | — | HoL blocking, 6 conns/host |
| HTTP/2 | Multiplex over 1 TCP, header compression (HPACK), server push (deprecated) | Single TCP HoL |
| HTTP/3 (QUIC) | UDP, 0-RTT, no HoL across streams, faster handshake | Better mobile networks |
6. Designing Connection Keep-Alive Strategy
| Setting | Recommendation |
|---|---|
| Keep-Alive timeout | 60–120s |
| Max requests/conn | 1000+ (avoid premature close) |
| Connection pool (client) | Reuse across requests |
| LB idle timeout | Match upstream |
7. Designing Request Batching
| Strategy | Detail |
|---|---|
| Client batch | Group N requests into 1 (e.g., GraphQL batch) |
| Server-side coalescing | DataLoader, micro-batching |
| Time window | 10–50ms batch window |
| Trade-off | Latency for throughput |
8. Designing Lazy Loading Strategy
| Scope | Approach |
|---|---|
| Field-level | Resolve on demand |
| Pagination | Load chunks on scroll |
| Sub-resource | HATEOAS link, follow when needed |
| Defer / stream (GraphQL) | Send slow fields after first byte |
9. Designing API Response Time Optimization
| Lever | Impact |
|---|---|
| Cache layer | 10–100× for hits |
| Reduce DB queries (N+1) | Often 5–50× |
| Index tuning | Bring queries to ms |
| Async non-critical work | Return faster |
| Co-locate services | Eliminate cross-AZ hops |
10. Designing Performance Budgets
| Budget Type | Example |
|---|---|
| Latency | p95 < 200ms for /api/* |
| Payload size | < 50KB gzipped |
| Time to first byte | < 100ms |
| Error budget | < 0.1% 5xx |
| Enforcement | CI perf tests fail build |
11. Designing Payload Compression
| Layer | Detail |
|---|---|
| Transport | gzip/brotli/zstd |
| Serialization | Protobuf vs JSON: 3–10× smaller |
| Image/video | WebP/AVIF, AV1, modern codecs |
| Field-level | Diff/patch (JSON-Patch) for updates |
12. Designing Network Optimization
| Technique | Detail |
|---|---|
| CDN / PoPs | Reduce RTT to origin |
| TCP BBR / TLS 1.3 | Faster congestion + handshake |
| Connection prewarm | Keep N idle conns ready |
| Region pinning | Same-AZ where possible |
| Anycast | Lower client RTT globally |