Data consistency levels
Google Cloud Datastore queries can deliver their results at either of two consistency levels:
- 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. See the article Transaction Isolation in App Engine for more information on how entities and indexes are updated.
Cloud 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 an entity group) are strongly consistent by default, but can instead be made eventually consistent by setting the Cloud Datastore read policy (see below).
- Non-ancestor queries are always eventually consistent.
Setting the Cloud Datastore read policy
To improve performance, you can set the Cloud Datastore read 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 Cloud Datastore call deadline, which is the maximum time, in seconds, that the application will wait for Cloud 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 Cloud Datastore read policy in Java, you construct a Datastore service configuration (
DatastoreServiceConfig), using the nested helper class
DatastoreServiceConfig.Builder, and pass it an instance of class
ReadPolicy. The following example shows how to set the read policy, the call deadline, or both:
double deadline = 5.0; // Construct a read policy for eventual consistency ReadPolicy policy = new ReadPolicy(ReadPolicy.Consistency.EVENTUAL); // Set the read policy DatastoreServiceConfig eventuallyConsistentConfig = DatastoreServiceConfig.Builder.withReadPolicy(policy); // Set the call deadline DatastoreServiceConfig deadlineConfig = DatastoreServiceConfig.Builder.withDeadline(deadline); // Set both the read policy and the call deadline DatastoreServiceConfig datastoreConfig = DatastoreServiceConfig.Builder.withReadPolicy(policy).deadline(deadline); // Get Datastore service with the given configuration DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(datastoreConfig);
- Learn how to specify what a query returns and further control query results.
- Learn the common restrictions for queries on Cloud Datastore.
- Learn about query cursors, which allow an application to retrieve a query's results in convenient batches.
- Learn the basic syntax and structure of queries for Cloud Datastore.