Dev Skills
Go tests·13 questions

Production

Errors at the edge, logging, limits, containers, and profiling.

1 / 13

An unexpected database error reaches your handler. What does the client get?

All questions with explanations (answers shown)
  1. 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. 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 anyone

    Treating client disconnects as 500s fills your error budget with noise and hides real failures.

  3. 3. What must never appear in a log line?

    Answer: Passwords, tokens, or whole Authorization headers

    It 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. 4. Why keep /livez and /readyz separate?

    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. 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. 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 with Allow-Credentials: true

    The usual bad fix is to echo back whatever Origin arrives, which does let any site make credentialed calls. Keep an explicit allowlist from config, and set Vary: Origin so a cache does not serve the wrong header to the wrong site.

  7. 7. Which two environment variables should every Go container set?

    Answer: GOMEMLIMIT and GOMAXPROCS

    By default GOGC=100 lets 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, without GOMAXPROCS Go 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. 8. Where should net/http/pprof be 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. 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. 10. You want to know whether a change made things faster. What do you run?

    Answer: go test -bench=. -count=10 both times, then benchstat old.txt new.txt

    A single benchmark run is noise. benchstat tells you whether the difference is statistically real.

  11. 11. Which optimisation should you apply without a profile first?

    Answer: Preallocating with make([]T, 0, n) when you already know n

    Preallocation 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. 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.Body is never closed, so every call leaks a connection; add defer resp.Body.Close() right after the error check

    A 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. 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 errgroup with SetLimit, and wait for the work to finish

    Goroutines 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.