Skip to content

Lesson 9 of 9 · 29m

Deployment Hygiene: Small Binaries, Predictable Rollouts, Honest Rollbacks

Build reproducible static binaries, configure processes deliberately, roll out with health gates, and prepare rollback paths that account for data changes.

Go makes deployment look easy because the output is often one binary. That simplicity is real, but it seduces teams into skipping versioned builds, migration safety, rollout health, configuration review, and rollback plans. The binary is the easy part. Operations is the product.

Build reproducibly and label the artifact

Use locked module resolution, strip unnecessary paths, and stamp version information into the binary:

CGO_ENABLED=0 GOOS=linux GOARCH=amd64   go build -trimpath   -ldflags="-s -w -X main.version=1.4.2 -X main.commit=$(git rev-parse HEAD)"   -o bin/orders ./cmd/orders

A static binary is convenient, but CGO_ENABLED=0 is a choice, not a religion. It changes DNS resolver behavior and disables libraries requiring cgo. Understand what you ship.

A minimal container is still a contract

FROM golang:1.23-bookworm AS build
WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 go build -trimpath -ldflags='-s -w' -o /out/orders ./cmd/orders

FROM gcr.io/distroless/static-debian12:nonroot
COPY --from=build /out/orders /orders
USER nonroot:nonroot
EXPOSE 8080
ENTRYPOINT ["/orders"]

latest is not a version; it is an argument waiting to happen during an incident. Pin base images in a way your update process can manage.

Configuration belongs outside the binary

Bake defaults, not environment-specific secrets. Inject configuration through environment variables, files, or a secret manager. Fail startup when required configuration is missing or malformed; half-configured services fail later in more expensive places.

Validate combinations: shutdown timeout below orchestrator grace, client timeout below server budget, database pool below database capacity, admin listener separate from public traffic.

Migrations decide whether rollback is real

Rolling back code is easy. Rolling back data often is not:

expand: add nullable column or new table
migrate: backfill incrementally
switch: code reads and writes new shape
contract: remove old shape after old code is gone

Expand-migrate-contract is slower than heroic alteration, but it gives rollouts room to fail without corrupting the future.

Roll out in stages and define healthy

Gate rollout on readiness and behavior, not merely process start:

kubectl rollout status deployment/orders --timeout=3m
kubectl logs deploy/orders --tail=100

Watch error rate, latency, saturation, and dependency health through real traffic. For low-traffic services, run synthetic checks against critical endpoints.

Rollback is a plan, not a button

A rollback plan names the artifact, configuration, schema shape, feature flags, and data corrections involved. If the new version wrote data the old one cannot read, redeploying the old binary is not rollback; it is a second incident.

Field notes

  • Record artifact digest and commit in startup logs.
  • Keep migrations out of application startup unless your platform has a deliberate migration-job pattern.
  • Do not run containers as root because it is convenient for five minutes.
  • Practice rollback before you need it; the first rehearsal should not be customer-visible.

Was this lesson helpful?

Sign in to rate