Is there a way to observe using Node SDK?


#1

Is there a way to have observability functionality similar to other SDK’s using the Node.js SDK. It doesn’t seem to be there. For example if I do an upsert or insert:

    myBucket.insert('document_name', {some:'value'}, function(err, res) {
      if (err) {
        console.log('operation failed', err);
        return;
      }

  console.log('success!', res);
});

is there a way to observe that document that was just inserted so I can catch when it’s written to disk… and/or replicated?

Additionally is there a way to observe more generic events, such as when any document is written to disk? or replicated?

Thanks,


#2

Hey @bojand,

Simply pass an options object specifying the durability requirements you want to meet for the operation to be considered a success, for instance:

myBucket.insert('document_name', {some:'value'}, {persist_to:2, replicate_to:1}, function(err, res) {
  if (err) {
    console.log('operation failed', err);
    return;
  }

  console.log('success!', res);
});

Cheers, Brett


#3

Thanks for the reply. Sorry, I should have stated I am aware of these options. It is not exactly what I am looking for. The issues with this approach are:

  1. There is significant performance hit for using any one of these options that make them basically completely impractical to use in any production environment where any sensible performance is needed. I admit it is very dependent on setup and hardware, but just adding persistTo: 1 locally adds anywhere from around 100 to 200 ms to a single insert call (and I am on an SSD). You can imagine how this slows down REST API’s significantly. Avoiding the flag, understandably, cuts operation time to basically nothing, ie. single digit ms usually.

  2. It is very useful to execute the database operation as normal and as fast as possible, but to know when a document is persisted or replicated. The calling client code doesn’t really care that the doc is persisted or replicated as part of the db op to return to user; but that it is done eventually and we may want to perform actions when that does happen.

  3. Depending on situation, context and settings, durability requirements failures are quite common, thus reducing the usefulness of these options at actual call time.

What I am looking for is more of an async solution. I would like to know when persisted and replicated events occur without adding that as a synchronous requirement to my db operation.

What I think would be more useful is an API that would allow async observability, perhaps using EventEmmiter interface. Something like:

var observer = myBucket.observe('document_name'); // returns an EventEmitter
observer.on('persisted', function(eventData) {
  console.log(eventData);
  // do whatever. force refresh view of a design document
});

observer.on('replicated', function(eventData) {
  console.log(eventData);
  // do whatever. 
});

myBucket.insert('document_name', { some: 'value' }, function(err, res) {
  if (err) {
    console.log('operation failed', err);
    return;
  }

  console.log('success!', res);
});

Or perhaps using an options param:

myBucket.insert('document_name', { some: 'value' }, {observe: true}, function(err, res) {
  if (err) {
    console.log('operation failed', err);
    return;
  }

  // res implements EventEmitter interface
  res.on('persisted', function(eventData) {
  console.log(eventData);
    // do whatever. force refresh view of a design document
  });

  res.on('replicated', function(eventData) {
    console.log(eventData);
    // do whatever. 
  });

  console.log('success!', res);
});

Another useful thing would be a global emitter perhaps:

myBucket.on('persisted', function(key, eventData), function({
  console.log('persisted document with key: %s', key);
}));