Created
January 9, 2014 16:50
-
-
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).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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