Data Consistency in Datastore Queries Stay organized with collections Save and categorize content based on your preferences.
Data consistency levels
Datastore queries can deliver their results at either of two consistencylevels:
- Strongly consistent queries guarantee the freshest results, but may take longer to complete.
- Eventually consistent queries generally run faster, but may occasionally return stale results.
In an eventually consistent query, the indexes used to gather the results are also accessed with eventual consistency. Consequently, such queries may sometimes return entities that no longer match the original query criteria, while strongly consistent queries are always transactionally consistent.
Datastore query data consistency
Queries return their results with different levels of consistency guarantee, depending on the nature of the query:
- Ancestor queries (those within anentity group)are strongly consistent by default, but can instead be made eventuallyconsistent by setting the Datastore read policy (see below).
- Non-ancestor queries are always eventually consistent.
Fetching an entity by key, which is also called "lookup by key", is stronglyconsistent.
Setting the Datastore read policy
To improve performance, you can set the Datastoreread policy so that all reads and queries are eventually consistent. (The API also allows you to explicitly set a strong consistency policy, but this setting will have no practical effect, since non-ancestor queries are always eventually consistent regardless of policy.)
You can also set the Datastorecall deadline, which is the maximum time, in seconds, that the application will wait for Datastore to return a result before aborting with an error. The default deadline is 60 seconds; it is not currently possible to set it higher, but you can adjust it downward to ensure that a particular operation fails quickly (for instance, to return a faster response to the user).To set the Datastore read policy and call deadline in Python, you pass themas arguments to therun(),get(),fetch(), andcount() methods of classQuery orGqlQuery. For example:forresultinEmployee.all().run(limit=5,read_policy=db.EVENTUAL_CONSISTENCY,deadline=5):# Body of iterative loopWhat's next?
- Learn how to specify what a query returns and further control query results.
- Learn thecommon restrictions for queries on Datastore.
- Learn aboutquery cursors, which allow an application to retrieve a query's results in convenient batches.
- Learn thebasic syntax and structure of queries for Datastore.
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-12-15 UTC.