Initial commit: Go SRE interview exercises
Includes solved 02-worker-pool and 09-safe-counter (mutex, atomic, sharded). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01GbnJj9ZqEx9nUvzkjdwfMq
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
# 06 — Retry with Backoff + Jitter
|
||||
|
||||
Retries are the most dangerous reliability primitive: applied naively they
|
||||
convert partial outages into total ones. Interviewers use this task to check
|
||||
whether you've internalized that or just memorized the loop.
|
||||
|
||||
## Why jitter is non-negotiable (the thundering herd)
|
||||
|
||||
A dependency blips for 2 seconds. 10,000 clients fail *simultaneously*.
|
||||
Without jitter they all retry at t+1s, t+3s, t+7s — as one synchronized
|
||||
battering ram. The service recovers, gets hit by a perfectly-phased wave of
|
||||
10k requests, dies again. Congratulations, your retry policy is now a DDoS
|
||||
with extra steps — this is a **metastable failure**, and the system can stay
|
||||
wedged even after the original trigger is gone.
|
||||
|
||||
The AWS Architecture Blog's classic analysis ("Exponential Backoff and
|
||||
Jitter") compared strategies and **full jitter** — `sleep = rand(0, backoff)`
|
||||
— came out best: maximal desynchronization, nearly minimal total work. That's
|
||||
what you implement here. "Equal jitter" (`backoff/2 + rand(0, backoff/2)`) is
|
||||
the runner-up if you want a floor.
|
||||
|
||||
## `time.Sleep` vs `select` — the test that catches everyone
|
||||
|
||||
```go
|
||||
select {
|
||||
case <-time.After(sleep):
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
}
|
||||
```
|
||||
|
||||
`time.Sleep` is uninterruptible. In a shutdown, a worker mid-backoff on a
|
||||
10-minute sleep holds your rolling deploy hostage until SIGKILL. Selecting on
|
||||
`ctx.Done()` is what makes retries composable with graceful shutdown
|
||||
(exercise 08). This single line is the difference between "knows Go" and
|
||||
"has been paged."
|
||||
|
||||
## Error classification — retrying the unretryable
|
||||
|
||||
Retrying a 400 or a permission error is pure waste; retrying a *non-idempotent*
|
||||
operation (POST /charge) is a career event. The pattern here uses a sentinel
|
||||
(`errors.Is(err, ErrRetryable)`); production code often classifies by type
|
||||
(`errors.As(&netErr)`), by status code, or via an `interface{ Temporary() bool }`.
|
||||
Bring up **idempotency keys** as the real fix for retrying writes.
|
||||
|
||||
## The complete picture (say this at the whiteboard)
|
||||
|
||||
Retries alone are not a resilience strategy. The full stack:
|
||||
|
||||
1. **Cap the backoff** (`min(backoff, maxBackoff)`) — 2^20 seconds is not a plan.
|
||||
2. **Retry budgets** (e.g., retries ≤ 10% of requests, à la the SRE book / Finagle)
|
||||
beat per-request counts — they bound *aggregate* amplification.
|
||||
3. **Circuit breaker** — stop calling a dead dependency entirely; probe
|
||||
half-open. Backoff protects *them*, the breaker protects *you*.
|
||||
4. **Deadline propagation** — retrying past the caller's deadline serves
|
||||
responses nobody is waiting for. Notice the ctx check does this for free.
|
||||
5. **Only retry at one layer.** Client retries × mesh retries × app retries =
|
||||
exponential amplification. Pick an owner.
|
||||
@@ -0,0 +1,53 @@
|
||||
// Exercise 06 — Retry with Exponential Backoff + Jitter
|
||||
//
|
||||
// ASSIGNMENT
|
||||
// ----------
|
||||
// Implement Retry(ctx, attempts, base, fn):
|
||||
//
|
||||
// 1. Calls fn up to `attempts` times; returns nil on first success.
|
||||
// 2. Retries ONLY errors matching ErrRetryable (errors.Is — fn may wrap it).
|
||||
// Non-retryable errors are returned immediately.
|
||||
// 3. Between attempt i and i+1, sleeps a random duration in
|
||||
// [0, base * 2^i) — exponential backoff with FULL JITTER.
|
||||
// 4. The sleep must be interruptible by ctx: if ctx is cancelled mid-sleep,
|
||||
// return promptly with an error that matches ctx.Err() (errors.Is).
|
||||
// 5. If all attempts fail, return an error that wraps the last error from fn.
|
||||
// 6. Never sleep after the final attempt.
|
||||
//
|
||||
// Run the tests: go test -race ./06-retry-backoff/
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
// ErrRetryable marks transient failures. fn returns errors wrapping this
|
||||
// to request a retry.
|
||||
var ErrRetryable = errors.New("retryable")
|
||||
|
||||
// Retry runs fn with exponential backoff + full jitter.
|
||||
// TODO: implement me.
|
||||
func Retry(ctx context.Context, attempts int, base time.Duration, fn func(context.Context) error) error {
|
||||
// TODO:
|
||||
// - loop attempts times, call fn
|
||||
// - errors.Is(err, ErrRetryable) to classify
|
||||
// - backoff := base << i ; sleep := rand in [0, backoff)
|
||||
// - select { time.After(sleep) / ctx.Done() }
|
||||
// - wrap the final error with fmt.Errorf("... %w", err)
|
||||
return nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
i := 0
|
||||
err := Retry(context.Background(), 5, 50*time.Millisecond, func(ctx context.Context) error {
|
||||
i++
|
||||
if i < 3 {
|
||||
return fmt.Errorf("attempt %d flaked: %w", i, ErrRetryable)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
fmt.Println("attempts:", i, "err:", err)
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestSucceedsAfterTransientFailures(t *testing.T) {
|
||||
calls := 0
|
||||
err := Retry(context.Background(), 5, time.Millisecond, func(ctx context.Context) error {
|
||||
calls++
|
||||
if calls < 3 {
|
||||
return fmt.Errorf("flaky: %w", ErrRetryable)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("want success, got %v", err)
|
||||
}
|
||||
if calls != 3 {
|
||||
t.Fatalf("want 3 calls, got %d", calls)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNonRetryableStopsImmediately(t *testing.T) {
|
||||
fatal := errors.New("permission denied")
|
||||
calls := 0
|
||||
err := Retry(context.Background(), 5, time.Millisecond, func(ctx context.Context) error {
|
||||
calls++
|
||||
return fatal
|
||||
})
|
||||
if calls != 1 {
|
||||
t.Fatalf("non-retryable error must stop retries: %d calls", calls)
|
||||
}
|
||||
if !errors.Is(err, fatal) {
|
||||
t.Fatalf("returned error should match/wrap the fatal error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExhaustionWrapsLastError(t *testing.T) {
|
||||
calls := 0
|
||||
err := Retry(context.Background(), 3, time.Millisecond, func(ctx context.Context) error {
|
||||
calls++
|
||||
return fmt.Errorf("boom %d: %w", calls, ErrRetryable)
|
||||
})
|
||||
if calls != 3 {
|
||||
t.Fatalf("want 3 attempts, got %d", calls)
|
||||
}
|
||||
if err == nil || !errors.Is(err, ErrRetryable) {
|
||||
t.Fatalf("exhaustion error must wrap the last attempt error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestContextCancelsSleep(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
go func() {
|
||||
time.Sleep(30 * time.Millisecond)
|
||||
cancel()
|
||||
}()
|
||||
|
||||
start := time.Now()
|
||||
err := Retry(ctx, 10, 10*time.Second, func(ctx context.Context) error {
|
||||
return fmt.Errorf("always: %w", ErrRetryable)
|
||||
})
|
||||
elapsed := time.Since(start)
|
||||
|
||||
if elapsed > time.Second {
|
||||
t.Fatalf("Retry ignored ctx during backoff sleep: took %v (time.Sleep instead of select?)", elapsed)
|
||||
}
|
||||
if err == nil || !errors.Is(err, context.Canceled) {
|
||||
t.Fatalf("want error matching context.Canceled, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBackoffActuallyWaits(t *testing.T) {
|
||||
start := time.Now()
|
||||
_ = Retry(context.Background(), 3, 40*time.Millisecond, func(ctx context.Context) error {
|
||||
return fmt.Errorf("nope: %w", ErrRetryable)
|
||||
})
|
||||
// Full jitter lower bound is 0, so we can't assert a minimum reliably —
|
||||
// but the loop must finish in bounded time: < base*(2^0+2^1+2^2) + slack.
|
||||
if elapsed := time.Since(start); elapsed > 500*time.Millisecond {
|
||||
t.Fatalf("backoff far too long (sleeping after last attempt?): %v", elapsed)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user