Upgrade to Pro — share decks privately, control downloads, hide ads and more …

After Years of Node in Prod, We Still Block the...

After Years of Node in Prod, We Still Block the Event Loop

Avatar for Yamil Asusta

Yamil Asusta

October 27, 2017

More Decks by Yamil Asusta

Other Decks in Programming

Transcript

  1. /usr/bin/whoami • I do stuff for Auth0 • Been using

    Node.js since ~2011 • From Puerto Rico , fluent in Spanglish • @elbuo8
  2. What is the event loop again? • Pipeline of operations

    ◦ Timers: setTimeout/setInterval ◦ I/O: most of your callbacks ◦ Poll: looks for new events to schedule (ej. new HTTP connection) ◦ Check: setImmediate ◦ Close: server.on(‘close’, cb) • Runs our JS code and delegates operations to give us “async”
  3. Why should we event loop? “@coreinfra: customer reported had another

    spike of failed requests (502 and timeouts) at 13:00 UTC”
  4. How can we monitor it? • npm install blocked (new

    npm --save by default ) • Hard to find which part of the code actually caused the delay • Complements: graphs (cpu), heapdumps (memory) & custom metrics (apm)
  5. Bad Case: Lazy require • require is a blocking operation

    • Invoked mostly when starting the process • Avoid require outside the initialization logic
  6. Bad Case: JSON parse • Complexity (nested levels) are proportional

    to time • Used for serializing communication across services • Enforce limits that prevent unexpected payloads
  7. Bad Case: Garbage Collection • “max_old_space_size” helps memory leaks (

    yenkel) • “MarkSweepCompact” gets slower as heap increases • GC operations are less frequent and have higher latency
  8. Bad Case: Arbitrary Size Data Structures • Add “request.id” in

    RateLimitMap when new request is received • Listen for “onPreResponse” to be triggered • Remove “request.id” from RateLimitMap when request is processed
  9. Bad Case: npm install innocent-package • We install after we

    determine a module is “good” • Reviewers often trust a selected module ☑ • Once merged, the module is “trusted” by the whole organization