On Wed, Jan 23, 2013 at 4:41 PM, Gabriel Sobrinho
<
gabriel....@gmail.com> wrote:
>> Since I have to keep a cache column of the paid value for the debt, I have 25 workers (sidekiq) that can call `increment!(:paid_value, >> paid_value)` and it should keep the total paid value.
>
> [SNIP]
>
> Yes, I'm thinking about that and seems there is no way to handle this in a
> smart way.
>
> Developers can think "from what reason I've incremented by 100 and it
> incremented by 300" until he figure out that was incremented by other
> threads too.
>
Gabriel: I may be confused about your use case, but it sounds like
your concern is around ensuring that the database client that makes an
update can rely on the fact that his update was the only one made. Or,
at least, your application has an "audit" concern, where, you need to
be able to prove how values were mutated. There are a couple of ways
to handle this, optimistic locking is one, row-level locks are
another, or designing a double-entry transaction model is another (so
you don't rely on locks at all).
If this is indeed your use case, what I'm advocating here is much
simpler than that. I just want to be able to increment numeric columns
in active record without declaring them as counter cache columns.
> Maybe some documentation on increment(!)/decrement(!) methods explaining
> that is not safe to use it concurrently will avoid to think they are.
That'd certainly be helpful, but the same logic applies to any method
you use. Any sort of CRUD application is subject to concurrent access
and update problems. Are you simply suggesting documenting
#increment!/#decrement! because they *don't* behave in the way that
I'm advocating for in this thread?