Reputation: 4353
On the Google App Engine with Python, I am looking for solutions to the race condition problem, i.e., multiple users are trying to increment a certain counter at the same time. I found two of them: the increment_counter() described in transactions and the bump_counter() in compare-and-set. My questions: 1) Do both of them completely solve the race condition problem? 2) If so, which one is better?
In addition, could some body elaborate more about each of them, because I can't see how the codes solve the problem. For examples, 1) during the increment_counter() transaction, if another user updates the counter, the transaction would fail? 2) similarly, during the bump_counter() in compare-and-set, if another user updates the counter, the client.cas() would fail?
Upvotes: 2
Views: 452
Reputation: 80330
Yes they both can eliminate race conditions.
The first is using datastore, the second memcache. So they can not be compared. Memcache is volatile and can be purged at any time - you should not use it for storing permanent data. So in this regard datastore transactions are better. Also, transactions can assure atomicity on a set of entities, while compare_and_set assures atomicity only on one memcache value.
Transactions do not do blocking. If they detect collision they fail and you need to roll it back and repeat again yourself.
Ditto for memcache: you need to repeat the procedure yourself.
Upvotes: 3