Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6032955
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:24:45+00:00 2026-05-23T05:24:45+00:00

Each time a user’s profile gets displayed we update an integer counter in the

  • 0

Each time a user’s profile gets displayed we update an integer counter in the Users table.

I started thinking about high-concurrencey situations and wondered what happens if a bunch of people hit a user’s profile page at the exact same time: does rails/activerecord magically handle the record locking and semaphore stuff for me?

Or does my app need to explicitly call some sort of mechanism to avoid missing update events when concurrent calls are made to my update method?

def profile_was_hit
  self.update_attributes :hitcounter =>  self.hitcount + 1
end

And along those lines, when should I use something like Users.increment_counter(:hit counter, self.id) instead?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T05:24:46+00:00Added an answer on May 23, 2026 at 5:24 am

    In the default configuration, a single Rails instance is only handling a single request at a time, so you don’t have to worry about any concurrency trouble on the application layer.

    If you have multiple instances of your application running (which you probably do/will), they will all make requests to the database without talking to one another about it. This is why this is handled at the database layer. MySQL, PostgreSQL, etc. are all going to lock the row on write.

    The way you are handling this situation isn’t ideal for performance though because your application is reading the value, incrementing it, and then writing it. That lag between read and write does allow for you to miss values. You can avoid this by pushing the increment responsibility to your database (UPDATE hitcounter SET hitcount = hitcount + 1;). I believe ActiveRecord has support for this built in, I’ll/you’ll have to go dig around for it though. Update: Oh, duh, yes you want to use the increment_counter method to do this. Reference/Documentation.

    Once you update your process push incrementing responsibility to the database I wouldn’t worry about performance for a while. I once had a PHP app do this once per request and it scaled gloriously for me to 100+ updates/second (mysqld on the same machine, no persistent connections, and always < 10% cpu).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table where new data gets filled each time a user clicks
My question about My SQL is, I have a table where users enter time
Imagine a table view listing some recipes. Each time the user taps on a
I have a table that contains a column named views. Each time the user
Hi I am creating a listbox containing 2 text boxes each time a user
My Rails application makes use of Time.zone so that each user can set their
wat i am trying to is, each time user touches screen, i am adding
My goal: each time a user selects text, and clicks a button, that text
I have a custom shell script that runs each time a user logs in
Each user can vote once per item (only registered users can vote). So I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.