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

worker_threads.pdf

 worker_threads.pdf

Tamar Twena-Stern

May 07, 2020
Tweet

More Decks by Tamar Twena-Stern

Other Decks in Programming

Transcript

  1. Tamar Twena-Stern • Software Engineer - manager and architect •

    Backend Manager @XM Cyber • Was a CTO of my own startup • Passionate about Node.js ! • Twitter: @SternTwena
  2. Tamar Twena-Stern • On Maternity Leave • Have 3 kids

    • Loves to play my violin • Javascript Israel community leader
  3. Problems •The system allocates CPU and memory resources for every

    new thread •When the system is stressed – overhead of thread scheduling and context switching •The system waste resources for allocating threads instead of doing actual work
  4. • One Process • One Thread • One Event Loop

    • One JS Engine Instance • One Node.js Instance Node.js Process
  5. Blocking & Non Blocking Functions • Blocking Function - Main

    event loop must wait until it has finished to execute the next command. • Non-Blocking Function - • Main event loop will continue as soon as the function begins • Alerts the main loop once it has finished by calling a “callback”.
  6. Why CPU Intensive Operations Don’t Shine In Node • Non

    Blocking IO + Event Loop • Constant amount of threads • CPU Intensive operation will block the event loop • CPU intensive operation that will be off loaded to the worker thread pool will make one of the threads busy for long
  7. Why It Will Not Work ? • Theoretically simple -

    add Libraries to Node core to enable threads • Adding threads will change the nature of JavaScript • No synchronise mechanism • JavaScript basic types are not atomic, for example numerics
  8. Worker Threads - Definition • Applications use multiple isolated JavaScript

    workers • Main worker and child workers • Communication provided by Node.js • Separate instances of V8 and Event Loop • Shared memory
  9. Worker Threads - Components • One process • Multiple threads

    • One event loop per thread • One JS Engine Instance per thread • One Node.js Instance per thread
  10. How Is It Work ? • Worker executes a script

    provided by the parent worker • The worker runs in Isolation from other workers • Ability to pass messages between the worker and the parent • Parallelism - V8 Isolate
  11. Guidelines For Writing Production Worker Threads Applications : • For

    production - use worker threads pool • If you can - use asynchronous operations, it is much faster • Don’t use Worker Thread for IO operations - Node.js built in mechanisms are much faster