Go handles resource management with a garbage collector, cheap goroutines and context, and it scales vertically very well because one process uses all available cores. Horizontal scaling and load balancing are not built into the language: you run several stateless instances of your service and balance traffic with a reverse proxy, a cloud load balancer or Kubernetes, while the Go code itself stays graceful, bounded and observable.
This guide covers managing resources inside a Go process, scaling up and out, balancing load (including a small httputil.ReverseProxy example) and the operational practices that make it reliable. Examples target Go 1.24 and later.
Four resources decide how far a single instance scales: memory, CPU, file descriptors and connections.
GOMEMLIMIT just below the container limit so the collector works harder before the process is killed for exceeding it.GOMAXPROCS threads. Since Go 1.25 the default follows the container CPU limit; on older versions set it explicitly.defer and pooled (database/sql pools connections; reuse a single http.Client).errgroup.Group.SetLimit.A bounded worker pool is the basic building block for protecting a service from overload:
Go programs use every core without extra threading code, so adding CPU and memory to one instance often helps immediately. Make that effective by profiling with pprof, preallocating slices, avoiding lock contention and using sync/atomic or sharded state for hot counters. Vertical scaling has a ceiling and a single point of failure, so it is rarely enough alone.
Horizontal scaling means running several identical instances. It works only if instances are stateless:
On Kubernetes, a Horizontal Pod Autoscaler adds or removes replicas based on CPU, memory or custom metrics. Set resource requests and limits so scheduling and autoscaling make sensible decisions.
When instances are added and removed, in-flight requests must finish. http.Server.Shutdown stops accepting connections and waits for active ones:
The method-and-path pattern in mux.HandleFunc("GET /healthz", ...) requires Go 1.22 or later.
| Layer | Examples | When to use |
|---|---|---|
| Cloud or hardware load balancer | AWS ALB/NLB, Google Cloud Load Balancing | Default for public traffic |
| Reverse proxy | NGINX, HAProxy, Envoy, Caddy | Routing, TLS termination, rate limits |
| Kubernetes Service and Ingress | kube-proxy, ingress controllers | Services inside a cluster |
| In-process proxy | httputil.ReverseProxy | Small gateways, custom routing logic |
| Client-side | gRPC resolvers and balancers | Service-to-service calls without a proxy |
Common algorithms are round robin, least connections and consistent hashing (useful for caches). Pair any of them with health checks so failed instances are removed.
This distributes requests evenly but has no health checks or retries, so for production prefer a dedicated proxy or the platform load balancer.
Load balancing spreads load, but each instance must still defend itself:
ReadHeaderTimeout, context.WithTimeout).golang.org/x/time/rate and return 429 Too Many Requests when a client exceeds its budget.Expose metrics (for example with the Prometheus client or OpenTelemetry), including request rate, latency percentiles, error rate, goroutine count, heap size and GC pause. Scale on saturation signals such as CPU and queue depth, and keep pprof reachable on an internal port to explain surprises.
No. The standard library gives you httputil.ReverseProxy as a building block, and gRPC has client-side balancing, but production load balancing is normally done by a proxy or the platform.
Both. Scale up until profiling shows diminishing returns, then scale out with stateless replicas for capacity and availability.
Handle SIGTERM, call http.Server.Shutdown, and use readiness probes so the load balancer stops sending traffic before the process exits.
Yes. Bursts, slow downstreams and retries can still create unbounded work inside one instance.
Manage resources by bounding goroutines, closing handles and setting memory limits. Scale up with profiling and scale out with stateless instances, balance traffic with a proxy or platform load balancer with health checks, shut down gracefully, and give every instance timeouts, rate limits and metrics so overload degrades gracefully.