Skip to content

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

Nextdoor Engineering 5 min read Case study easyNovice

Scaling Nextdoor’s Datastores: Part 1

Nextdoor's Django/PostgreSQL backend outgrew the usual scaling fixes, and a move to distributed SQL was ruled out because multi-JOIN relational queries were spread across the codebase. A look-aside Redis cache, read replicas and splitting tables across databases by severing foreign keys each bought runway but brought their own problems. Those include uncacheable ORM queries, stale reads before writes, schema changes breaking cached data, and engineers defaulting to the primary. Partitioning also quietly broke atomicity for transactions that span databases. The team's 2024 goals are automatic routing of queries to replicas, cache fills from replicas, time-bounded eventual cache consistency and schema-tolerant cache serialization, which later parts of the series cover.

Assumes: relational databasesORMsread replicaslook-aside caching

Related posts

  • Scaling Nextdoor’s Datastores: Part 2

    Nextdoor Engineering mediumAdept

    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.

  • 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 architecture of Neki

    PlanetScale mediumAdept

    Neki shards unmodified PostgreSQL behind a single connection string. A Router speaks the Postgres wire protocol, parses and plans queries against the sharding layout, and either pushes joins down to shards or runs cross-shard nested-loop, hash or merge joins itself. Each instance runs a Sidecar that pools connections by lifetime (statement, transaction, session advisory lock) and a PostgresManager. Admin handles failover, pg_rewind and per-shard sync/async/cross-zone durability, while an authoritative shard keeps custom type OIDs consistent and etcd holds the Data Topology. A Replicator built on logical replication drives MoveTables, Reshard and shadow-table OnlineDDL, and the Router buffers retryable queries during cutovers and failovers.

  • How Figma’s databases team lived to tell the scale

    Figma mediumAdept

    Figma horizontally sharded its RDS Postgres stack after vertical partitioning ran out of room. Multi-terabyte tables were hurting vacuum reliability and approaching RDS IOPS limits. Instead of moving to CockroachDB, TiDB, Spanner, Vitess or NoSQL, the team built sharding on its existing Postgres fleet using several pieces: colocated table groups ("colos") that share a shard key such as UserID, FileID or OrgID; hash-based routing to avoid hotspots from auto-incrementing and Snowflake IDs; logical sharding via views, kept separate from physical shard splits; and a DBProxy query engine that parses and routes queries and also does load-shedding and request hedging. Deliberate tradeoffs included dropping atomic cross-shard transactions, accepting slower range scans, keeping rollback possible, and using full logical replication. Sharding the first table took nine months.

  • You should shard your database

    PgDog mediumAdept

    Sharding Postgres early avoids the operational pain of large single instances, illustrated by an outage where stale planner statistics caused a 300 GB table to be sequentially scanned. Attempts to fix it by raising default_statistics_target added planning time (0.2ms to ~1ms per query, significant at 100k QPS), while making autovacuum analyze more often increased IOPS. Splitting the table into 12 shards cuts writes, autovacuum work and scanned rows by 12x, making migrations like ALTER COLUMN TYPE BIGINT finish in seconds and lowering the cost of mistakes.

The quest for software engineering knowledge.

Look:
Layout: