ruby
26 lines · 6 steps
Backfilling counter caches in a Rake task in Rails
A batched Rake task that recomputes stale Rails counter caches without hammering the database.
Explained by
highlit
1namespace :counter_cache do
2 desc "Recalculate comments_count for posts after a backfill"
3 task warm_post_comments: :environment do
4 scope = Post.where(comments_count: nil).or(Post.where("comments_count < 0"))
5 total = Post.count
6 warmed = 0
7
8 Post.in_batches(of: 500).each_with_index do |batch, index|
9 batch.pluck(:id).each do |post_id|
10 Post.reset_counters(post_id, :comments, :likes)
11 warmed += 1
12 end
13
14 progress = (warmed.to_f / total * 100).round(1)
15 Rails.logger.info("[counter_cache] batch #{index + 1}: warmed #{warmed}/#{total} (#{progress}%)")
16 sleep(0.1)
17 end
18
19 stale = scope.count
20 if stale.positive?
21 warn "WARNING: #{stale} posts still have suspicious counts after warming"
22 else
23 puts "Counter caches warmed for #{warmed} posts"
24 end
25 end
26end
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1reset_counters recomputes a counter cache from the real associated rows, fixing drift.
- 2Processing records in batches with a short sleep keeps a heavy backfill from overwhelming the database.
- 3Verifying with a suspicious-count query after the run turns a script into a self-checking migration.
Related explainers
ruby
require "shellwords" require "open3" module Backup
Building safe shell commands in Ruby
shell-out
subprocess
command-injection
Intermediate
7 steps
ruby
class UserAgentParser BROWSERS = [ [/Edg\/([\d.]+)/, "Edge"], [/OPR\/([\d.]+)/, "Opera"],
Parsing user-agent strings in Ruby
regex
pattern-matching
lookup-tables
Intermediate
8 steps
ruby
class LogAggregator BUCKET_FORMAT = "%Y-%m-%dT%H:%M" def initialize(entries)
Bucketing log entries by the minute in Ruby
aggregation
hashing
enumerable
Intermediate
5 steps
ruby
class WeeklySignupsReport DEFAULT_WEEKS = 12 def initialize(weeks: DEFAULT_WEEKS, source: User.all)
Building a weekly signups report in Rails
service object
aggregation
group by
Intermediate
7 steps
ruby
class ApplicationController < ActionController::Base EXPERIMENTS = { checkout_button_color: %w[control blue green], onboarding_flow: %w[control streamlined]
How A/B test cohorts are assigned in Rails
a-b-testing
cookies
hashing
Intermediate
8 steps
ruby
class SnippetHighlighter CONTEXT_RADIUS = 60 MAX_TERMS = 8
Building search-result snippets in Ruby
regular-expressions
text-processing
search
Intermediate
9 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/backfilling-counter-caches-in-a-rake-task-in-rails-explained-ruby-eac5/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.