Concept -
- Generate and store thousands of codes in a data bucket overnight (Ex: CodeBucket)
- Give out these codes when requested (one code per request).
- 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