← All tags

#patterns

14 entries tagged #patterns

  • Two Writes, One Row: Who Wins?

    Post · Sep 26, 2026

    Two requests read the same row, both do their maths, both write back. One of them silently disappears. How the system should resolve that isn't one answer: it depends on whether the write is a delta, a quick piece of logic, or a human edit made minutes after the read.

  • Migrating Schema-Per-Tenant Databases at Scale

    Post · Sep 12, 2026

    Choosing physical tenant isolation over a shared, RLS-scoped schema buys two new problems: knowing where a tenant's data actually lives, and running one migration correctly hundreds of times instead of once. Neither has an app-code fix, both need their own infrastructure.

  • Designing Multi-Tenant APIs That Scale

    Post · Sep 12, 2026

    A missing tenant filter is a data leak, not a crash. Row-level security fixes that structurally, but rate limits, connection pools, and error codes built for one instance break the same quiet way once the API runs as several.

  • Why Payment Retries Need Idempotency

    Post · Sep 11, 2026

    A plain payment endpoint looks correct until you trace what a double-click, a timed-out request, or a redelivered webhook actually does to it. Each one turns one payment into two. Idempotency keys are the fix, at two layers most write-ups skip.

  • Building a Typed Fetch Factory

    Post · Aug 25, 2026

    How a single createFetcher factory infers request/response types from an OpenAPI schema and layers in caching, retries, and cancellation, and why each piece is built the way it is.

  • Two Pointers

    Post · Aug 11, 2026

    Two indices walking through one ordered structure, discarding the side that cannot improve the answer at every step and replacing a nested loop with a single pass.

  • Retry with Backoff & Jitter

    Snippet · Sep 1, 2026

    A generic `retry(task, options)` that re-runs a failing async task with exponential backoff and full jitter, an optional `shouldRetry` predicate, and a cap on any single delay.

  • Typed Event Emitter

    Snippet · Sep 1, 2026

    A ~20-line pub/sub where the event names and each event's payload type come from one map, so `on` and `emit` are fully checked against it and `on` returns its own unsubscribe.

  • assertNever

    Snippet · Sep 1, 2026

    A one-line helper that makes a `switch` over a discriminated union a compile error the moment a case is added and left unhandled, with a runtime throw as the safety net for data that violated the types at the edges.

  • Result & tryCatch

    Snippet · Sep 1, 2026

    A `Result<T, E>` union plus `tryCatch` wrappers that turn a throwing call into a value the caller has to inspect: the failure path becomes part of the return type instead of an invisible jump.

  • useLocalStorage

    Snippet · Sep 1, 2026

    A `localStorage`-backed `useState` built on `useSyncExternalStore`: no hydration mismatch under SSR, live updates when another tab writes, and live updates when another hook on the same page writes.

  • useControllableState

    Snippet · Sep 1, 2026

    One hook that lets a component be driven from outside (`value` + `onChange`) or manage its own state (`defaultValue`), the same two-mode contract a native `<input>` has, while the component body only ever reads one value and calls one setter.

  • A Readable useToggle

    Snippet · Sep 1, 2026

    A boolean `useState` that hands back `on` / `off` / `toggle` instead of a raw setter, so call sites read as intent. Every function, and the controls object itself, stays referentially stable, safe to pass as props or list as effect dependencies without retriggering anything downstream.

  • Make a Write Endpoint Safely Retryable

    Cheatsheet · Sep 1, 2026

    A client that retries a POST after a timeout can create the resource twice or charge a card twice. An idempotency key lets the server spot the retry and replay the first response instead of doing the work again.