The database's only job is to keep persistence between server restarts. On server start I fetch the a collection from the database and populate a variable with it, which I use for any direct access.
When a user updates something it will update the variable as well as updating the collection in the database.
Something like this:
var foo = readFromDB();
socket.on('connection', function() {
this.emit(foo);
});
socket.on('update', function(data) {
foo[data.id] = data.val;
updateDB(data.id, data.val);
});
(In my real application each collection has sub-arrays etc and not just key-value like in the example.)What should I use instead of MongoDB for this? Or is my approach fundamentally flawed?