Go Academy · Lesson

Health Checks and Production Tuning

HEALTHCHECK, tini, signal handling in containers

Lesson 4 of 413 steps

Health Checks and Production Tuning is a free Go Academy lesson on CoddyKit. This is lesson 4 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, and your progress syncs across the web and the CoddyKit app. The Go Academy course includes 4 lessons in total.

Health check endpoints

A health check endpoint lets load balancers and orchestrators verify the service is alive and ready to serve traffic. Two common kinds: liveness (is it running?) and readiness (can it serve requests?).

Liveness endpoint

Returns 200 OK if the process is alive. A simple always-passing handler — the process being up is sufficient:

mux.HandleFunc("/healthz/live", func(w http.ResponseWriter, r *http.Request) {
    w.WriteHeader(http.StatusOK)
})

Readiness endpoint

Returns 200 only when the service is ready (DB connected, cache warmed up, migrations done). Returns 503 during startup or degraded state:

mux.HandleFunc("/healthz/ready", func(w http.ResponseWriter, r *http.Request) {
    if err := db.PingContext(r.Context()); err != nil {
        w.WriteHeader(http.StatusServiceUnavailable); return
    }
    w.WriteHeader(http.StatusOK)
})

Kubernetes probes

Configure liveness and readiness probes in your Kubernetes Deployment spec:

livenessProbe:
  httpGet: { path: /healthz/live, port: 8080 }
  initialDelaySeconds: 5
  periodSeconds: 10
readinessProbe:
  httpGet: { path: /healthz/ready, port: 8080 }
  initialDelaySeconds: 3
  periodSeconds: 5

GOMAXPROCS tuning

In containers, GOMAXPROCS defaults to the host CPU count, not the container's CPU limit. Use go.uber.org/automaxprocs to set it correctly from cgroup limits.

import _ "go.uber.org/automaxprocs"

Memory limits

Set GOMEMLIMIT (Go 1.19+) to the container memory limit to allow the GC to use memory more aggressively before the OOM killer triggers:

import "runtime/debug"
debug.SetMemoryLimit(512 * 1024 * 1024) // 512 MiB

GC tuning

Set GOGC to control GC frequency. Default is 100 (GC when heap doubles). Lower values reduce memory at the cost of more frequent GC; higher values trade more memory for less GC.

os.Setenv("GOGC", "200") // allow heap to grow 2× before GC

Connection pool tuning

Set database connection pool limits appropriate to your container's resource limits and the database's max_connections:

db.SetMaxOpenConns(25)
db.SetMaxIdleConns(10)
db.SetConnMaxLifetime(5 * time.Minute)

Metrics exposure

Expose Prometheus metrics on a separate port (not the main API port) to avoid leaking internal data to external clients:

go func() {
    http.Handle("/metrics", promhttp.Handler())
    http.ListenAndServe(":9090", nil)
}()

Graceful shutdown in Kubernetes

Set terminationGracePeriodSeconds in the pod spec to allow enough time for the shutdown timeout. Return 503 from the readiness probe immediately when SIGTERM is received.

Profiling in production

Enable pprof on a non-public port with authentication. Never expose it publicly — it reveals internal state and can be used to DoS the service.

Quick Check

Why should GOMAXPROCS be set based on container CPU limits rather than host CPUs?

Recap: Health Checks and Production Tuning

Key points:

  • Liveness: process alive; Readiness: service can serve traffic
  • automaxprocs sets GOMAXPROCS from container CPU limits
  • GOMEMLIMIT prevents OOM kills; GOGC tunes GC frequency
  • Expose metrics on a separate internal-only port
Free to start

Learn Go with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
51
Lessons
203

Frequently Asked Questions

Is the “Health Checks and Production Tuning” lesson free?

Yes — any 3 lessons of the Go Academy learning path, including “Health Checks and Production Tuning”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Go Academy course includes 4 lessons in total.

What will I learn in “Health Checks and Production Tuning”?

HEALTHCHECK, tini, signal handling in containers You practise Go Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Go Academy?

No prior experience is required. Go Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 4 of 4.

How long does the “Health Checks and Production Tuning” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Go Academy lesson?

Yes. Every Go Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Multi-Stage Docker Builds for Go
  2. Environment Config and Secrets
  3. Docker Compose for Local Development
  4. Health Checks and Production Tuning
← Back to Go Academy