Have you ever created an application where you needed to save a document into Couchbase and then immediately query for that created document? It happens to me all the time when I’m developing. Take for example, adding new data into a list where each list item is a new document. In many scenarios you’ll want to update the UI that renders the list immediately after adding to it. The problem here is that the document you had just created may not show up in the query results. This is because the Couchbase indexers may not have processed the latest mutations against the Couchbase Bucket.

We’re going to see how to define our own scan consistency within a Node.js application which will yield variable results in our Couchbase N1QL queries.

Defining the scan consistency in an application will be similar regardless of the programming technology used. For this particular example we’ll be using Node.js with both N1QL and Ottoman.js.

Before going forward, the assumption is that you’ve got Couchbase installed and configured for N1QL. This means that you have at least one index ready to go, even if it is a primary index.

Defining the Scan Consistency with N1QL Queries

The first thing we’re going to worry about is N1QL related queries. Take a look at the following snippet of Node.js JavaScript code:

By default, the SELECT query is unbounded. This means that the query will return only data that is currently indexed, yielding the fastest possible response. Remember, if our new data hasn’t been indexed, it won’t be returned in the results.

If we wanted to wait until our data was indexed, we can define our scan consistency by doing the following:

Notice that we’ve changed the consistency in the above snippet to REQUEST_PLUS rather than leaving it unbounded. This means that the query won’t execute until all the mutations in the Bucket have been processed.

So what if we’re not using N1QL, but Ottoman.js instead?

Defining the Scan Consistency with Ottoman.js

Ottoman is a little different, but the rules still apply because when using the find operator, N1QL is being used under the covers.

Take a look at the following JavaScript snippet:

The goal here is to accomplish the same thing we had seen previously. In the above, we are doing an unbounded query and will only receive results that were processed by the index.

This can easily be changed by the following:

In the above code, LOCAL consistency is the same as saying REQUEST_PLUS.

Not so bad right?

Conclusion

You just saw how to define your own scan consistency in Couchbase using Node.js and either N1QL or Ottoman. By default queries are performance first, but should you need to make adjustments, at least it is available to you as an option.

If you’d like to learn more about scan consistency and the available options, check out the Couchbase documentation on the subject.

Author

Posted by Nic Raboy, Developer Advocate, Couchbase

Nic Raboy is an advocate of modern web and mobile development technologies. He has experience in Java, JavaScript, Golang and a variety of frameworks such as Angular, NativeScript, and Apache Cordova. Nic writes about his development experiences related to making web and mobile development easier to understand.

Leave a reply