Skip to content

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

Nextdoor Engineering 7 min read Case study mediumAdept

Scaling Nextdoor’s Datastores: Part 4

Nextdoor prevents racing writers from leaving stale values in its look-aside Redis cache by versioning every database row and making cache writes conditional on that version. A Postgres trigger maintains a monotonic db_version column (1 on insert, +1 on update), and the application reads it back inside the same transaction because Django's ORM doesn't return updated rows. The version travels as a metadata header on the cached value, and Redis Lua functions (set_if_version, del_if_version) atomically compare and apply updates, rejecting writes with older versions. Timestamps are rejected as version sources because they are neither unique nor reliably monotonic.

Related posts

  • Scaling Nextdoor’s Datastores: Part 1

    Nextdoor Engineering easyNovice

    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.

  • 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.

  • Scaling Nextdoor’s Datastores: Part 3

    Nextdoor Engineering mediumAdept

    Nextdoor replaced pickle-style byte serialization of Django ORM objects in its Redis/Valkey look-aside cache with MessagePack, so cache entries stay readable across code releases and schema migrations. Pickled objects are tied to runtime and package versions; deserialization failures after a deploy or migration turn into mass cache misses and a thundering herd on the database. With MessagePack, older code ignores unknown new fields (forward compatibility) and newer code fills missing fields with required defaults (backward compatibility); field removals go through Django's SeparateDatabaseAndState. MessagePack was slower than pickle in isolation but had no noticeable effect on request latency, produced smaller payloads, and each value carries a 10-byte header (2-byte format metadata plus 8-byte object version) to support future changes such as compression.

  • 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.

  • Tracing Cache Inconsistencies to Their Root Cause

    Nextdoor Engineering mediumAdept

    Nextdoor extended its versioned Valkey ORM cache so every conditional write also atomically stores a short-lived verification key (receipt) recording the operation (fill_on_write or fill_on_miss), writer service, trace ID and replaced version, colocated via a hash tag in the same cluster slot. The CDC-driven reconciler reads receipts for both the checked and cached versions during its two passes, classifying each inconsistency as an expected race, a stale fill, or a missing cache write, and emits one per-model metric. Receipts expire after pass 2 or within minutes, and are skipped where the reconciler never checks. The approach exposed three non-cache bugs: a silent context-cancellation failure in the reconciler, an ORM save() falling back to INSERT and resurrecting deleted rows, and redelivered long-running background jobs writing stale versions.

The quest for software engineering knowledge.

Look:
Layout: