google-app-enginegoogle-app-engine-pythonentity-groupsentity-group-transactions

Are Google App Engine entity groups locked when writes are not in a transaction


I have a question-answer-comment application(similar to stackoverflow). The questions and their related answers and comments logically form part of entity groups as defined in App Engine Docs .

I want to use entity groups/ancestor paths to group my entities together for 2 reasons:

  1. Improve query efficiency by storing Question and Answer entities together physically
  2. Allow me to perform ancestor queries thus eliminating the need for me store the Answer keys on the Question entity (relationships)

I do not want strong consistency as it will eventually cause contention.

Does App Engine always lock an entity group when updating or only when the update is being done in a transaction? In other words, do entity groups force updates to happen in transactions or simply provide the option to use transactions?


Solution

  • About your 1st reason for choosing an ancestry-based approach - I don't think I ever saw any kind of promise with respect to the physical location in the datastore - I imagine any such constraint would collide with its high scalability. I wouldn't worry about it, IMHO the gain of such efficiency optimisation, if any, would be negligible.

    You should be aware that contention isn't directly related to (strong) consistency (consistency really boils down to just the accuracy of query results).

    Contention is however directly related to accessing the same entity group simultaneously, even for read operations, not only for write - see Contention problems in Google App Engine. Using ancestry is only making it worse as all entities in an ancestry tree are in the same entity group.

    For your 2nd reason (if I understand your goal correctly) you don't need to store Answer keys into your Question entity or use ancestry. If you store the Question key (or key ID) into the Answer entity you can obtain the answers to a question by making regular (non-ancestor) queries for Answer entities with the matching question key/ID.

    The entity group "locking" is only visible in transactions (and no, transactions aren't enforced, but think twice before attempting to write outside transactions - unintended overwrites will occur). But note that such locking is only effective as protection against conflicting write ops, but not against contention.