Designing API Performance Optimization

1. Designing Response Compression

AlgorithmUseNotes
gzipUniversal defaultGood ratio, low CPU
brotliHTTPS responses~20% better than gzip; modern browsers
zstdInternal RPCFast, tunable
ThresholdCompress >1KB onlyAvoid CPU on small bodies

2. Designing API Caching Strategy

HeaderEffect
Cache-Control: max-age=NClient caches N seconds
s-maxageShared cache (CDN) overrides max-age
stale-while-revalidateServe stale, refresh in bg
ETag / If-None-Match304 Not Modified
VaryCache key dimensions (Accept-Encoding, Accept-Language)

3. Designing Efficient Payload Structures

PracticeDetail
Compact JSONDrop nulls, short keys for hot endpoints
Binary formatsProtobuf, MessagePack, CBOR for internal
StreamingNDJSON / chunked transfer for large lists
PaginationDon't return full collections

4. Designing Field Selection

ApproachExample
Sparse fieldsets (JSON:API)?fields[user]=name,email
GraphQLClient picks exact fields
Google APIs style?fields=name,email,address(city)

5. Using HTTP/2 and HTTP/3

VersionWinsNotes
HTTP/1.1HoL blocking, 6 conns/host
HTTP/2Multiplex over 1 TCP, header compression (HPACK), server push (deprecated)Single TCP HoL
HTTP/3 (QUIC)UDP, 0-RTT, no HoL across streams, faster handshakeBetter mobile networks

6. Designing Connection Keep-Alive Strategy

SettingRecommendation
Keep-Alive timeout60–120s
Max requests/conn1000+ (avoid premature close)
Connection pool (client)Reuse across requests
LB idle timeoutMatch upstream

7. Designing Request Batching

StrategyDetail
Client batchGroup N requests into 1 (e.g., GraphQL batch)
Server-side coalescingDataLoader, micro-batching
Time window10–50ms batch window
Trade-offLatency for throughput

8. Designing Lazy Loading Strategy

ScopeApproach
Field-levelResolve on demand
PaginationLoad chunks on scroll
Sub-resourceHATEOAS link, follow when needed
Defer / stream (GraphQL)Send slow fields after first byte

9. Designing API Response Time Optimization

LeverImpact
Cache layer10–100× for hits
Reduce DB queries (N+1)Often 5–50×
Index tuningBring queries to ms
Async non-critical workReturn faster
Co-locate servicesEliminate cross-AZ hops

10. Designing Performance Budgets

Budget TypeExample
Latencyp95 < 200ms for /api/*
Payload size< 50KB gzipped
Time to first byte< 100ms
Error budget< 0.1% 5xx
EnforcementCI perf tests fail build

11. Designing Payload Compression

LayerDetail
Transportgzip/brotli/zstd
SerializationProtobuf vs JSON: 3–10× smaller
Image/videoWebP/AVIF, AV1, modern codecs
Field-levelDiff/patch (JSON-Patch) for updates

12. Designing Network Optimization

TechniqueDetail
CDN / PoPsReduce RTT to origin
TCP BBR / TLS 1.3Faster congestion + handshake
Connection prewarmKeep N idle conns ready
Region pinningSame-AZ where possible
AnycastLower client RTT globally