Search code examples
google-app-engineconcurrencybigtable

In a BigTable datastore, with regards to concurrency, how do I "lock" an entity?


I am not sure how to handle this in a BigTable datastore.

Imagine the following example (just to explain the concept. The example does not match my actual data model):

  • I have a Counter entity that keeps track of the number of Transactions in my dataStore. Let's say the current 'count' is 100.
  • Now two web requests read this value at the same time.
  • Both web requests add a new Transaction
  • And finally both update the counter (to 101).

The counter value is now inaccurate. It should be 102.

Any suggestions on how to handle this situation? Can I 'lock' the counter to ensure that the second web request doesn't even read it until the first web request completes?


Solution

  • You have several options:

    • Depending on the scope of your counter and your entities, have the Transaction entities be child entities of the counter. Then, you can insert a transaction and update the counter transactionally. Bear in mind that this limits your update rate to about 1-5 QPS.
    • If your counts don't have to be 100% accurate, insert the entity and update the counter (using a single-entity transaction) separately. You can run a regular cronjob to re-count the number of entities and fix the counter if errors force it to be out of sync.
    • You could build your own limited distributed transaction support.