Performance optimization makes a single Go program use less CPU, memory and time, while load balancing spreads incoming work across several instances so no one instance is overloaded. Optimization improves how fast one process runs, and load balancing improves how much traffic a fleet can handle and how well it survives failures. Measure first with profiling, and add balancing when one well-tuned instance is no longer enough.
They are complementary. A faster service needs fewer replicas, and a balanced fleet hides the failure of a single instance.
| Aspect | Performance optimization | Load balancing |
|---|---|---|
| Scope | Inside one process | Across many instances |
| Goal | Lower latency, CPU and memory per request | Higher throughput, availability, fault tolerance |
| Tools | pprof, go test -bench, trace | Reverse proxy, cloud load balancer, Kubernetes Service, client-side balancing |
| Typical change | Code, data structures, allocations | Infrastructure and routing configuration |
| Limit | Hardware of one machine | Cost and complexity of more instances |
Never guess. Write a benchmark and profile it.
Run go test -bench . -benchmem -cpuprofile cpu.out and inspect with go tool pprof cpu.out. The builder version allocates far less because it avoids creating a new string on each concatenation. For a running service, expose profiles with net/http/pprof on an internal-only port.
make([]T, 0, n), reuse buffers with sync.Pool, and check escape analysis with go build -gcflags=-m.errgroup with SetLimit instead of unlimited goroutines.sync/atomic or sync.RWMutex where reads dominate.GOMAXPROCS (container-aware by default in recent Go releases) and GOMEMLIMIT for the garbage collector.default.pgo CPU profile in the main package and build with go build, which uses it by default since Go 1.21 and can improve speed by a few percent.A load balancer picks an instance for each request. Common algorithms:
In production you usually use something that already exists: a cloud load balancer, NGINX, Envoy, HAProxy or a Kubernetes Service. gRPC clients can balance on the client side with the built-in resolver and round_robin policy.
This example shows the idea using the standard library only.
This lacks health checks, retries and timeouts, which a real balancer needs. Treat it as a teaching example, not a replacement for a proven proxy.
http.Server.Shutdown.slog logs, and re-profile under realistic load.Profile first. If one hot loop wastes most of the CPU, fixing it is cheaper than scaling out. If the code is already efficient and traffic grows, add instances.
No. The Go scheduler balances goroutines across threads inside a process. Load balancers distribute requests across separate processes or machines.
Round robin or least connections works for most stateless HTTP services. Use consistent hashing when requests must stick to the same node, such as for caches.
No. It helps for frequently allocated, short-lived temporary objects, and can hurt for rare or small ones. Confirm with a benchmark.
Performance optimization tunes one Go process using benchmarks, profiles and fewer allocations, and load balancing distributes traffic across many instances for scale and resilience. Optimize what you measure, keep services stateless and observable, and use an established load balancer in front of multiple instances rather than building one yourself.