Slide 1

Slide 1 text

Worker Threads Tamar Twena-Stern

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

Tamar Twena-Stern • On Maternity Leave • Have 3 kids • Loves to play my violin • Javascript Israel community leader

Slide 4

Slide 4 text

Introduction

Slide 5

Slide 5 text

Traditional Approach - Blocking IO Client Server Thread Thread Thread Request Request Request

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

Node.js Architecture

Slide 8

Slide 8 text

Node.js Architecture - High Level

Slide 9

Slide 9 text

• One Process • One Thread • One Event Loop • One JS Engine Instance • One Node.js Instance Node.js Process

Slide 10

Slide 10 text

Demo - CPU Intensive Operation

Slide 11

Slide 11 text

Lets Deep Dive Into The CPU Intensive Problem

Slide 12

Slide 12 text

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”.

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

CPU Intensive Operations Synchrones Operations

Slide 15

Slide 15 text

Brilliant Idea :Let’s Add Threads To Node.js Core !

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

Worker Threads

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

Worker Threads - Components • One process • Multiple threads • One event loop per thread • One JS Engine Instance per thread • One Node.js Instance per thread

Slide 20

Slide 20 text

From One Process To Worker Threads

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

How Is It Work ?

Slide 23

Slide 23 text

Demo - CPU Intensive Operations With Worker Threads

Slide 24

Slide 24 text

Why The Improvement Is Not Enough ?

Slide 25

Slide 25 text

Our Mistake : Opening Worker On Each Request

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

Demo - Creating Worker Threads Pool

Slide 28

Slide 28 text

• Twitter: @SternTwena