Optimizing Performance

1. Implementing Database Indexing

Index TypeUse
B-treeEquality, range, sort (default)
HashEquality only (in-memory)
GIN / GiSTJSON, full-text, arrays
CompositeMulti-column queries (order matters)
CoveringInclude needed columns; index-only scan
PartialIndex subset (e.g. WHERE active=true)

2. Using Connection Pooling

LibraryLanguage
HikariCPJava
pgbouncerPostgreSQL (external)
node-postgres poolNode.js
SQLAlchemy poolPython
SettingRecommendation
Pool size≈ (CPU cores × 2) + disk count
Connection timeout30s
Idle timeout10 min
Max lifetime30 min (rotate connections)

3. Using CDN for Static Resources

CDNStrength
CloudflareFree tier, edge functions
FastlyVCL, real-time purge
CloudFrontAWS integration
AkamaiEnterprise scale

4. Implementing API Response Caching

LayerTool
HTTP edgeCDN with Cache-Control
ApplicationRedis, Memcached
In-processCaffeine (Java), LRU-cache (Node)
DatabaseMaterialized views, query cache

5. Using Lazy Loading

PatternTrade-off
LazyOn-demand fetch; risk of N+1
EagerLoad upfront; over-fetching
Explicit (DataLoader)Batch + cache lazy fetches

6. Implementing Field Selection

See section 15. Reduces payload size and DB I/O by returning only requested columns.

EffectBenefit
Smaller payloadsLess bandwidth, faster parse
Index-only scansNo table heap access
Skip JOINsFaster query plans

7. Using Asynchronous Processing

PatternUse Case
Message queue (Kafka, RabbitMQ, SQS)Decouple slow work from request
Background workersEmail send, image resize, reports
Event-drivenPub/sub for cross-service work
202 Accepted + pollingLong-running operations (section 27)

8. Implementing Database Query Optimization

TechniqueBenefit
EXPLAIN ANALYZEIdentify slow queries
Avoid SELECT *Project only needed columns
Use LIMIT + indexAvoid full scans
Batch queriesReduce round-trips (IN clause)
Read replicasOffload SELECTs from primary
Materialized viewsPrecomputed aggregates
Sharding / partitioningHorizontal scale

9. Monitoring Performance Metrics

MetricThreshold
p50 latency< 100ms
p95 latency< 500ms
p99 latency< 1s
Error rate< 0.1%
Throughput (req/s)Track per endpoint
Saturation (CPU, memory, DB)< 70% sustained

10. Implementing Load Balancing

AlgorithmUse
Round-robinSimple, equal capacity
Least connectionsLong-lived connections
WeightedHeterogeneous instance sizes
IP/session hashSticky sessions (avoid if stateless)
GeographicLatency-based routing

11. Using HTTP/2

FeatureBenefit
MultiplexingConcurrent streams on one connection
Header compression (HPACK)Reduce overhead
Server push DEPRECATEDRemoved in Chrome 106; use early hints (103)
Binary framingMore efficient parsing

12. Implementing Connection Keep-Alive

HeaderValue
Connection: keep-aliveHTTP/1.1 default
Keep-Alive: timeout=30, max=10030s idle, 100 reqs per connection
HTTP/2Always persistent