---
title: "Scaling Nextdoor’s Datastores: Part 4"
description: "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."
url: "https://swe.quest/articles/scaling-nextdoor-s-datastores-part-4-160381ed2304f3a4"
canonical: "https://engblog.nextdoor.com/scaling-nextdoors-datastores-part-4-c9d3d3edcd34"
source: "Nextdoor Engineering"
published: "2025-03-19"
difficulty: "medium"
kind: "Case study"
reading_minutes: 7
tags: ["caching","consistency","postgres","concurrency"]
---

# Scaling Nextdoor’s Datastores: Part 4

A post on [Nextdoor Engineering](https://swe.quest/sources/nextdoor.md), published 2025-03-19, 7 min read, Case study. Original: [https://engblog.nextdoor.com/scaling-nextdoors-datastores-part-4-c9d3d3edcd34](https://engblog.nextdoor.com/scaling-nextdoors-datastores-part-4-c9d3d3edcd34)

## Summary

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.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [Caching](https://swe.quest/tags/caching.md) (Backend & APIs)
- [Consistency models](https://swe.quest/tags/consistency.md) (Distributed systems)
- [PostgreSQL](https://swe.quest/tags/postgres.md) (Data & databases)
- [Concurrency](https://swe.quest/tags/concurrency.md) (Compilers & runtimes)

## Prerequisites

- look-aside caching ([easy posts on Caching](https://swe.quest/tags/caching.md?difficulty=easy))
- Redis basics ([easy posts on Caching](https://swe.quest/tags/caching.md?difficulty=easy))
- Postgres triggers ([easy posts on PostgreSQL](https://swe.quest/tags/postgres.md?difficulty=easy))

## Keywords

Redis, Lua, PostgreSQL, Django, Nextdoor

## Related posts

- [Scaling Nextdoor’s Datastores: Part 1](https://swe.quest/articles/scaling-nextdoor-s-datastores-part-1-2e24faaf7f855796.md): Nextdoor Engineering · 2025-03-19 · Easy · 5 min read · Scaling, PostgreSQL, Caching, Replication, Consistency models
  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.
- [Scaling Nextdoor’s Datastores: Part 5](https://swe.quest/articles/scaling-nextdoor-s-datastores-part-5-9c2a55c33ba04857.md): Nextdoor Engineering · 2025-03-19 · Medium · 8 min read · Caching, Consistency models, Event-driven architecture, PostgreSQL, Scaling
  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.
- [Scaling Nextdoor’s Datastores: Part 3](https://swe.quest/articles/scaling-nextdoor-s-datastores-part-3-5cb25113d465521d.md): Nextdoor Engineering · 2025-03-19 · Medium · 5 min read · Caching, Python, Schema migrations, Databases
  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.
- [Scaling Nextdoor’s Datastores: Part 2](https://swe.quest/articles/scaling-nextdoor-s-datastores-part-2-3bede3a52d4cf43e.md): Nextdoor Engineering · 2025-03-19 · Medium · 4 min read · Replication, Consistency models, Databases, Scaling
  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.
- [Tracing Cache Inconsistencies to Their Root Cause](https://swe.quest/articles/tracing-cache-inconsistencies-to-their-root-cause-0cc21b10b814cb9e.md): Nextdoor Engineering · 2026-11-23 · Medium · 7 min read · Caching, Consistency models, Observability, PostgreSQL, Debugging
  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.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
