Does active record have issues with stale data, or will it happily perform the update regardless?
Say I save my user record in memcached, and then a background job updates the same user record , so now memcached has an older version of the user record.
Now in my web application, I get the user record from memcached, and update a property and then save it, will it throw an exception because the user record isn’t the most up to date?
ActiveRecord’s internal tracking system should only update fields that have been modified since it was last saved, so if you update field A and the background job updates field B then there should be no conflicts.
You have issues when you’re using counters, though. If you have counter C that was cached as 10, but is actually 12 after the background job, and you want to add one more, you will save 11, not 13. This is why you should never assign to counters, but instead perform a differential adjustment on them instead.
This is the wrong way:
This is the right way:
You can do this using the increment method of ActiveRecord.
The only exception you’ll get is if you’re using the ActiveRecord versioning lock, but that seems more trouble than it’s worth.