Backend Performance Optimization
This skill focuses on making backend services fast, scalable, and efficient. It transforms "it works" into "it scales."
🎭 Performance Review Protocol
Act as a Performance Engineer when optimizing. Follow this protocol:
- Measure First: Never optimize without a baseline. Use Profiling Tools.
- Identify Bottlenecks: Is it CPU? Memory? I/O? Database?
- Hypothesize & Test: "Adding an index will fix this." -> Verify with
EXPLAIN ANALYZE. - Stress Test: Verify the fix holds under load using Load Testing.
⛔ Anti-Patterns to Avoid
| ❌ Anti-Pattern | ✅ Better Approach |
|---|---|
| N+1 Queries | Use IN (...) or DataLoaders. See N+1 Fix Example. |
| Sync I/O in Main Thread | Offload to Worker Threads or use Async APIs. |
| Missing DB Indexes | Index FKs and query predicates. See Query Optimization. |
| No Rate Limiting | Implement Rate Limiting to prevent abuse. |
| Unlimited Arrays | Use pagination or streams. Never findAll(). |
| JSON.parse(hugeString) | Stream parsing or offload to worker (blocks Event Loop). |
1. Caching Strategies
"The fastest query is the one you don't make."
- Patterns: Cache-Aside, Write-Through, Stale-While-Revalidate.
- Implementation: Redis Cache Wrapper | Example
- Golden Rule: Always set a TTL (Time-To-Live).
2. Database Optimization
- Indexing: Use Composite Indexes for multi-column queries.
- Connection Pooling: Maintain a healthy pool size (
Core Count * 2). - Read Replicas: Offload heavy
SELECTtraffic. See Horizontal Scaling. - Deep Dive: Query Optimization Example
3. Language-Specific Tuning
Node.js
- Event Loop: Don't block it. Use Async Patterns.
- Memory: Handle leaks with Memory Debugging.
- Concurrency: Use
Promise.allbut limit concurrency withp-limit.
Python
- GIL: Python is single-threaded. Use
multiprocessingorgunicornworkers for CPU tasks. - Async: Use
asynciofor I/O bound tasks. - Profiling: Use
py-spyorcProfile.
Go
- Goroutines: Cheap, but not free. Avoid leak by ensuring they exit.
- GC Tuning: Adjust
GOGCfor memory vs CPU trade-offs. - Pprof: Built-in world-class profiling.
4. Bottleneck Analysis
Use the right tool for the job. See Profiling Guide.
- CPU Limited? Flame Graphs,
clinic flame,pprof. - Memory Leaking? Heap Snapshots, GC logs.
- Slow Requests? Distributed Tracing (OpenTelemetry), APM.
5. Scaling & Reliability
- Horizontal Scaling: Sharding, Replicas, Statelessness. Read Guide.
- Load Testing: Validate capacity with k6/Artillery. Guide.
- Rate Limiting: Protect your API. Guide.
🛠️ Optimization Checklist
- N+1 Queries eliminated?
- Indexes exist for all
WHEREandJOINcolumns? - Caching implemented for expensive, frequent reads?
- Rate Limiting enabled on public endpoints?
- Compression (Gzip/Brotli) enabled?
- Payloads minimized (no
SELECT *)? - Connection Pool sized correctly?
- Memory Leaks checked (heap usage stable)?
📂 Resources
Patterns
- Profiling Guide
- Load Testing
- Memory Debugging
- Horizontal Scaling
- Rate Limiting
- Async Patterns
Code Examples
- N+1 Fix
- Query Optimization
- Cache Implementation
Templates
- k6 Load Test Script
- Redis Cache Wrapper
- Express Rate Limiter