I know that the way to handle DB transactionality on the app engine is to give different entities the same Parent
(Entity Group) and to use db.run_in_transaction
.
However, assume that I am not able to give two entities the same parent. How do I ensure that my DB updates occur in a transaction?
Is there a technical solution? If not, is there a pattern that I can apply?
Note: I am using Python.
App Engine is a fully managed, serverless platform for developing and hosting web applications at scale. You can choose from several popular languages, libraries, and frameworks to develop your apps, and then let App Engine take care of provisioning servers and scaling your app instances based on demand.
Datastore is a NoSQL document database built for automatic scaling, high performance, and ease of application development. Datastore features include: Atomic transactions. Datastore can execute a set of operations where either all succeed, or none occur. High availability of reads and writes.
The fees and free tier for Google Cloud Datastore are the same as the Datastore fees for App Engine. Small Datastore operations include calls to allocate Datastore IDs or keys-only queries. These operations are free.
As long as the entities belong to the same Group, this is not an issue. From the docs:
All datastore operations in a transaction must operate on entities in the same entity group. This includes querying for entities by ancestor, retrieving entities by key, updating entities, and deleting entities. Notice that each root entity belongs to a separate entity group, so a single transaction cannot create or operate on more than one root entity. For an explanation of entity groups, see Keys and Entity Groups.
There is also a nice article about Transaction Isolation in App Engine.
EDIT: If you need to update entities with different parents in the same transaction, you will need to implement a way to serialize the changes that were made by yourself and rollback manually if an exception is raised.
If you want cross-entity-group transactions, you'll have to implement them yourself, or wait for a library to do them. I wrote an article a while ago about how to implement cross-entity-group transactions in the 'bank transfer' case; it may apply to your use-case too.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With