Views returning stale data


#1

Concept -

  1. Generate and store thousands of codes in a data bucket overnight (Ex: CodeBucket)
  2. Give out these codes when requested (one code per request).
  3. To ensure a code is given out only once, using a view as follows to retrieve a code

//View
function (doc, meta) {
if(doc.used == false) {
emit(meta.id, doc.code);
}
}

//Code
View view = client.getView(“CODE_VIEW”, “getNewCode”);
Query query = new Query();
query.setIncludeDocs(true);
query.setLimit(1);
query.setStale(Stale.FALSE);
ViewResponse response = client.query(view, query);

which is immediately followed by

a) updated used == true
b) call client.replace(key, 0, jsonString).get(); //tried with client.update as well.

Issue -
Under heavy loads, I see that view is returning same code. (Ex: I get “code1”, call update/replace, and when I request for new code, I again get “code1”).

What can I do differently so I can get a new code every time even under high load situations?

Best,
Srikrishna Kalavacharla


#2

using durability options resolved the issue. Provide appropriate PersistTo. and ReplicateTo. parameters to set/replace call. Also using query.setStale(Stale.FALSE) forces indexing before returning results.