How do I consistently increase a counter cache column?

元气小坏坏 提交于 2019-12-11 08:13:41

问题


Lets say I have a counter cache that needs to to be incremented on every page load. Say I have 10 web instances. How do I consistently increase a counter cache column?

Consistency is easy with one web instance. but with several instances running, A race conditions may occur.

Here is a quick explanation. Let's say my counter cache column is called foo_counts and its starting value is 0. If 2 web instance are loaded at the same time, both realize the count as 0. When it comes time to increase the count. They both increment the count from 0 to 1.

I looked at http://guides.rubyonrails.org/active_record_querying.html#locking-records-for-update

Any ideas would be greatly appreciated.


回答1:


You could use update_counter:

increment_counter(counter_name, id)

Increment a number field by one, usually representing a count.

This does a direct UPDATE in SQL so Model.increment_counter(:c, 11) sends this SQL to the database:

update models set c = coalesce(c, 0) + 1 where id = 11

so you don't have to worry about race conditions. Let the database do its job.




回答2:


Consider queueing the increments, and having workers do the actual incrementation in the background. You won't have completely up-to-the-millisecond data, but at least it will be accurate.



来源:https://stackoverflow.com/questions/11910974/how-do-i-consistently-increase-a-counter-cache-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!