Posts tagged “postgres”

  1. Why does one long-running transaction slow down the whole database?

    Databases keep old row versions for as long as any open transaction might need them. One idle transaction on an unrelated table made PostgreSQL's busiest table 200× bigger and its writes 3× slower; in MySQL it made the old transaction's own reads 250× slower. Measured, with the settings that…

    · 10 min read · Databases

  2. How do you keep two systems consistent without distributed transactions?

    An order in one database, its payment in another, and a process that can die between them. Dual writes, two-phase commit, sagas and the outbox, each run 1,000 times with crashes injected: what goes wrong, what recovers, and what two-phase commit's in-doubt transactions block.

    · 9 min read · Databases

  3. What does ACID actually promise, and what do you lose with BASE?

    ACID is four separate promises, and most 'NoSQL vs SQL' arguments mix them up. Money transfers in PostgreSQL, MongoDB and DynamoDB, with and without transactions, with crashes: what each letter bought, measured, and why atomic writes still aren't enough if your reads aren't snapshots.

    · 9 min read · Databases

  4. What's the cheapest disaster recovery that actually works?

    Backups you've never restored are a hope. Two numbers decide a disaster-recovery plan: how much data you can lose (RPO) and how long you can be down (RTO). Measured on a 1 GB PostgreSQL database: a nightly dump vs continuous WAL archiving, with the server and its disk deleted mid-write.

    · 9 min read · AWS

  5. How do queues schedule millions of delayed retries?

    "Retry this in 30 seconds", a million times over. A min-heap, a timing wheel, and a row in PostgreSQL with an index on its due time, measured: what each costs to add, cancel and fire, how late jobs run, and what survives a restart.

    · 11 min read · Data Structures

  6. Why isn't a Redis lock enough?

    A Redis lock is a lease: it expires whether or not its holder is done. Measured with Redis 7.4 and PostgreSQL 17: a paused holder overwriting newer data, a release that frees someone else's lock, a cron job running twice, and what fixes each.

    · 10 min read · Databases

  7. Should you lock the row or retry on conflict?

    Pessimistic locking (SELECT … FOR UPDATE) vs optimistic retries (a version column) vs a single atomic UPDATE, measured on PostgreSQL 17 at high and low contention, plus 50 buyers rushing the last 10 tickets.

    · 12 min read · Databases

  8. Why did adding a cache make our data wrong?

    A cache is a second copy of your data with its own rules, and every way it can disagree with the database shows up as a bug. Five failure modes measured with Redis in front of PostgreSQL, and the fix for each.

    · 11 min read · System Design

  9. Why does my Postgres query get slower as the table grows, even with an index?

    An index only helps when Postgres can stop early or touch few pages. Six measured reasons it can't, from 10K to 10M rows, and the fix for each.

    · 12 min read · Databases

  10. How do you generate unique IDs across many servers?

    Inserting 5 million rows with random UUIDv4 keys took 34 s and left a 196 MB index at 69% fill; time-ordered UUIDv7 took 21 s with a 150 MB index at 90%; plain bigint 24 s and 107 MB. Database sequences, UUIDv4, UUIDv7 and Snowflake-style IDs compared, measured on PostgreSQL, with when to use which.

    · 6 min read · System Design

  11. A user says a page has started lagging: how do you debug it end to end?

    Narrow it down before you open a profiler: who, which page, since when. Then measure one layer at a time (browser, network, server, database, dependencies) until one of them owns the time. Five server-side causes measured with OpenTelemetry traces, and the signature each leaves.

    · 12 min read · System Design

  12. How do you scale an API?

    Find the bottleneck first, then use the cheapest lever that removes it: fix the query, cache the read, pool the connections, add replicas, queue the slow writes, limit each client. Each lever measured on one machine: what it bought, and what it cost.

    · 15 min read · System Design

  13. Postgres or DynamoDB for a new SaaS?

    DynamoDB is superb at the questions you designed it for and expensive at the ones you didn't; PostgreSQL answers questions you haven't thought of yet. The same 200,000 orders in both: 0.5 read units for the planned query, 2,689 for an unplanned report that PostgreSQL ran in 13 ms without an index.

    · 9 min read · Databases

  14. CAP or PACELC: what trade-off do you make when nothing is broken?

    CAP describes the rare moment the network splits. PACELC adds the trade-off you make all day: a fresher answer costs latency. Measured on a PostgreSQL primary and replica: how often reading your own write is stale, what each synchronous_commit level costs, and what happens when the replica is gone.

    · 9 min read · Databases

  15. Read replicas, partitioning or sharding: what does each one fix?

    Three ways to outgrow one database, often confused. Replicas add read capacity and cost you freshness. Partitioning keeps one huge table manageable but adds no capacity. Sharding adds write capacity and costs you almost everything that was easy. Measured on PostgreSQL 17: replica staleness, and…

    · 8 min read · Databases

  16. How do you migrate a live database without downtime?

    Most outages during migrations aren't failed migrations; they're migrations that held a lock while traffic queued behind them. Measured on a 5M-row table under load: one-step changes that stalled every request for 2-8 seconds, and the online versions that stalled them for a fraction of a second…

    · 9 min read · System Design

  17. When should a write go through a queue instead of the API?

    Queue the work behind a write when it's slow, unreliable or can finish after you answer; keep it synchronous when the caller needs the result. And never publish from the request itself: use an outbox.

    · 11 min read · System Design

  18. Should you self-host your database or use RDS?

    RDS and Aurora buy you backups, point-in-time recovery, failover and patching for a premium on every instance-hour. Self-hosting on EC2 can cost far less, and makes every one of those jobs yours. When each is the right call, and what you must build before self-hosting is safe.

    · 7 min read · AWS

  19. How do you keep tenants apart when they share one database?

    How I let Postgres, not application code, keep tenants apart, so a forgotten tenant filter returns nothing instead of another customer's data: FORCE, non-owner roles, transaction-local settings, fail-closed policies, and tests that prove it.

    · 11 min read · System Design

  20. Which isolation level should you use, and what does each one let through?

    Ten concurrency scenarios, four isolation levels, one PostgreSQL 17 database. A measured matrix of what each level allows, and why the stricter levels move the work into your retry loop.

    · 12 min read · Databases