Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting notifications on database changes: is it possible to watch entries in riak?

Tags:

node.js

riak

I'm looking for an efficient way to subscribe to events in riak from node. I would like to be able to be notified of changes on an entry from riak.

For example when one node.js server updates an entry, another server using and watching that entry receives the updated entry or a notification about its update automatically.

If this is impossible is there an efficient messaging system that can be efficiently used across node.js servers?

like image 829
Coyote Avatar asked Jan 02 '13 23:01

Coyote


1 Answers

Riak implements what are called Pre and Post commit hooks. Post-commits, which will be triggered when a write successfully occurs (and is presumably what you want) can only be written in Erlang code and Riak needs to be configured to trigger your custom Erlang function, as a property on the appropriate bucket.

Depending on your needs and the scale of your application, there can be several options for your Erlang setup to notify your Node.js server(s). It would be relatively easy to write an Erlang function that would send a HTTP request to your Node.js server, but that carries quite a lot of overhead, that may very well be inappropriate for your application. A lot Better, but slightly more complicated, would be to use a pub/sub system like those offered by Redis or ZeroMQ (just to name a couple), that are battle-tested and proven to perform very well under heavy load. If you want to go with ZeroMQ, see this guide on how to implement very reliable pub/sub.

Both of these messaging tools, as well as many others, can notify your Node.js instance of updates to watched entries from either Riak or the Node.js instance that's effectively modifying the data. The second option (Node.js to Node.js) might be simpler since it wouldn't require you to learn Erlang if you're not familiar with it. Both of these tools have node.js libraries that are very well-tested:

  • Zeromq.node
  • redis-node

And if you were to use them to send out notifications from within Riak as post-commit hooks, here are the corresponding erlang drivers:

  • Erlzmq2
  • Eredis
like image 114
matehat Avatar answered Sep 28 '22 11:09

matehat