Query Consistency and Reading Your Writes

Query Consistency and Reading Your Writes

In order to make query execution as quick as possible, queries are executed against an index. When documents are created, removed, or otherwise modified, the index is updated asynchronously.

Because indexes are updated asynchronously, a query that is issued immediately after a document mutation may not reflect the recently-executed mutation.
cb.upsert('user:12345', {
    'names': ['Brass', 'Doorknob'],
    'emails': ['brass.doorknob@juno.com']
})

query = N1QLQuery(
    'SELECT names, emails FROM users WHERE $1 IN names', 'Brass')
for row in cb.n1ql_query(query):
    print row
In the above example, the user:12345 may not be reflected in the query results if the asynchronous index update took place after the query was executed.

Forcing query to wait until the index has been updated

It is possible to ensure that prior mutations are always considered and indexed when performing the query. This option works by instructing the query service to wait for all pending mutations to be indexed. The option is off by default to increase performance: in write heavy environments, many pending writes may be active and therefore waiting for them all to be indexed can slow down the execution time of the server. This option is called consistency
from couchbase.n1ql import CONSISTENCY_REQUEST
query = N1QLQuery(
    'SELECT names, emails FROM default WHERE $1 IN names', 'Brass')
query.consistency = CONSISTENCY_REQUEST
Examples for setting this option in the SDKs:

C | Python | Java | .NET | node.js