In loop, i’m inserting documents in couchbase. I save document in couchbase with await UpsertAsync.
In next step i load this document with await QueryAsync. And sometimes this document doesn’t exists.
If i put small delay between Upsert and Query, then it’s ok.
I’m I doing something wrong? Does upsert has some delay, when saving the document ?
The issue you’re encountering is because the indices supporting queries are eventually consistent. There will always be some delay before mutations are represented on the portion of the query predicate being served by the index.
You can get a MutationToken from the upsert response and pass this to the query. This will ensure that particular mutation has been indexed before executing the query. This is known as “read your own write”. However, it comes at the cost of a slightly slower query. There is also an option to ensure that ALL mutations are indexed that are in progress at the time your run your query, which will be even slower.
As with most things in Couchbase, they are performance first and then opt-in to slower features when required. So I wouldn’t do this on everything, just where it’s necessary for your application design.