Stage 3 · Build
Performance & Caching at Scale
Latency Profiling
Use pprof, trace, request histograms, and flame graphs to find CPU, lock, and allocation hot spots.
Why Profile
Do not guess where bottlenecks are. Profile to find them. CPU profiling shows where time is spent. Memory profiling shows where allocations happen. Goroutine profiling shows where concurrency is stuck.
pprof
CPU Profiling
# Capture 30-second CPU profile
go tool pprof http://localhost:6060/debug/pprof/profile?seconds=30
# Interactive analysis
go tool pprof pprof.cpu.001.pb.gz
# Common commands
top # show top functions
top -cum # show top by cumulative time
list handler # show source for a function
web # open in browser (requires graphviz)CPU profiling samples which functions are executing. The profile shows where your program spends time. Focus on the top functions first.
Memory Profiling
# Heap profile
go tool pprof http://localhost:6060/debug/pprof/heap
# Allocation profile (cumulative allocations)
go tool pprof http://localhost:6060/debug/pprof/allocs
# In interactive mode
top -inuse_space # show functions using most memory
top -alloc_space # show functions allocating most memory
list myFunction # show allocation details for a functionHeap profiles show current memory usage. Allocation profiles show cumulative allocations. Use heap for memory leaks, allocs for GC pressure.
Request Histograms
Flame Graphs
# Install go-flamegraph
go install github.com/google/pprof@latest
go install github.com/flamegraph/go-flamegraph@latest
# Capture and visualize
go tool pprof -http=:8080 pprof.cpu.001.pb.gz
# Or generate SVG
go tool pprof -svg pprof.cpu.001.pb.gz > flame.svgFlame graphs visualize where time is spent. Wide bars mean more time. Deep stacks mean more nesting. This makes it easy to spot the hottest code paths.
Profile your service under production-like load. Idle profiling shows nothing useful. Use k6 or vegeta to generate traffic while capturing the profile.
Mark this lesson complete to store local progress and unlock a cleaner resume path the next time you visit.