<!-- llms-explorer concept facts · https://llms-explorer.com/tree/approximation-pattern-for-write-heavy-computed-counters/ · pack 2026-10-31 · ~1844 tokens -->

# Approximation pattern for write-heavy computed counters

> Source-anchored research for Approximation pattern for write-heavy computed counters. Run status: COMPLETED.

Parent: [Read Offload via Caching and Precomputed Views ($merge/$out, Computed Pattern)](https://llms-explorer.com/tree/read-offload-via-caching-and-precomputed-views-merge-out-computed-pattern/) · 5 facets · 15 facts · page: https://llms-explorer.com/tree/approximation-pattern-for-write-heavy-computed-counters/

## How it works

- The approximation pattern moves counter maintenance into application logic and reduces write frequency by using coarser updates when exact real-time values are unnecessary; website visits are a representative use case. (high confidence) — [source](https://www.mongodb.com/docs/manual/data-modeling/design-patterns/computed-values/approximation-schema-pattern.md)
- Do not use intentional imprecision where business decisions require exact counts. MongoDB flags financial, inventory and audit requirements; AWS explains that undercounting a resource can cause overselling and discusses stronger counter designs when accuracy matters. (high confidence) — [source](https://github.com/mongodb/agent-skills/blob/main/skills/mongodb-schema-design/references/pattern-approximation.md)
- Threshold batching can keep per-key increments in application memory and flush them with a single $inc; a lastSyncedAt field records publication time. MongoDB's reference explicitly warns that unsynced increments disappear when the application restarts. (low confidence) — [source](https://github.com/mongodb/agent-skills/blob/main/skills/mongodb-schema-design/references/pattern-approximation.md)

## Measurements and reference values

- For a monotone stream of unit increments, flushing every 100 events needs approximately one counter write per 100 events. MongoDB illustrates this as 99% fewer counter writes; this is a workload calculation, not a measured hundredfold improvement in end-to-end throughput. (high confidence) — [source](https://www.mongodb.com/docs/manual/data-modeling/design-patterns/computed-values/approximation-schema-pattern.md)
- Threshold batching and probabilistic updates implement different approximations. MongoDB’s repository describes accumulating increments locally before a threshold flush; its design-pattern blog also describes randomly updating by 100 approximately 1% of the time. (high confidence) — [source](https://github.com/mongodb/agent-skills/blob/main/skills/mongodb-schema-design/references/pattern-approximation.md)

## Problems, failure modes and limitations

- Derived fixed-probability sampling model: let X count the independently sampled events among n unit events with n > 0 and sampling probability 0 < p <= 1. With one write per sampled event, expected writes are np. Then X is binomial and the stored estimator X/p has expected value n, variance n(1−p)/p, and relative standard deviation sqrt((1−p)/(np)). The binomial variance is np(1−p); scaling X by 1/p gives the stated variance. Sampling 1% of events therefore does not imply 1% relative error. (medium confidence) — [source](https://www.mongodb.com/company/blog/building-with-patterns-the-approximation-pattern)
- The repository example stores lastSyncedAt with the approximate counter and inspects elapsed time since synchronization. This exposes temporal staleness, but does not itself measure count error or recover increments lost during a crash. (low confidence) — [source](https://github.com/mongodb/agent-skills/blob/main/skills/mongodb-schema-design/references/pattern-approximation.md)
- Atomic updates alone do not establish exactly-once event accounting. AWS's DynamoDB counter analysis shows that an ambiguous failed response can leave a client unable to tell whether an increment applied, and a retry can overapply it. This is a failure-mode warning for counter design, not a claim that MongoDB has identical retry semantics. (low confidence) — [source](https://aws.amazon.com/blogs/database/implement-resource-counters-with-amazon-dynamodb/)
- Choose approximation only when the application can tolerate count error. MongoDB excludes counters whose exactness is mandated; AWS explains how counter errors can cause overselling and why resource thresholds require race-free enforcement. An approximate display count alone is unsuitable for enforcing an exact resource limit. (high confidence) — [source](https://github.com/mongodb/agent-skills/blob/main/skills/mongodb-schema-design/references/pattern-approximation.md)
- Do not transfer guarantees for monotone counters directly to net counters with increments and decrements. Microsoft Research shows that separately approximating two counts and subtracting them need not preserve relative error in their difference; under fully adversarial alternating updates, continuous relative-accuracy tracking can require communication on every event. (low confidence) — [source](https://www.microsoft.com/en-us/research/wp-content/uploads/2016/02/MSR-TR-2011-128.pdf)
- Keep intentional approximation distinct from accidental loss: threshold buffering loses unsynced increments on restart, while an ambiguous counter-update retry can overapply a change. The acceptable approximation budget must account for these failure modes separately from normal publication lag or sampling error. (medium confidence) — [source](https://github.com/mongodb/agent-skills/blob/main/skills/mongodb-schema-design/references/pattern-approximation.md)

## Comparisons and alternatives

- MongoDB's approximation pattern updates frequently changing stored values at coarser granularity when precise values are unnecessary. It reduces counter-maintenance writes and supports reading stored summaries rather than continuously recomputing exact values. (high confidence) — [source](https://www.mongodb.com/docs/manual/data-modeling/design-patterns/computed-values/approximation-schema-pattern.md)

## Facts and statements

- The MongoDB repository’s local-memory batching example loses unsynced increments on application restart. Its one-threshold lag statement describes the local batching example; inference: with K independently buffering writers, each holding fewer than B events, aggregate unflushed state can reach K(B-1), even before considering failed or in-flight flushes. (low confidence) — [source](https://github.com/mongodb/agent-skills/blob/main/skills/mongodb-schema-design/references/pattern-approximation.md)
- Probabilistic-counter research supports trading precision for reduced state and fewer actual updates, but its in-memory and parallel-computing results are not MongoDB database benchmarks. Sandia analyzes configurable counting accuracy; Oracle Labs develops addition algorithms that preserve expected estimated sums for supported counter representations. (high confidence) — [source](https://www.sandia.gov/files/samitch/files/pc.pdf)
- A one-threshold lag description applies to one local buffer. With W independent writers each retaining fewer than B unflushed unit increments, aggregate unpublished increments can reach W(B−1), before allowing for in-flight writes or failures. This is a derived bound under those assumptions, not a database-wide guarantee from MongoDB. (low confidence) — [source](https://github.com/mongodb/agent-skills/blob/main/skills/mongodb-schema-design/references/pattern-approximation.md)
