Skip to content

Instantly share code, notes, and snippets.

@strikeout
Created January 9, 2014 16:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save strikeout/8337568 to your computer and use it in GitHub Desktop.
Save strikeout/8337568 to your computer and use it in GitHub Desktop.
please help me with my flawed logic on how to observeChanges in a clustered multi-server environment (with oplog-scaling).
// 200k entries and more
HugeCollection = new Meteor.Collection("HugeCollection");
// convenience collection to hold the computed stats of HugeCollection
StatsForHugeCollection = new Meteor.Collection("StatsForHugeCollection");
/**
* Observer
*/
if (Meteor.isServer) {
var HugeCollectionObserverHandle = HugeCollection.find({}).observeChanges({
/*
this works perfectly on a single instance server but
when scaling out horizontally with eg. 4 cores, each stat inc.'ed 4 times
*/
added: function(_id, fields) {
// example stats, real scenario is much more complicated
StatsForHugeCollection.upsert('total_sum', {$inc: {total: fields.value}})
StatsForHugeCollection.upsert('total_count', {$inc: {count: 1}})
}
});
}
/**
* Pub / Sub
*/
if (Meteor.isServer) {
// the stats are really important for the clients,
// the actual HugeCollection data is not
Meteor.publish('publishStats', function() {
return StatsForHugeCollection.find()
});
}
if (Meteor.isClient) {
Deps.autorun(function(c) {
Meteor.subscribe('publishStats');
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment