Engineering Snippets

Production-oriented engineering patterns for reliability, distributed systems, databases, backend systems, observability, security, infrastructure, and AI systems. Each snippet starts with the problem it solves and gives the smallest useful implementation—not a framework-shaped demo.

These are design-review starting points. Keep the invariant; adapt the operational limits, failure policy, and observability to your system.

33 production patterns

Reliability · TypeScript

Bounded Retry with Backoff + Jitter#

A retry spends the same dependency capacity that may already be failing.

import { setTimeout as sleep } from 'node:timers/promises'

async function retry<T>(operation: (signal: AbortSignal) => Promise<T>, retryable: (error: unknown) => boolean, caller: AbortSignal, deadlineMs = 2_000) {
  const deadline = Date.now() + deadlineMs
  const signal = AbortSignal.any([caller, AbortSignal.timeout(deadlineMs)])
  for (let attempt = 0; ; attempt++) {
    try { return await operation(signal) }
    catch (error) {
      if (signal.aborted) throw signal.reason
      const delay = Math.min(50 * 2 ** attempt, 500) * (0.5 + Math.random())
      if (!retryable(error) || Date.now() + delay >= deadline || attempt >= 4) throw error
      await sleep(delay, undefined, { signal })
    }
  }
}

Invariant: Never retry after the caller’s deadline.

Reliability · TypeScript

Abortable Polling Loop#

The owner of repeated work must also own its cancellation and cadence.

function sleep(ms: number, signal: AbortSignal) {
  signal.throwIfAborted()
  return new Promise<void>((resolve, reject) => {
    const done = () => { clearTimeout(timer); signal.removeEventListener('abort', abort) }
    const abort = () => { done(); reject(signal.reason) }
    const timer = setTimeout(() => { done(); resolve() }, ms)
    signal.addEventListener('abort', abort, { once: true })
  })
}

async function poll(load: (signal: AbortSignal) => Promise<void>, retryable: (error: unknown) => boolean, intervalMs: number, signal: AbortSignal) {
  while (!signal.aborted) {
    const started = Date.now()
    try { await load(signal) }
    catch (error) {
      if (signal.aborted) throw signal.reason
      if (!retryable(error)) throw error
    }
    await sleep(Math.max(0, intervalMs - (Date.now() - started)), signal)
  }
}

Invariant: At most one poll is in flight, and cancellation stops all future work.

Distributed systems · TypeScript + SQL

Idempotent Command Reservation#

Ambiguous delivery must not become an ambiguous business effect.

return db.transaction(async (tx) => {
  const owner = await tx.oneOrNone(
    'INSERT INTO idempotency_keys (tenant_id, key, request_hash, status) VALUES ($1,$2,$3,\'running\') ON CONFLICT (tenant_id,key) DO NOTHING RETURNING key',
    [tenantId, key, requestHash],
  )
  if (!owner) return replayOrConflict(tx, tenantId, key, requestHash)

  const response = await applyCommand(tx, command)
  await tx.query(
    'UPDATE idempotency_keys SET status=\'completed\', response=$3 WHERE tenant_id=$1 AND key=$2',
    [tenantId, key, response],
  )
  return response // reservation, domain write, and response commit together
})

Invariant: One logical command produces at most one committed business effect.

Distributed systems · SQL

Recoverable Outbox Claim#

Parallel delivery needs exclusive claims, stable event identity, and recovery after abandoned work.

WITH claimed AS (
  SELECT id FROM outbox
  WHERE published_at IS NULL
    AND available_at <= now()
    AND (claimed_until IS NULL OR claimed_until < now())
  ORDER BY id
  FOR UPDATE SKIP LOCKED
  LIMIT 100
)
UPDATE outbox o
SET claimed_by = $1, claimed_until = now() + interval '30 seconds'
FROM claimed
WHERE o.id = claimed.id
RETURNING o.*;

Invariant: No healthy worker reclaims a row before its lease expires; event identity remains stable after recovery.

Mutual exclusion is useful only while ownership remains bound to one live session.

SELECT pg_try_advisory_lock($1::int, $2::int) AS acquired;
-- Exit cleanly when acquired = false.
-- Run work on this dedicated database session inside try/finally.
SELECT pg_advisory_unlock($1::int, $2::int);

Invariant: Only one database session owns the job at a time.

A cursor is a position in a total order, not a page number.

SELECT id, created_at, payload
FROM events
WHERE tenant_id = $1
  AND (created_at, id) < ($2, $3)
ORDER BY created_at DESC, id DESC
LIMIT 50;
-- Cursor = base64url(last.created_at + ',' + last.id)

Invariant: Every page continues after a unique, immutable ordering key.

Zero-downtime migration is a compatibility protocol between deployed versions.

-- Deploy 1: expand
ALTER TABLE accounts ADD COLUMN display_name text;
-- Application dual-writes old_name and display_name.
-- Backfill in bounded, restartable batches.
UPDATE accounts SET display_name = old_name
WHERE id > $1 AND id <= $2 AND display_name IS NULL;
-- Deploy 2: read new column; validate; stop old writes.
-- Deploy 3: contract only after rollback window closes.

Invariant: Every deployed application version remains compatible with the active schema.

A decision is valid only against the version of state it observed.

UPDATE subscriptions
SET plan = $1, version = version + 1, updated_at = now()
WHERE id = $2 AND version = $3
RETURNING *;
-- Zero rows means conflict: reload state; do not silently retry the decision.

Invariant: A write succeeds only against the exact version the decision observed.

Backend · TypeScript

Request Deadline Propagation#

Downstream work inherits the caller’s remaining time; it does not receive a new budget.

async function withDeadline<T>(parent: AbortSignal, remainingMs: number, work: (signal: AbortSignal) => Promise<T>) {
  if (remainingMs <= 0) throw new Error('deadline exceeded')
  const signal = AbortSignal.any([parent, AbortSignal.timeout(remainingMs)])
  return work(signal)
}

await withDeadline(request.signal, remainingMs, signal => fetch(url, { signal }))

Invariant: No downstream operation outlives the remaining end-to-end budget.

Backend · TypeScript

Graceful HTTP Shutdown#

Termination is an admission-control transition followed by a bounded drain.

let draining = false
app.get('/ready', (_, res) => res.sendStatus(draining ? 503 : 204))

process.on('SIGTERM', () => {
  draining = true
  server.close((error) => process.exit(error ? 1 : 0))
  setTimeout(() => process.exit(1), 25_000).unref()
})

Invariant: No new work is accepted after readiness fails; in-flight work gets a bounded drain window.

Observability · TypeScript

Structured Boundary Log#

A boundary log is durable causal evidence, not a prose debugging message.

logger.info({
  event: 'payment_authorized',
  trace_id: traceId,
  request_id: requestId,
  tenant_id: tenantId,
  payment_id: paymentId,
  provider: 'stripe',
  duration_ms: performance.now() - started,
  outcome: 'success',
})

Invariant: Every emitted boundary event has stable causal identity, outcome, and duration fields.

Observability · PromQL

SLO Error Ratio#

An SLI is valid only when errors and totals describe the same event population.

sum(rate(http_requests_total{service="api",code=~"5.."}[5m]))
/
sum(rate(http_requests_total{service="api"}[5m]))
-- Alert with both a fast and slow burn window to control noise.

Invariant: Errors and total requests cover the same population and time window.

Cache expiry must not synchronize callers into an origin outage.

const cached = await cache.get(key)
if (cached?.freshUntil > Date.now()) return cached.value
if (cached?.staleUntil > Date.now()) {
  if (await lease.tryAcquire(key, 10_000)) {
    void refresh(key).finally(() => lease.release(key))
  }
  return cached.value
}
// A miss also needs shared single-flight; do not fan out origin loads.
return lease.runExclusive(key, () => loadAndCache(key))

Invariant: One refresh occurs per hot key while callers receive bounded-stale data.

Security · Redis Lua

Atomic Sliding-Window Limit#

Admission and accounting must be one atomic decision at the scarce-resource boundary.

local key, now, window, limit = KEYS[1], ARGV[1], ARGV[2], tonumber(ARGV[3])
redis.call('ZREMRANGEBYSCORE', key, 0, now - window)
if redis.call('ZCARD', key) >= limit then return 0 end
redis.call('ZADD', key, now, now .. '-' .. ARGV[4])
redis.call('PEXPIRE', key, window)
return 1

Invariant: Admission and accounting happen atomically for one principal and window.

Security · TypeScript

SSRF-Safe URL Admission#

Redirects are new destinations, not continuations of the original authorization.

// Admission returns the address the transport MUST connect to.
async function authorize(input: string) {
  const url = new URL(input)
  if (url.protocol !== 'https:') throw new Error('protocol denied')
  const hostname = url.hostname.replace(/^\[|\]$/g, '')
  const family = net.isIP(hostname)
  const answers = family
    ? [{ address: hostname, family }]
    : await dns.promises.lookup(hostname, { all: true, verbatim: true })
  const allowed = answers.filter(({ address }) => isPublicRoutable(normalizeIp(address)))
  if (!allowed.length) throw new Error('destination denied')
  return { url, hostname, ...allowed[crypto.randomInt(allowed.length)] }
}

const target = await authorize(input)
const request = https.request({
  hostname: target.address, family: target.family,
  port: target.url.port || 443,
  path: target.url.pathname + target.url.search,
  headers: { host: target.url.host },
  servername: net.isIP(target.hostname) ? undefined : target.hostname,
  rejectUnauthorized: true, signal: AbortSignal.timeout(2_000),
})
request.end()
// Disable automatic redirects. Re-run authorize() for every Location.
// Enforce a response-byte ceiling before buffering the body.
// A proxy can bypass this pin: enforce the same policy at actual egress.

Invariant: Every network destination actually connected to is authorized before the connection is established.

Infrastructure · YAML

Kubernetes Disruption Budget#

Voluntary maintenance must consume an explicit availability budget.

apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
  name: api
spec:
  minAvailable: 2
  selector:
    matchLabels:
      app: api
# Requires at least 3 replicas; pair with readiness and topology spread.

Invariant: Voluntary disruption never removes more healthy replicas than the service can tolerate.

AI infrastructure · TypeScript

Bounded Agent Execution#

A model proposes actions; deterministic policy owns authority, limits, and termination.

const budget = { steps: 12, tokens: 24_000, toolMs: 30_000, spendUsd: 0.40 }
for (const step of plan) {
  const reservation = usage.reserve(step.maximumUsage, budget)
  authorize({ actor, tool: step.tool, resource: step.resource })
  if (isIrreversible(step)) await requireApproval(runId, step)
  const result = await execute(step, { timeoutMs: reservation.toolMs })
  usage.settle(reservation, result.usage)
  audit.append({ runId, step, result: redact(result) })
}

Invariant: No step starts without reserved budget and authorization for its exact tool and resource.

AI infrastructure · TypeScript

Validated LLM Decision Boundary#

Structured model output remains untrusted until deterministic gates accept it.

const Decision = z.object({
  action: z.enum(['approve', 'review', 'reject']),
  confidence: z.number().min(0).max(1),
  evidenceIds: z.array(z.string()).max(10),
})

const decision = Decision.parse(JSON.parse(modelOutput))
if (decision.action === 'approve' && decision.confidence < 0.95) decision.action = 'review'
assertEvidenceExists(decision.evidenceIds)
authorize({ actor, action: decision.action, resource })
await applyDecision(decision) // deterministic, audited, idempotent boundary

Invariant: Invalid or unauthorized model output never reaches a side effect.

Reliability · TypeScript

Capacity-Protecting Circuit Breaker#

A dependency failure should consume a bounded share of caller capacity, not every available request slot.

type State = { mode: 'closed' | 'open' | 'half-open'; failures: number; retryAt: number }

async function protectedCall<T>(key: string, operation: () => Promise<T>) {
  const state = await breaker.read(key)
  if (state.mode === 'open' && Date.now() < state.retryAt)
    throw new DependencyUnavailable(key)

  // compare-and-set admits one half-open probe across the fleet
  if (state.mode === 'open' && !await breaker.tryProbe(key, state))
    throw new DependencyUnavailable(key)

  try {
    const result = await operation()
    await breaker.recordSuccess(key)
    return result
  }
  catch (error) {
    await breaker.recordFailure(key, classify(error))
    throw error
  }
}

Invariant: When open, no ordinary request reaches the unhealthy dependency; probes remain bounded.

Distributed systems · SQL

Distributed Lease with Fencing#

Lease expiry revokes ownership only when downstream writes reject stale owners.

-- Acquisition increments a monotonic token atomically.
UPDATE job_leases
SET owner_id = $2,
    fencing_token = fencing_token + 1,
    expires_at = now() + interval '30 seconds'
WHERE resource_id = $1 AND expires_at < now()
RETURNING fencing_token;

-- Every protected write rejects stale owners.
UPDATE protected_resources
SET value = $2, last_fencing_token = $3
WHERE id = $1 AND last_fencing_token < $3;
-- Zero rows: ownership is stale; stop immediately.

Invariant: Only the holder of the greatest issued fencing token may commit protected work.

Retention becomes reliable when the physical deletion unit matches the policy boundary.

-- Lifecycle worker first proves the partition is expired and unheld.
BEGIN;
SELECT pg_advisory_xact_lock(hashtext('retention:request_logs_2026_05'));

ALTER TABLE request_logs
  DETACH PARTITION request_logs_2026_05;

INSERT INTO retention_audit(partition_name, detached_at, policy_version)
VALUES ('request_logs_2026_05', now(), 'logs-v4');
COMMIT;

-- Verify downstream and hold constraints, then drop in a later step.
DROP TABLE request_logs_2026_05;

Invariant: No unheld record remains queryable after its retention partition becomes eligible for removal.

Infrastructure · TypeScript

Statically Stable Release Kill Switch#

A release control must remain operable when the released dependency is unhealthy.

type Snapshot = { version: number; validUntil: number; flags: Record<string, boolean>; signature: string }

async function refreshPolicy() {
  const candidate = await controlPlane.fetch({ signal: AbortSignal.timeout(1_000) })
  verifySignature(candidate)
  if (candidate.version <= current.version) return
  await snapshots.writeAtomically(candidate)
  current = candidate
}

function enabled(flag: string) {
  if (Date.now() <= current.validUntil) return current.flags[flag] ?? false
  return safeDefaults[flag] ?? false // explicit per-feature failure policy
}

Invariant: The last known-good release policy remains readable without the control plane.

AI infrastructure · TypeScript

Policy-Gated AI Model Router#

Model selection begins with eligibility; cost and latency optimize only among proven-safe candidates.

const eligible = registry.models.filter(model =>
  model.capabilities.hasAll(task.required)
  && model.regions.includes(task.dataRegion)
  && evals.passes(model.version, task.evaluationSet, task.minimumScore)
  && policy.allows(task.risk, model),
)

const ranked = eligible.sort((a, b) => score(a, task) - score(b, task))
for (const model of ranked) {
  const reservation = await capacity.tryReserve(model, task.maximumUsage)
  if (!reservation) continue
  try { return await infer(model, task, { signal: deadlineSignal }) }
  catch (error) { if (!isSafeFallback(error)) throw error }
  finally { await capacity.settle(reservation) }
}
return deterministicFallback(task) // never route to an ineligible model

Invariant: Every selected model satisfies the task’s capability, policy, evaluation, budget, and deadline constraints.

Checkpoint tuning is an I/O budget shared with foreground transactions and recovery.

SELECT now() AS sampled_at,
       num_timed, num_requested,
       write_time, sync_time, buffers_written
FROM pg_stat_checkpointer;

-- Store samples and alert on deltas, not lifetime totals:
-- requested/timed ratio -> WAL pressure
-- sync_time/checkpoints  -> durability-path latency
-- correlate with WAL bytes, disk latency, p99, and replica lag.

Invariant: Checkpoint rate and duration remain inside the tested foreground-latency and disk-headroom budget.

Distributed systems · TypeScript

Kafka External-Effect Identity#

Kafka delivery identity must become stable business-operation identity before an external effect.

const key = `order:${event.orderId}:capture:v1`
const fingerprint = sha256(canonicalJson(event.payment))
const known = await operations.get(key)
if (known && known.fingerprint !== fingerprint) throw new KeyReuseConflict()

const result = await payments.capture({
  idempotencyKey: key,
  amount: event.payment.amount,
})
await operations.record(key, fingerprint, result.providerId)
// Commit the offset after durable evidence. On ambiguity, query by key.

Invariant: One logical Kafka record produces at most one externally committed business effect.

Infrastructure · YAML

Kubernetes Drain Budget#

A termination grace period budgets routing convergence and owned-work drainage.

spec:
  terminationGracePeriodSeconds: 40
  containers:
    - name: api
      lifecycle:
        preStop:
          httpGet: { path: /begin-drain, port: 8080 }
      readinessProbe:
        httpGet: { path: /ready, port: 8080 }
# /begin-drain returns quickly, fails readiness, and stops new claims.
# Budget = routing convergence + maximum drain + safety margin.
# preStop and process shutdown share the same 40 seconds.

Invariant: Owned work exits or becomes safely reclaimable before termination grace expires.

An index earns admission only after its recurring write cost is counted.

SELECT i.indexrelname, i.idx_scan,
       pg_size_pretty(pg_relation_size(i.indexrelid)) AS size,
       t.n_tup_upd, t.n_tup_hot_upd,
       round(100.0*t.n_tup_hot_upd/nullif(t.n_tup_upd,0),1) AS hot_pct
FROM pg_stat_user_indexes i
JOIN pg_stat_user_tables t ON t.relid = i.relid
WHERE i.relname = 'orders'
ORDER BY pg_relation_size(i.indexrelid) DESC;
-- Pair with EXPLAIN (ANALYZE, BUFFERS) for the beneficiary query.
-- Note statistics resets and replica-only usage.

Invariant: Every index has a measured beneficiary, observed write cost, owner, and removal condition.

AI infrastructure · TypeScript

Versioned Prompt Prefix Layout#

Reusable context belongs in a stable prefix; volatile evidence belongs at the edge.

const promptVersion = 'sales-agent/policy/v7'
const input = [
  systemPolicy,             // stable application policy
  toolSchemas,             // stable contracts
  `Prompt-Version: ${promptVersion}`,
  tenantPolicy(tenantId),   // isolated tenant layer
  sessionSummary,          // compact memory
  retrievedEvidence,       // volatile, untrusted
  userRequest,             // volatile
]
// Record model, version, cached tokens, outcome, and latency.
// A cache miss must affect only cost or latency.

Invariant: A cache miss changes cost or latency, never policy, authority, or correctness.

Database · TypeScript

Serializable Transaction Retry#

A serialization abort invalidates the whole decision, not only the statement that noticed it.

async function serializable<T>(run: (db: Client) => Promise<T>) {
  for (let attempt = 1; attempt <= 4; attempt++) {
    const db = await pool.connect()
    try {
      await db.query('BEGIN ISOLATION LEVEL SERIALIZABLE')
      const result = await run(db) // repeat the entire decision
      await db.query('COMMIT')
      return result
    } catch (error) {
      await db.query('ROLLBACK').catch(() => {})
      if (sqlState(error) !== '40001' || attempt === 4) throw error
      await sleep(Math.random() * 20 * attempt)
    } finally { db.release() }
  }
  throw new Error('unreachable')
}

Invariant: Every attempt recomputes the business decision from a fresh serializable snapshot and stops inside a bounded retry budget.

Infrastructure · YAML

Zone Spread + Disruption Budget#

Availability requires placement and disruption policy to describe the same failure domain.

apiVersion: apps/v1
kind: Deployment
metadata: { name: checkout }
spec:
  replicas: 3
  template:
    metadata: { labels: { app: checkout } }
    spec:
      topologySpreadConstraints:
        - maxSkew: 1
          topologyKey: topology.kubernetes.io/zone
          whenUnsatisfiable: DoNotSchedule
          labelSelector: { matchLabels: { app: checkout } }
---
apiVersion: policy/v1
kind: PodDisruptionBudget
metadata: { name: checkout }
spec:
  minAvailable: 2
  selector: { matchLabels: { app: checkout } }

Invariant: A voluntary disruption cannot reduce ready capacity below two replicas, and replicas remain spread across zones when capacity exists.

AI infrastructure · TypeScript

KV-Cache Admission Budget#

Tokens are capacity reservations; reject work that cannot finish inside its cache and deadline budget.

type Request = { promptTokens: number; maxOutputTokens: number; tier: 'interactive' | 'batch' }

async function admit(request: Request) {
  const requested = request.promptTokens + request.maxOutputTokens
  const limit = policy[request.tier].maxSequenceTokens
  if (requested > limit) throw new RequestTooLargeError(limit)

  const lease = await tokenBudget.tryReserve(request.tier, requested)
  if (!lease) throw new OverloadedError({ retryable: request.tier === 'batch' })
  try {
    const result = await inference.generate(request)
    await lease.settle(result.promptTokens + result.outputTokens)
    return result
  } finally { await lease.release() }
}

Invariant: No request begins prefill unless its maximum token reservation fits both its service-class limit and currently available capacity.

Distributed systems · TypeScript

Idempotent Durable Activity#

Durable orchestration records decisions, while idempotency protects effects outside the workflow engine.

export async function chargeOrder(input: ChargeInput) {
  const key = 'charge:' + input.orderId
  const prior = await operations.find(key)
  if (prior?.status === 'completed') return prior.result

  // The provider must persist the same key with the effect.
  const result = await payments.charge({
    amount: input.amount,
    idempotencyKey: key,
    signal: AbortSignal.timeout(10_000),
  })
  await operations.complete(key, result)
  return result
}

// Validation and authorization errors must be configured non-retryable.

Invariant: One logical activity command produces at most one external business effect across every retry.

Distributed systems · SQL

Monotonic Version Gate#

Delayed delivery must never make a replicated consumer move backward in committed state.

INSERT INTO tenant_policy (tenant_id, version, document)
VALUES ($1, $2, $3::jsonb)
ON CONFLICT (tenant_id) DO UPDATE
SET version = EXCLUDED.version,
    document = EXCLUDED.document
WHERE tenant_policy.version < EXCLUDED.version
RETURNING version;

-- Zero rows means the destination already holds this version or a newer one.
-- Use an authoritative monotonic version, never application wall-clock time.

Invariant: A destination accepts a state transition only when its version is greater than the currently committed version.

How I choose a snippet

A pattern belongs here when it removes ambiguity at a production boundary: retries need a deadline, writes need idempotency, migrations need compatibility, and agents need authority and budgets. For the reasoning behind these choices, read the engineering essays or use the interactive CTO Workbench.

>