Implementing Performance Optimization
1. Implementing Database Query Optimization
| Tactic | Detail |
|---|---|
| EXPLAIN ANALYZE | Read execution plans |
| Indexes | Cover WHERE / JOIN / ORDER BY |
| Avoid SELECT * | Project only needed columns |
| Pagination | Keyset over OFFSET for deep pages |
| Batch fetch | Avoid N+1 |
2. Implementing Caching Strategies
| Layer | Hit Time |
|---|---|
| CPU cache | ns |
| In-process (Caffeine) | μs |
| Distributed (Redis) | ~1 ms |
| CDN edge | ~10 ms |
3. Implementing Connection Pooling
| Setting (HikariCP) | Guide |
|---|---|
| maximumPoolSize | ~ (cores * 2) + effective spindles |
| connectionTimeout | 30s |
| idleTimeout | 10 min |
| maxLifetime | 30 min (< DB wait_timeout) |
| leakDetectionThreshold | 20s in dev |
4. Implementing Async Processing
| Move Off Request | Examples |
|---|---|
| Email/SMS | Queue + worker |
| Heavy reports | Background job |
| 3rd-party fanout | Event publisher |
| Image processing | Worker pool |
5. Implementing Lazy Loading
| Approach | Detail |
|---|---|
| JPA FetchType.LAZY | Default for collections |
| EntityGraph | Per-query eager fetch plan |
| Open-in-view | Disable in production |
| DTO projection | Avoid loading full graph |
6. Implementing Pagination Optimization
Example: Keyset pagination
SELECT id, created_at FROM events
WHERE (created_at, id) < (:lastTs, :lastId)
ORDER BY created_at DESC, id DESC
LIMIT 50;
7. Implementing Database Indexing Strategy
| Index | Use |
|---|---|
| B-tree | Equality, range, sort |
| Composite | Match query column order |
| Covering / INCLUDE | Index-only scans |
| Partial | Index slice (e.g., active rows) |
| GIN | JSONB, FTS, arrays |
8. Implementing Response Compression
| Layer | Detail |
|---|---|
| Reverse proxy | Nginx gzip/brotli |
| App level | server.compression.enabled=true |
| Min size threshold | ~ 1024 bytes |
| Skip binary | Already-compressed types |
9. Implementing Resource Pooling
| Pool | Setting |
|---|---|
| DB connections | HikariCP |
| HTTP connections | Apache HC pool |
| Threads | ThreadPoolExecutor (bounded queue) |
| Direct buffers | Netty PooledByteBufAllocator |
10. Implementing Memory Optimization
| Practice | Detail |
|---|---|
| Right-size heap | Avoid > 32 GB (compressed oops break) |
| GC tuning | G1 default; ZGC for low pause |
| Streams | Process iteratively, not in memory |
| Profiler | JFR, async-profiler |
| Heap dumps | On OOM for postmortem |
11. Implementing CPU Optimization
| Tactic | Detail |
|---|---|
| Profile first | JFR / async-profiler flame graphs |
| Algorithmic wins | O(n²) → O(n log n) |
| Avoid boxing in hot paths | Primitive streams |
| JIT-friendly | Small monomorphic methods |
| Vector API | Java 21 incubator for SIMD |
12. Implementing Network Optimization
| Tactic | Detail |
|---|---|
| HTTP keep-alive | Reuse TCP connections |
| HTTP/2 or HTTP/3 | Multiplexing, header compression |
| Co-locate | Same AZ as dependencies |
| Reduce roundtrips | Batch APIs, GraphQL/BFF |
| CDN | Static and cacheable API responses |