Randy Tang
Randy Tang

Reputation: 4353

Google app engine: which solves the race condition? Transactions or compare-and-set?

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

Answers (1)

Peter Knego
Peter Knego

Reputation: 80330

  1. Yes they both can eliminate race conditions.

  2. 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.

  3. Transactions do not do blocking. If they detect collision they fail and you need to roll it back and repeat again yourself.

  4. Ditto for memcache: you need to repeat the procedure yourself.

Upvotes: 3

Related Questions