Implementing Performance Optimization
1. Optimizing Database Queries
| Tactic | Detail |
|---|---|
| Indexes | Cover WHERE / ORDER BY / JOIN cols |
| Avoid N+1 | JOIN FETCH / batch / DataLoader |
| Project columns | Avoid SELECT * |
| Pagination | Keyset over OFFSET |
| Analyze | EXPLAIN ANALYZE |
2. Implementing Connection Pooling
| Param | Guidance |
|---|---|
| Max size | ≈ cores × 2; not "as high as possible" |
| Min idle | 1–5 to avoid cold starts |
| Timeout | Connection < request timeout |
| Library | HikariCP (Java), pgbouncer (proxy) |
3. Using Asynchronous Processing
| Tactic | Detail |
|---|---|
| Reactive / virtual threads | Java 21 Loom |
| Background queue | Offload non-critical work |
| Effect | Lower p99, higher throughput |
4. Implementing Lazy Loading
| Pattern | Detail |
|---|---|
| JPA lazy | Load relations on access |
| Caveat | N+1 risk; pair with fetch graph |
| UI | Lazy load images, code-split |
5. Using Batch Processing
| Tactic | Detail |
|---|---|
| Bulk insert | JDBC batch, COPY (Postgres) |
| Batch API | Combine N requests |
| Tradeoff | Latency vs throughput |
6. Optimizing Payload Size
| Tactic | Detail |
|---|---|
| Field selection | GraphQL / sparse fieldsets |
| Binary formats | Protobuf, MessagePack |
| Compression | gzip / brotli / zstd |
| Pagination | Cursor-based |
7. Implementing Parallel Processing
| Tactic | Detail |
|---|---|
| Fan-out / fan-in | CompletableFuture, virtual threads |
| Stream parallel | For CPU-bound, sized data |
| Caveat | Backpressure & pool exhaustion |
8. Using CDN for Static Assets
| Element | Detail |
|---|---|
| Edge caching | Near user POPs |
| Versioned URLs | Long TTL + immutable |
| Image optimization | WebP/AVIF, responsive |
| Vendors | Cloudflare, Fastly, CloudFront |
9. Optimizing Network Latency
| Tactic | Detail |
|---|---|
| Co-locate | Same AZ for chatty pairs |
| Connection reuse | HTTP keep-alive, gRPC channel |
| Reduce hops | Direct service-to-service via mesh |
| Edge compute | For geo-distributed users |
10. Implementing Query Optimization
| Tactic | Detail |
|---|---|
| Materialized views | Pre-compute heavy aggregates |
| Read model (CQRS) | Optimized for reads |
| Search index | Elasticsearch / OpenSearch |
| Avoid functions on indexed cols | Breaks index use |
11. Using HTTP/2 and HTTP/3
| Feature | Detail |
|---|---|
| HTTP/2 | Multiplexing, header compression |
| HTTP/3 | QUIC over UDP; better lossy networks |
| gRPC | HTTP/2 mandatory |
| Adoption | Edge first; backends per support |
12. Implementing Response Compression
| Algorithm | Detail |
|---|---|
| gzip | Default; widely supported |
| brotli | Better ratio for text |
| zstd | Fast + great ratio |
| Skip | Already-compressed (images, video) |