Production
Errors at the edge, logging, limits, containers, and profiling.
An unexpected database error reaches your handler. What does the client get?
All questions with explanations (answers shown)
1. An unexpected database error reaches your handler. What does the client get?
Answer: A generic 500, with the detail logged against a request id
A raw SQL error leaks your table and column names; a stack trace leaks your source paths. Log the detail, return a status.
2. A request is cancelled because the client disconnected. How should you treat it?
Answer: Recognise
context.Canceled— nobody is waiting, so do not page anyoneTreating client disconnects as 500s fills your error budget with noise and hides real failures.
3. What must never appear in a log line?
Answer: Passwords, tokens, or whole
AuthorizationheadersIt is the easiest way to turn a log store into a breach, and log stores are often readable by far more people than your database.
4. Why keep
/livezand/readyzseparate?Answer: Liveness means "restart me"; readiness means "stop sending traffic". Merging them makes a brief DB blip restart every instance at once.
Wire the database ping into readiness only.
5. Which endpoints deserve the tightest rate limit?
Answer: Login, register, and password reset
That is where credential stuffing lands. Rate limiting turns "unlimited guesses" into "twelve a minute". It is some of the cheapest security you can add.
6. What is wrong with
Access-Control-Allow-Origin: *on an authenticated API?Answer: Any site can read your API’s responses from a browser, and
*is invalid combined withAllow-Credentials: trueThe usual bad fix is to echo back whatever
Originarrives, which does let any site make credentialed calls. Keep an explicit allowlist from config, and setVary: Originso a cache does not serve the wrong header to the wrong site.7. Which two environment variables should every Go container set?
Answer:
GOMEMLIMITandGOMAXPROCSBy default
GOGC=100lets the heap grow to about twice the live heap before the next collection, and the GC knows nothing about your container limit. If live heap plus that headroom passes the limit, the OOM killer ends the process even though most of that memory was garbage.GOMEMLIMIT(set a little below the limit, say 90%) is a soft limit: as the heap nears it the GC runs more often, trading CPU for staying under it. It cannot save you if the live heap itself is too big. Before Go 1.25, withoutGOMAXPROCSGo used the host’s core count, not your CPU limit: 64 schedulers sharing 2 cores of quota. Go 1.25 and later read the cgroup limit on Linux.8. Where should
net/http/pprofbe exposed?Answer: On a separate localhost-only port
Public pprof leaks your source paths and lets anyone stall your process with a 30-second CPU profile.
9. What is most often the biggest cause of slow endpoints?
Answer: A query in a loop, or a missing index
The database is where your latency lives. One JOIN beats twenty round trips; one index beats a month of Go micro-optimisation.
10. You want to know whether a change made things faster. What do you run?
Answer:
go test -bench=. -count=10both times, thenbenchstat old.txt new.txtA single benchmark run is noise.
benchstattells you whether the difference is statistically real.11. Which optimisation should you apply without a profile first?
Answer: Preallocating with
make([]T, 0, n)when you already know nPreallocation costs nothing in readability and is obviously right. The other three each need a benchmark proving they help in your workload, or they only add risk.
12. Find the bug:
resp, err := client.Do(req); if err != nil { return err }; if resp.StatusCode != 200 { return fmt.Errorf("status %d", resp.StatusCode) }Answer:
resp.Bodyis never closed, so every call leaks a connection; adddefer resp.Body.Close()right after the error checkA response body must always be closed, even when you do not read it. Draining it with
io.Copy(io.Discard, resp.Body)before closing also lets the connection be reused.13. A batch endpoint does
for _, item := range req.Items { go process(item) }. One request has 50,000 items. What is the right fix?Answer: Cap concurrency with a fixed pool, a semaphore channel, or
errgroupwithSetLimit, and wait for the work to finishGoroutines are cheap, but what they do is not: 50,000 at once can exhaust database connections, memory or a downstream API. Unbounded fan-out also never reports errors. Bound it and wait.