<!-- llms-explorer concept facts · https://llms-explorer.com/tree/maintained-or-cached-count-counters/ · pack 2026-10-31 · ~1378 tokens -->

# Maintained or cached count counters

> Source-anchored research for Maintained or cached count counters. Run status: COMPLETED.

Parent: [Total count strategies for paginated UIs](https://llms-explorer.com/tree/total-count-strategies-for-paginated-uis/) · 5 facets · 14 facts · page: https://llms-explorer.com/tree/maintained-or-cached-count-counters/

## Definitions

- An atomic increment is not automatically safe to retry: AWS documents ambiguous service failures that can happen before or after a counter update, so retrying can overapply and declining a retry can underapply. Exact counter designs need an appropriate deduplication or transactional protocol. (low confidence) — [source](https://aws.amazon.com/blogs/database/implement-resource-counters-with-amazon-dynamodb/)

## How it works

- A maintained counter stores a precomputed total and updates it when contributing records change, replacing repeated count computation with a stored-value read. This shifts work from UI reads to counter maintenance and is especially useful when reads greatly outnumber writes. (high confidence) — [source](https://www.mongodb.com/docs/manual/data-modeling/design-patterns/computed-values/computed-schema-pattern.md)
- A historical counter_culture 2.1.4 / Rails 5.2.2 reproduction shows a conditional counter decrementing incorrectly when its predicate reads an associated object; the author recommends basing that predicate on the counted object’s own attributes. This is historical evidence, not verification of a current-version defect. (low confidence) — [source](https://qiita.com/kymmt90/items/f533729bd9e1c39a7aff)
- Duplicating a total in separately cached page responses can show inconsistent totals when navigating between pages. Codemzy demonstrates this and proposes separating count metadata in the client cache and invalidating other pages when the total changes; this is an implementation example, not a guarantee of database snapshot consistency. (low confidence) — [source](https://www.codemzy.com/blog/react-query-pagination-with-count)

## Problems, failure modes and limitations

- Inference for filtered pagination: use a counter whose membership predicate matches the displayed dataset. A simple association total cannot substitute for a conditional total; counter_culture explicitly supports conditional counters that include only qualifying records. (medium confidence) — [source](https://www.mongodb.com/docs/mongoid/current/data-modeling/associations/customize-association.md)
- The counter_culture project documents deferring counter updates until after commit as a way to address deadlocks, explicitly warning that this gives up transactional guarantees for those updates. (low confidence) (verified as of 2026-10-30) — [source](https://github.com/magnusvk/counter_culture)

## Comparisons and alternatives

- Mongoid documents counter_cache as a parent-model field storing the number of associated objects. This supplies an association-scoped count, rather than a general total for arbitrary UI filters. (low confidence) — [source](https://www.mongodb.com/docs/mongoid/current/data-modeling/associations/customize-association.md)

## Facts and statements

- A stored total can be refreshed on source-data writes or by a periodic job. Periodic refresh does not guarantee an exact current total; a UI using that strategy must tolerate stale counts. (high confidence) — [source](https://www.mongodb.com/docs/manual/data-modeling/design-patterns/computed-values/computed-schema-pattern.md)
- Counter caches need initialization from existing records and a way to repair drift. Rails documents stale counters and reset_counters; counter_culture documents backfilling from primary data and recommends periodic reconciliation to detect incorrect cached values. (high confidence) — [source](https://guides.rubyonrails.org/association_basics.html)
- A hot shared counter can become a write bottleneck: competing updates serialize or retry, and counter-cache transactions can deadlock. A fast count read therefore does not imply a cheap overall workload. (high confidence) — [source](https://planetscale.com/blog/the-slotted-counter-pattern)
- PlanetScale documents spreading increments across randomly selected counter slots and obtaining the total by summing those slots. This reduces contention on any one row but adds a rollup read or a separately refreshed aggregate. (medium confidence) — [source](https://planetscale.com/blog/the-slotted-counter-pattern)
- Atomic increment alone does not ensure correct counting across ambiguous failures. AWS describes DynamoDB failures occurring before or after an increment, making a blind retry capable of applying an update twice; its marker-item transaction pattern records a unique operation token to make retries idempotent. (low confidence) — [source](https://aws.amazon.com/blogs/database/implement-resource-counters-with-amazon-dynamodb/)
- Precomputed counts and external caches introduce maintenance work: application logic must propagate source-data changes, while cached query results require invalidation or replacement as records change. (medium confidence) — [source](https://www.usenix.org/system/files/osdi18-gjengset.pdf)
- Rails documents counter_cache: { active: false } for rollout: keep maintaining the counter during child creation/removal while reads continue using database results until backfill has completed. (low confidence) (verified as of 2026-10-30) — [source](https://guides.rubyonrails.org/association_basics.html)
