Go gives a team useful building blocks for network services, but a compiled program is only one part of a dependable API. Customers experience deadlines, errors, deployments, and recovery. Those behaviors need deliberate design regardless of the language used to implement the service.
Consider an inventory API that reads availability and reserves stock through another system. Its most important requirement is a clear business outcome when that dependency slows down or returns an uncertain result. This example is a design exercise: use it to frame the decisions your team should settle before increasing traffic.
The key ideas
- Carry deadlines and cancellation across request boundaries.
- Bound concurrent work and define who owns shared state.
- Test shutdown and uncertain outcomes alongside successful requests.
Begin with the API's business contract
Describe accepted inputs, validation failures, authorization rules, and the response for each business outcome. An inventory read and a stock reservation deserve different retry policies because only one changes state. Keep the operation identifier and the meaning of a repeated reservation explicit in the contract.
Organize handlers around translating HTTP requests into application operations. Keep business rules testable without starting a server, and isolate dependency access behind small interfaces where substitution is useful. Avoid adding an interface for every type by habit. The boundary should make a real dependency or ownership decision easier to understand.
Carry deadlines into downstream work
Pass the incoming request's context into operations that support it. Go's database guidance demonstrates deriving a timeout context and supplying it to QueryContext; cancellation then follows the parent context or the deadline. Call the returned cancel function when the operation finishes to release associated resources.
Allocate a latency budget across the whole request rather than assigning every dependency the full customer-facing deadline. Confirm the selected drivers and clients honor cancellation as expected. For a write, cancellation still does not establish the remote business outcome. Keep a way to reconcile a reservation that may have completed after the caller stopped waiting.
Put a limit on concurrent work
A goroutine makes concurrent execution convenient; it does not make a downstream system unlimited. Decide how many requests, background tasks, and dependency calls the service can sustain. Use bounded queues or worker capacity where appropriate, with a defined response when capacity is exhausted. Track queue age as well as queue length.
Give every background task an owner, a stop condition, and a way to report failure. For the inventory example, avoid launching an untracked goroutine for each reservation after returning success. If the business requires durable background processing, acknowledge work only after it has reached the durable boundary your design promises.
Treat shutdown as an application workflow
Coordinate deployment draining with the host or load balancer so new traffic stops reaching an instance before it exits. The net/http Server.Shutdown method closes listeners and idle connections and waits for active connections to become idle, subject to its context. Keep the process alive until that shutdown work finishes.
Plan separate cleanup for workers and hijacked connections such as WebSockets, which Shutdown does not manage automatically. Set a shutdown budget that fits the hosting environment. Exercise a deployment while a request and a background operation are running, then verify whether each completes, resumes elsewhere, or leaves an inspectable recovery task.
Reference: Go net/http: graceful server shutdown
Verify the behavior customers depend on
Record request identifiers, operation outcomes, latency, dependency failures, and saturation without exposing sensitive payloads. Distinguish readiness to receive traffic from the fact that a process exists. Give the on-call owner a way to identify whether a failure belongs to validation, capacity, storage, or a downstream integration.
Test invalid requests, duplicate writes, slow dependencies, client cancellation, and interrupted deployment before release. For a timed-out stock reservation, verify the final inventory state as well as the HTTP response. Establish a measured baseline before introducing more services or concurrency. A useful production design makes both ordinary operation and recovery understandable.
Sources & further reading
The guidance above combines our engineering recommendations with the official references below. Service features and platform requirements can change; check the current documentation when implementing.