Skip to content

In development. Expect rough edges and changes. The source code is not public yet.

Nextdoor Engineering 4 min read Case study mediumAdept

Scaling Nextdoor’s Datastores: Part 2

Letting product engineers manually choose replica vs. primary routing in Django broke down as abstractions hid read-after-write dependencies, and the common workaround of wrapping logic in transactions sent everything to the primary, slowly eroding the capacity gained from read replicas. Nextdoor replaced manual routing with ORM-level tracking of which tables a web request has written to, automatically sending subsequent reads of those tables to the primary and everything else to replicas. Because average replication lag (~20ms) was far shorter than request duration, they refined this with a timer that makes tables replica-eligible again after p99.9 replication lag elapses, moving most read queries off the primary.

Related posts

  • Don't add a read replica until you've read this

    incident.io mediumAdept

    incident.io moved nearly all Postgres read traffic to a read replica and had to preserve read-after-write consistency without hand-routing each query. Within a request, a Go context flag is tainted on any write or transaction so later reads go to the primary, a simple SELECT-prefix check classifies queries, and a circuit breaker falls back to the primary if the replica is unreachable. Across async boundaries, 0.1–0.5% of queue messages hit NotFound errors because workers ran faster than replication. The fix stamps each message and each mutating API actor (user or API key) with the primary's WAL LSN and checks pg_last_wal_replay_lsn() before reading from the replica. Lagging messages are nacked with exponential backoff, which doubles as backpressure, and replica lag is best monitored in bytes rather than time.

  • Scaling Prisma applications with Neon read-only replicas

    Neon mediumAdept

    Neon read-only replicas are independent compute instances that read from the same shared storage as the read-write compute. Because no data is copied via WAL replay as in traditional Postgres, they add no storage cost and are up to date at startup. Prisma apps can route reads to them with the @prisma/extension-read-replicas Client extension, which picks a random replica when several URLs are given. Writes and $transaction queries go to the primary, and $primary() forces a read against the primary when fresh data is required.

  • When to use Read Replicas vs. bigger compute

    Supabase mediumAdept

    Choosing between vertical scaling and Postgres Read Replicas comes down to diagnosing the bottleneck first, using pg_stat_statements, read/write ratio queries, CPU utilization and EXPLAIN ANALYZE. Upgrade compute for write-heavy workloads, simple fixes with no code changes, or when tier headroom remains. Add replicas to isolate analytics, serve distant regions, or scale reads past the 16XL ceiling. Worked cost comparisons show replicas becoming more cost-effective at higher tiers. Long analytics queries on replicas need tuning of max_standby_streaming_delay, with hot_standby_feedback as a last resort because it bloats the primary. Connection pooling, CDC-based Pipelines and Multigres sharding are covered as alternatives.

  • How to Minimise the Impact of Database Latency

    Neon mediumAdept

    Database round-trip latency compounds with every sequential query, so co-locating the backend with the database and cutting round trips matters more than raw query speed. Neon's open-source Regional Latency Dashboard gives P50/P99 latencies between hosting providers and Neon regions, which can be multiplied by query count to estimate N+1 costs. The fixes are JOINs or bounded IN queries instead of N+1 loops, Django's select_related/prefetch_related, batching queries in one transaction (45% faster in a simple benchmark), Postgres RETURNING and ON CONFLICT clauses, connection reuse (8-9x lower response times in a Django sample), and application-level connection pools (10x+ throughput).

  • Scaling Nextdoor’s Datastores: Part 5

    Nextdoor Engineering mediumAdept

    Nextdoor closes cache consistency gaps in its look-aside Redis cache by consuming the PostgreSQL change data stream and issuing version-conditional del_if_version deletes whenever writers fail to update the cache. Because deletes are guarded by row versions, changes can be processed out of order and horizontally scaled. A race between cache fills on read misses and missed writes is handled with two reconcilers: one near-real-time, and one delayed slightly beyond the web request timeout to mop up stale refills. The pipeline is pg-bifrost republishing the WAL into Kafka, with a Go consumer that uses a time wheel for the delayed pass.

The quest for software engineering knowledge.

Look:
Layout: