First, your TL;DR:
- Stress test your Meteor app.
- Oplog tailing may be less efficient for particular workloads.
My work involves using crowdsourcing to assess and improve technical skill. We’re focusing on improving basic technical skills of surgeons first because—no surprise here—it matters. A more skilled surgeon means patients with less complications. Being healthy, not dying. Good stuff.
One way we gather data is a survey app where crowdworkers watch a short video of real human surgery and answer simple questions about what they saw. For example:
- Do both hands work well together?
- Is the surgeon efficient?
- Are they rough or gentle?
Turns out the crowd nails this! Think of it this way: most anyone can recognize standout performers on the basketball court or a playing a piano, even if they’re not an expert at either. Minimal training and this “gut feel” are all we need to objectively measure basic technical skill.
And of course we used oplog tailing right from the start because much of what read about oplog tailing made it sound like it was the only way to go. Sure, you’ll want oplog tailing for realtime (<10sec delayed) data when you have multiple apps connecting to the same MongoDB database. But if you don’t need that, you may not need it at all, and you may not want it.
Our traffic is very bursty. We publish a HIT on Amazon Mechanical Turk. Within minutes, the crowd is upon our survey app. Our app generally does fine, but folks complained of very slow survey completion times when we started hitting somewhere around 80 DDP(?) sessions in Kadira. Each DDP session in our survey app should equate to one simultaneous active user (hereafter “user”).
Here’s what we want to know:
- Why does our app slow down when it does?
- Can it scale [linearly]?
- Are there any small code or configuration changes we could do to get a lot more performance out of the existing hardware?
- Meteor pegs the CPU when oplog tailing is enabled.
- Yes, if we disable oplog tailing.
- Yes, disabling oplog tailing and clustering our app.
We created a stress test to get a better feel for the performance characteristics of our app.
The test uses nightwatch to emulate a turker completing a survey. Load the survey app, click radio buttons, enter comments, and throw in a few random waits. Many threads of the nightwatch test are spawned and charge on in parallel. The machine running nightwatch needs to be pretty beefy. I preferred a browser-based stress test because I noticed client-server interactions amplified the amount and frequency of DDP traffic (hello Mr. Reactivity). It was also easier to write and run nightwatch then pick the exact DDP traffic to send.
Notes on our app:
- We use mup to deploy to Ubuntu EC2 servers on AWS.
- Tested configuration uses one mup-deployed Meteor app.
- The app connects to a local MongoDB server running a standalone one-member replica set (just to get the oplog).
- I also tested with Modulus, scaled to one 512mb servo in
us-east-1a. Non-enterprise Modulus runs with oplog tailing disabled, and the app connects to MongoDB on a host other than
- I also tested with Modulus, scaled to one 512mb servo in
- Our app uses iron:router.
- Our app doesn’t need to be reactive. Surveyees work in isolation. But this is how we wrote the app, so that’s what I tested.
I ran a series of stress tests. Ramp up traffic, capture metrics, change code and/or server configuration, repeat. Here are the results.
- Each row in the spreadsheet represents one test.
- Every test ran for 5 minutes.
- When one “user” completes a survey, another one begins (so the number of users is kept more or less constant during evey 5-minute test).
- There are lots of notes and Kadira screenshots in the results spreadsheet. For the Kadira screenshots, the relevant data is on the rightmost side of the graphs.
- I think Kadira session counts are high. Maybe it isn’t counting disconnects, maybe DDP timeouts take a while, or maybe the nightwatch test disconnects slowly.
- Row 3. At 40 users, the CPU is pegged. Add any more users and it takes too long for them to complete a survey.
- Row 5. Notice how doubling the cores does not double the number of test passes (less than linear scaling along this dimension).
- Row 6. Ouch, we’re really not scaling! Might need to investigate the efficiency of
- Row 7. Oplog tailing is disabled for this and all future tests. MongoDB CPU load is roughly doubled from the 40-user, 1-core, oplog-tailing-enabled test.
- Row 9. Too much for one core: 6.5% of the tests failed.
- Row 11. This is what we want to see! 2x cores, 2x users, 2x passes. In other words, with oplog tailing disabled and double the number of cores, we supported double the number of users and doubled test passes.
- I should have also tested 160 users, 4 cores, oplog disabled. I didn’t. Live with it.
- Disabling oplog tailing seemed to allow the processing load to shift more to MongoDB. MongoDB appeared to be able to handle same more… gracefully.
- I didn’t get very far with Modulus. I’m very interested in their offering, but I just couldn’t get users (test runs) through our app fast enough to make further testing worthwhile.
- A DNS issue prevented capturing Kadira status while running on Modulus.
- cluster lives up to its promise—adding cores and spreading load.
- I don’t think we’re moving much data, but any reactivity comes at a price at scale (even our so far little bitty scale).
- Our survey app could and should be modified to use much less reactivity since, as I mentioned earlier, it is unnecessary.
This is somewhat of an addendum, but I figured it might be useful.
Here’s what the Meteor Node.js process does when 10 users hitting our survey app running on one core.
Oplog tailing enabled:
Oplog tailing disabled:
- Note that these pie charts only show %CPU usage. CPU and network are the primary resources our app uses, so this is fine.
- The profile data for each slice (when you drill down) are very low-level. It’s hard to make any quick conclusions (or I just need more practice reading these).
- When oplog tailing is enabled, the
Cursor.fetchslice is about twice as big, and none of the methods causing that CPU load are ours. Perhaps this is the oplog “tailing” in action?
- When oplog taling is disabled, drilling into
Cursor.fetchshows us exactly what specific methods of ours are causing CPU load. Even if oplog tailing is more efficient, this level of introspection was priceless. We need this until we learn to better debug patterns in our code that lead to more CPU when oplog tailing is enabled.
- The giant ~30% slice of “Other” is a bit of a bummer. What’s going on in there? Low-level/native Node.js operations like the MongoDB driver doing its thing? Sending/receiving network traffic?
- Kadira monitoring isn’t free CPU-wise, but it is worth it.
- What should these pie charts look like in a well-optimized application under load? Perhaps the biggest slice should belong to “Other”?
- https://meteorhacks.com/mongodb-oplog-and-meteor – Huge props to Arunoda for mentioning potential drawbacks. “Even though oplog makes your Meteor app faster in general, it might make things worst[sic].”
- https://www.meteor.com/livequery – Very useful take on livequery (new nickname for “oplog tailing”) vs. “poll and diff”.
- https://forums.meteor.com/t/oplog-tailing-too-far-behind-not-helping/2235, especially this reply … we might be doing some “high velocity writes” (we write some log messages to a collection, for example) – we need to analyze writes per second per collection (and review publications of same). And we probably have low observer reuse (as I said earlier, surveyees work in isolation).
Feedback/questions/comments/corrections welcome! I’d espeically love to hear about your experiences scaling Meteor.