# counter implementation

**URL:** <https://discuss.rubyonrails.org/t/counter-implementation/34190>\
**Category:** rubyonrails-talk\
**Created:** [September 10, 2008, 3:59pm UTC](https://discuss.rubyonrails.org/t/counter-implementation/34190 "2008-09-10T15:59:40Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [September 10, 2008, 3:59pm UTC](https://discuss.rubyonrails.org/t/counter-implementation/34190/1 "2008-09-10T15:59:40Z")

</div>

Greetings all,

&nbsp;&nbsp;I have a model which holds a counter field, the field have to be incremented frequently to record the access frequency of the specific object:

@obj = Temp.find(id) @obj.counter += 1 @obj.save

&nbsp;&nbsp;The implementation seems quite simple, but I am worrying about the race condition (if there are any, I do not know whether rails is thread-safe) since these statements may be interleaved. Could anyone help about this? Thanks very much!

Difei

---

<div class="post-metadata">

**Author:** ![Jason\_Roelofs](https://avatars.discourse-cdn.com/v4/letter/j/e274bd/32.png) [@Jason\_Roelofs](https://discuss.rubyonrails.org/u/Jason_Roelofs)\
**Post date:** [September 10, 2008, 4:04pm UTC](https://discuss.rubyonrails.org/t/counter-implementation/34190/2 "2008-09-10T16:04:33Z")

</div>

You're correct to worry about a race condition, but for the wrong reason. Rails deployments up to now and for a while yet into the future are clusters of mongrels / thins / etc, and as such nothing is threaded. The race condition in who (as in, which Mongrel) gets to save to the database first. As for protecting against this, you'll need to make sure to lock the row you working with, update the counter, save, then unlock. You'll want to do this as quick as possible of course, but it still could cause some slowdown if a lot of processes are trying to do the same thing.

If there are better ways around this, I'd definitely be interested in hearing them.

Jason

---

<div class="post-metadata">

**Author:** ![Mauricio\_Linhares1](https://avatars.discourse-cdn.com/v4/letter/m/eada6e/32.png) [@Mauricio\_Linhares1](https://discuss.rubyonrails.org/u/Mauricio_Linhares1)\
**Post date:** [September 10, 2008, 4:04pm UTC](https://discuss.rubyonrails.org/t/counter-implementation/34190/3 "2008-09-10T16:04:36Z")

</div>

You should be doing it like this:

Temp.increment\_counter( :counter, id )

This will increment the property ":counter" for the model Temp with "id", this column must have a default value of 0 (zero) or else this call won't work.

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [September 10, 2008, 4:40pm UTC](https://discuss.rubyonrails.org/t/counter-implementation/34190/4 "2008-09-10T16:40:56Z")

</div>

Maurício Linhares wrote:

> You should be doing it like this:
> 
> Temp.increment\_counter( :counter, id )
> 
> This will increment the property ":counter" for the model Temp with "id", this column must have a default value of 0 (zero) or else this call won't work.

Thanks a lot! This look like what I am looking for.

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [September 10, 2008, 4:42pm UTC](https://discuss.rubyonrails.org/t/counter-implementation/34190/5 "2008-09-10T16:42:02Z")

</div>

> You should be doing it like this:
> 
> Temp.increment\_counter( :counter, id )
> 
> This will increment the property ":counter" for the model Temp with "id", this column must have a default value of 0 (zero) or else this call won't work.

this works because it does

update foos set counter = counter + 1

and then it's the databases problem to worry about concurrent queries  
and so on. If what you showed was a simplification of what you're actually doing  
(and you can't reduce it to increment\_counter) then you'll need to  
look into locking (either optimistic or pessimistic)

Fred

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [September 10, 2008, 4:42pm UTC](https://discuss.rubyonrails.org/t/counter-implementation/34190/6 "2008-09-10T16:42:32Z")

</div>

Jason Roelofs wrote:

> > The implementation seems quite simple, but I am worrying about the race condition (if there are any, I do not know whether rails is thread-safe) since these statements may be interleaved. Could anyone help about this? Thanks very much!
> > 
> > Difei
> 
> You're correct to worry about a race condition, but for the wrong reason. Rails deployments up to now and for a while yet into the future are clusters of mongrels / thins / etc, and as such nothing is threaded. The race condition in who (as in, which Mongrel) gets to save to the database first. As for protecting against this, you'll need to make sure to lock the row you working with, update the counter, save, then unlock. You'll want to do this as quick as possible of course, but it still could cause some slowdown if a lot of processes are trying to do the same thing.
> 
> If there are better ways around this, I'd definitely be interested in hearing them.

Maurício proposed the increment\_counter method, may I assume that is atomic?

---

<div class="post-metadata">

**Author:** ![Mauricio\_Linhares1](https://avatars.discourse-cdn.com/v4/letter/m/eada6e/32.png) [@Mauricio\_Linhares1](https://discuss.rubyonrails.org/u/Mauricio_Linhares1)\
**Post date:** [September 10, 2008, 4:47pm UTC](https://discuss.rubyonrails.org/t/counter-implementation/34190/7 "2008-09-10T16:47:06Z")

</div>

This method does something like this:

"update model\_table\_name set counter\_column = counter\_column + 1 where id = 'your row id' "

So, it's up to your database (and the isolation level that it's using) to handle the atomicity of this call.

Put simply, it will usually work, unless your database has some serious concurrency issues.

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [September 10, 2008, 4:53pm UTC](https://discuss.rubyonrails.org/t/counter-implementation/34190/8 "2008-09-10T16:53:00Z")

</div>

Maurício Linhares wrote:

> > Maur�cio proposed the increment\_counter method, may I assume that is atomic?
> 
> This method does something like this:
> 
> "update model\_table\_name set counter\_column = counter\_column + 1 where id = 'your row id' "
> 
> So, it's up to your database (and the isolation level that it's using) to handle the atomicity of this call.
> 
> Put simply, it will usually work, unless your database has some serious concurrency issues.

yeah, as you and Fred said, I noticed the SQL UPDATE statement in the log.

---

<div class="post-metadata">

**Author:** ![Mauricio\_Linhares1](https://avatars.discourse-cdn.com/v4/letter/m/eada6e/32.png) [@Mauricio\_Linhares1](https://discuss.rubyonrails.org/u/Mauricio_Linhares1)\
**Post date:** [September 10, 2008, 4:55pm UTC](https://discuss.rubyonrails.org/t/counter-implementation/34190/9 "2008-09-10T16:55:46Z")

</div>

One thing to notice is that a loaded model WILL NOT be updated by this call, if you have a model loaded and call this method to perform the increment, the model that is already loaded will not be updated, you will have to call "reload" on it to get the latest counter value.

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [September 10, 2008, 5:07pm UTC](https://discuss.rubyonrails.org/t/counter-implementation/34190/10 "2008-09-10T17:07:13Z")

</div>

Maurício Linhares wrote:

> One thing to notice is that a loaded model WILL NOT be updated by this call, if you have a model loaded and call this method to perform the increment, the model that is already loaded will not be updated, you will have to call "reload" on it to get the latest counter value.

Thanks for the information.
