In this episode, we're tackling the challenge of syncing article view counts from Redis back to our database so that we can easily query and sort articles by popularity. Right now, our view counts are stored in Redis for speed, but that makes things tricky when we want to display the most popular content directly from the database.
Here's what we do step by step:
view_count
column to the articles
table with a migration, making sure it defaults to zero so new articles start fresh.sync:article-view-count
) that will be responsible for updating the database with the current view counts from Redis.After syncing, we can now reliably sort (and display!) our articles by the number of views, right from the database—perfect for leaderboards, trending sections, and more. Super handy!
Next time you check out your articles, you’ll see the view counts staying up-to-date without you lifting a finger.