---
title: "Rails 7.2 makes counter_cache integration safer and easier"
description:
  "Rails 7.2 adds ability to ignore counter_cache column whilst the data is
  getting backfilled"
canonical_url: "https://www.bigbinary.com/blog/rails-8-adds-ability-to-ignore-counter_cache-column-while-backfilling"
markdown_url: "https://www.bigbinary.com/blog/rails-8-adds-ability-to-ignore-counter_cache-column-while-backfilling.md"
---

# Rails 7.2 makes counter_cache integration safer and easier

Rails 7.2 adds ability to ignore counter_cache column whilst the data is getting
backfilled

- Author: Navaneeth D
- Published: July 16, 2024
- Categories: Rails, Rails 7.2

Counter caches are a cornerstone of performance optimization in Rails
applications. They efficiently keep track of the number of associated records
for a model, eliminating the need for frequent database queries. However, adding
counter caches to existing applications, especially those with large tables,
often can be challenging. Rails 7.2 brings an exciting update to address just
that!

## Counter cache integration challenges

When introducing counter caches to large datasets, developers often encounter
two primary challenges:

- Backfilling data efficiently: Adding a counter cache column to an existing
  table with a substantial amount of data can be problematic. Backfilling the
  counter cache values separately from the column addition is necessary to
  prevent prolonged table locks, which can significantly impact application
  performance. This process requires careful consideration to ensure data
  integrity while minimizing downtime and avoiding disruptions to user
  experience.
- Ensuring data consistency: Once the counter cache is in place, maintaining
  data consistency becomes paramount. Methods such as `size`, `any?`, and others
  that utilize counter caches internally must return accurate results. However,
  during the backfilling process, relying solely on the counter cache may
  produce incorrect counts until all records are appropriately updated.

## Safer counter cache implementation in Rails 7.2

The new update in Rails 7.2 introduces a feature that allows developers to
manage counter caches more effectively, especially in scenarios involving
existing large datasets. By introducing the `active` option in the counter cache
configuration, developers can control when the counter cache is actively
utilized. This enables them to backfill counter cache columns separately from
their addition, minimizing table locks and potential performance issues. Once
the backfilling process is complete, developers can activate the counter cache,
ensuring accurate association counts without compromising application
performance.

Let's illustrate the implementation of this update in Rails using a
straightforward example involving a blog application with articles and comments.
In our blog application, each article can have multiple comments. We want to add
a counter cache to track the number of comments associated with each article.
However, our database already contains a significant amount of data, making
traditional counter cache implementation challenging.

### Implementation Steps

1. Define the association: Initially, we define the association between the
   `Article` model and the `Comment` model, specifying the counter cache with
   the `active: false` option to keep it inactive during the initial setup.

```ruby
class Comment < ApplicationRecord
  belongs_to :article, counter_cache: { active: false }
end
```

2. Backfill the Counter Cache: With the association configured, we proceed to
   backfill the counter cache column in the `articles` table. During this phase,
   the counter cache remains inactive, and methods like `size`, `any?`, etc.,
   retrieve results **directly from the database**. This prevents incorrect
   values from getting displayed during backfilling.

3. Activate the Counter Cache: Once the backfilling process is complete, we
   activate the counter cache by removing the `active: false` option from the
   counter cache definition.

```ruby
class Comment < ApplicationRecord
  belongs_to :article, counter_cache: true
end
```

Upon activation, the counter cache integrates into the association, efficiently
tracking the number of comments associated with each article.

This [PR introduced](https://github.com/rails/rails/pull/51453) active option.
Checkout full feature discussion
[here](https://discuss.rubyonrails.org/t/new-feature-to-make-introducing-counter-caches-safer-and-easier/85456).

## Links

- [Human page](https://www.bigbinary.com/blog/rails-8-adds-ability-to-ignore-counter_cache-column-while-backfilling)
