How Targeted DNS Builds Cut Cloudflare's Memory Use
Changing one DNS setting sounds small. In a system serving enormous zones, it can trigger surprisingly large work. Cloudflare’s Zone Builder used to respond to many setting changes by reading every record in the zone, creating the desired edge representation, and comparing it against what was already published. Its final write might be tiny, but producing that diff could consume the memory and time needed to process millions of records.
Cloudflare describes a redesign that makes the build proportional to the part of the zone that can actually change. The result is a useful study in selective invalidation: how to do less work without leaving stale answers at the edge.
The path from a change to a DNS answer
A customer edits a record or setting. The change enters a database, produces a Kafka event, and passes through an aggregator to the Zone Builder. The builder reads current state, converts it into the keys Cloudflare’s authoritative DNS servers use, compares those keys with Quicksilver, and writes the differences. Quicksilver then distributes the data across the network.
This is a build pipeline, rather than a direct copy of database rows. DNS answers depend on records, zone settings, and serving rules. A setting such as Universal SSL can affect CAA records at the zone apex even though nobody edited a CAA row. That is why an event labeled only “zone changed” was once enough to justify rebuilding everything.
The scale made the old default expensive. Cloudflare says the builder now receives about 700 record changes and 100 other events each second. Some zones exceed 20 million records. A full build reads, encodes, and compares every record, holding the current and existing representations in memory. A small final diff does not make the preceding computation small.
Turn an event into a proof of scope
The first change was upstream: events now carry which fields or settings changed, including old and new values when possible. Engineers traced each field through DNS output generation to establish what it might alter. A planner then selects one of five scopes:
| Build scope | What it updates | Typical trigger |
|---|---|---|
| Resources | Specific DNS names | Ordinary A, AAAA, TXT, or CNAME edit |
| Resources and metadata | Names plus shared zone metadata | Nameserver or wildcard edit |
| Apex and metadata | Apex records plus metadata | A setting such as Universal SSL |
| Purge | Remove a zone’s published state | Zone no longer served on that network |
| Full zone | All relevant records and metadata | First publication or uncertain impact |
The correctness condition is stronger than “the answer looks right.” Every key owned by a targeted plan must match the bytes a full build would have written from the same state. Deletions matter too. For each affected name, the builder checks existing Quicksilver keys and removes keys absent from the new result. Otherwise a changed record could leave an old answer behind.
Unknown events, missing detail, and timestamp-only changes still require a full build. A timestamp can mean harmless bookkeeping or a change elsewhere; the event does not distinguish them. The planner treats uncertainty as a reason to expand work. A new zone also needs a full publication because no targeted patch can create all of its initial records.
Cloudflare makes an explicit exception for its largest zones: beyond a record-count threshold, it cannot successfully run a full build. It rebuilds the apex, metadata, and names identified by queued events instead. That trades complete coverage in rare uncertain cases for a pipeline that can keep processing changes; Cloudflare notes manual per-name rebuilding remains available for exceptional cases.
One domain name can mean several internal zones
The metadata key complicates otherwise local work. A domain can temporarily have multiple internal zone IDs during onboarding or lifecycle transitions. Record keys belong to individual zones, but one shared metadata key lists the selected zone IDs and details the DNS servers need to serve them.
An A record edit in one selected zone may need only that zone’s record keys. A nameserver or wildcard edit can change the shared directory, so it needs metadata too. The builder reads the candidate zone set again just before writing and reconstructs the metadata key from that fresh view. It writes the triggering zone’s records before metadata that points to them.
Events also describe a past moment. By execution time, the database may say the zone belongs on another network, should be purged, or needs a broader build. The planner starts with a conservative event-based work list, then checks a fresh publication snapshot before writing. It may upgrade to full work, turn the job into a purge, or skip a zone no longer selected. Database replicas can lag Kafka, so the aggregator delays events for normal lag and the final read check rejects a replica still behind.
Merge work without losing acknowledgments
Previously, record and full-zone builds had separate schedulers. Cloudflare brought them together so pending work for one zone can be combined. Suppose an A record changes at www.example.com, then a wildcard changes at *.example.com. One queued build can cover both names and metadata. Combining is monotonic: it only adds required work. A full build absorbs narrower rebuilds; a purge supersedes them because the desired output is absence.
The scheduler keeps the work plan separate from the Kafka messages that caused it. Even if a later full build makes individual names irrelevant, their messages remain attached until the combined result is safely written. A retry retains both. This distinction protects against an attractive but dangerous shortcut: collapsing jobs and accidentally acknowledging an event before its effect is durable.
Kafka offsets add another constraint. Each partition has a committed position representing a continuous prefix of completed messages. If message 2 is a slow build while messages 3 through 20 finish, the consumer cannot commit past 2. It may mark later jobs complete in memory, but a restart will replay them. That costs duplicate work while preserving changes. Making old builds shorter reduces how long one zone holds back unrelated zones sharing its partition.
The scheduler also enforces a per-zone rebuild interval so a busy zone does not monopolize workers. New changes arriving during that interval merge into pending work. When queued work gets old enough to threaten a Kafka partition, the scheduler can let it run sooner. Locks and retry delays still prevent a failing build from spinning.
Proving smaller builds are equivalent
Selective work is only useful if it produces the same published state. Cloudflare ran full and targeted builds from the same database state, fixed time-dependent inputs, and compared the affected Quicksilver keys byte for byte. Tests covered nameservers, wildcards, apex changes, merges, purges, and escalation. During rollout it also compared full builds alongside targeted builds.
The production-like check used a mirror builder consuming the production Kafka stream and database but writing to a separate Quicksilver environment that serves no customer traffic. A comparer sampled 20% of zones for missing, extra, or different keys. Since the two environments need not build at precisely the same instant, a transient difference alone was not evidence of a bug; persistent or novel differences were investigated. The rollout was divided by a hash of the zone name, keeping same-name zones on one scheduler so two schedulers would not race on the shared metadata key.
What improved, and what the numbers mean
Across a measured day, Cloudflare reports 86.81% resources-only builds, 2.54% resources-and-metadata, 2.30% apex-and-metadata, 1.30% purges, and 7.04% full builds. In all, 92.95% avoided rebuilding the complete zone. Full-build count fell by about 40%; the avoided work was especially valuable for large zones.
Aggregate builder working-set memory averaged 7 GiB instead of 49 GiB, an 86% drop. Peak memory fell from 79 GiB to 12 GiB, about 85%. In a side-by-side 24-hour comparison using the same events, the largest p99 build-completion spike fell from 14.6 minutes to 1.21 minutes. That metric ends when data is written to Quicksilver; it does not include distribution across Cloudflare’s network.
The memory headroom let Cloudflare shorten per-zone rebuild intervals. A separate database-lag safeguard also allowed the aggregator to hold events for less time. Together those changes reduced median build completion delay from about 7.7 to 3.3 seconds. The distinction matters: targeted builds created capacity, while the shorter waits delivered much of the typical latency reduction. Cloudflare’s newer histogram puts p90 near 5.5 seconds and p99 around 12 seconds; older coarse histogram buckets overstated those percentiles.
The broader engineering pattern is to make the invalidation boundary explicit, then verify it against the complete computation. Here, event detail defines the smallest candidate change; a fresh read protects against stale assumptions; byte-level comparisons guard output; and Kafka bookkeeping keeps merged jobs reliable. Full builds remain the correct tool when the impact is broad or unknown. The gain comes from no longer applying them to narrow, well-understood changes.