Skip to the guide

Keeping derived data in sync: search indexes, caches and read models

AdvancedPattern5 pages

A company's HR office keeps the one true file on every employee, while reception keeps a phone list, IT the email directory and security the badge list, each copied from HR's file. HR could phone every department with each change, or it can keep a numbered log of changes that each department reads at its own pace. A department that loses its list rebuilds it from a copy of the files plus the log since.

HR's file is the database, the source of truth, and the departments' lists are derived data: a search index, a cache, a read model such as a profile's grid of posts. Phoning each department is a dual write from the request. The numbered log is the database's own change log, read by change data capture, or an outbox table written in the same transaction, and each department's clerk is a consumer that keeps one copy. Still with me? Good. Every design past its first box adds copies, and interviewers listen for how each copy learns of a write and what it shows until it has. The move: write once to the source of truth, feed every other copy from its log in order per key, and keep a way to rebuild any copy from scratch.