EventLoop in NodeJS
GOALS
Nodejs architecture
NodeJS architecture
NodeJS architecture
Eventloop
EventLoop structure
EventLoop structure and source code
Example
Initialization stage
Initialization stage
Initialization stage
Initialization stage
Initialization stage
Initialization stage
Initialization stage
Initialization stage
Initialization stage END
First circle START: nextTickQueue
First circle: Other microtasks queue
First circle
First circle: nextTickQueue
First circle: Phase timers
First circle: nextTickQueue
First circle: Other microtasks queue
First circle: Phase pending callbacks
First circle: nextTickQueue
First circle: Other microtasks queue
First circle: phase idle, prepare
First circle: nextTickQueue
First circle: Other microtasks queue
First circle: phase poll
First circle: nextTickQueue
First circle: Other microtasks queue
First circle: check
First circle: nextTickQueue
First circle: other microtasks queue
First circle: phase close callbacks
First circle END
Second circle START: phase timers
Second circle: phase pending callbacks
Second circle: idle, prepare
Second circle: phase poll
Second circle
Second circle: nextTickQueue
Second circle: other microtasks queue
Second circle: phase check
Second circle: phase close callbacks
Second circle END
Third circle START: phase timers
Third circle START: phase pending callbacks
Third circle START: phase idle, prepare
Third circle START: phase poll
Third circle START: phase check
Third circle START: phase close callbacks
Third circle END
Check loop alive condition
Check loop alive condition
Check loop alive condition: NO
EventLoop complete work
threadpool
EventLoop and ThreadPool
Example
Example
EventLoop and ThreadPool
ThreadPool
ThreadPool
ThreadPool
ThreadPool
ThreadPool and EventLoop
Example
Example
materials
Thank you for attention
9.48M
Category: englishenglish

EventLoop in NodeJS

1. EventLoop in NodeJS

Sergey Avanesyan

2.

S E N I O R S O F T WA R E E N G I N E E R
• More than 5 years production experience in web development.
• More than 1 year experience in EPAM Systems.
• Experience in pure JavaScript.
• Focus on writing SPA using React and Redux.
• Interest in Algorithms and Computer Science.
2

3. GOALS

UNDERSTAND HOW NODEJS WORKS UNDER A HOOD
ALLOWS BETTER UNDERSTAND CODE
EFFECTIVELY WRITE BACK-END JAVASCRIPT HIGH-LOAD APPLICATION
3

4. Nodejs architecture

NODEJS ARCHITECTURE

5. NodeJS architecture

6. NodeJS architecture

7. Eventloop

EVENTLOOP

8.

The event loop is what allows Node.js to perform
non-blocking I/O operations — despite the fact that
JavaScript is single-threaded — by offloading
operations to the system kernel whenever possible.
8

9. EventLoop structure

9

10. EventLoop structure and source code

10

11. Example

12. Initialization stage

>
12

13. Initialization stage

> START
13

14. Initialization stage

> START
14

15. Initialization stage

> START
15

16. Initialization stage

> START
16

17. Initialization stage

> START
17

18. Initialization stage

> START
18

19. Initialization stage

> START
19

20. Initialization stage END

> START
END
20

21. First circle START: nextTickQueue

> START
END
21

22. First circle: Other microtasks queue

> START
END
Next Tick
22

23. First circle

> START
END
Next Tick
Promise
23

24. First circle: nextTickQueue

> START
END
Next Tick
Promise
24

25. First circle: Phase timers

> START
END
Next Tick
Promise
Promise Next Tick
25

26. First circle: nextTickQueue

> START
END
Next Tick
Promise
Promise Next Tick
setTimeout 1
setTimeout 2
26

27. First circle: Other microtasks queue

> START
END
Next Tick
Promise
Promise Next Tick
setTimeout 1
setTimeout 2
27

28. First circle: Phase pending callbacks

> START
END
Next Tick
Promise
Promise Next Tick
setTimeout 1
setTimeout 2
28

29. First circle: nextTickQueue

> START
END
Next Tick
Promise
Promise Next Tick
setTimeout 1
setTimeout 2
29

30. First circle: Other microtasks queue

> START
END
Next Tick
Promise
Promise Next Tick
setTimeout 1
setTimeout 2
30

31. First circle: phase idle, prepare

> START
END
Next Tick
Promise
Promise Next Tick
setTimeout 1
setTimeout 2
31

32. First circle: nextTickQueue

32

33. First circle: Other microtasks queue

> START
END
Next Tick
Promise
Promise Next Tick
setTimeout 1
setTimeout 2
33

34. First circle: phase poll

> START
END
Next Tick
Promise
Promise Next Tick
setTimeout 1
setTimeout 2
34

35. First circle: nextTickQueue

> START
END
Next Tick
Promise
Promise Next Tick
setTimeout 1
setTimeout 2
35

36. First circle: Other microtasks queue

> START
END
Next Tick
Promise
Promise Next Tick
setTimeout 1
setTimeout 2
36

37. First circle: check

> START
END
Next Tick
Promise
Promise Next Tick
setTimeout 1
setTimeout 2
37

38. First circle: nextTickQueue

> START
END
Next Tick
Promise
Promise Next Tick
setTimeout 1
setTimeout 2
setImmediate
38

39. First circle: other microtasks queue

> START
END
Next Tick
Promise
Promise Next Tick
setTimeout 1
setTimeout 2
setImmediate
39

40. First circle: phase close callbacks

> START
END
Next Tick
Promise
Promise Next Tick
setTimeout 1
setTimeout 2
setImmediate
40

41. First circle END

> START
END
Next Tick
Promise
Promise Next Tick
setTimeout 1
setTimeout 2
setImmediate
41

42. Second circle START: phase timers

> START
END
Next Tick
Promise
Promise Next Tick
setTimeout 1
setTimeout 2
setImmediate
42

43. Second circle: phase pending callbacks

> START
END
Next Tick
Promise
Promise Next Tick
setTimeout 1
setTimeout 2
setImmediate
43

44. Second circle: idle, prepare

> START
END
Next Tick
Promise
Promise Next Tick
setTimeout 1
setTimeout 2
setImmediate
44

45. Second circle: phase poll

> START
END
Next Tick
Promise
Promise Next Tick
setTimeout 1
setTimeout 2
setImmediate
45

46. Second circle

> START
END
Next Tick
Promise
Promise Next Tick
setTimeout 1
setTimeout 2
setImmediate
46

47. Second circle: nextTickQueue

> START
END
Next Tick
Promise
Promise Next Tick
setTimeout 1
setTimeout 2
setImmediate
47

48. Second circle: other microtasks queue

> START
END
Next Tick
Promise
Promise Next Tick
setTimeout 1
setTimeout 2
setImmediate
readFile Next Tick
48

49. Second circle: phase check

> START
END
Next Tick
Promise
Promise Next Tick
setTimeout 1
setTimeout 2
setImmediate
readFile Next Tick
49

50. Second circle: phase close callbacks

> START
END
Next Tick
Promise
Promise Next Tick
setTimeout 1
setTimeout 2
setImmediate
readFile Next Tick
readFile setImmediate
50

51. Second circle END

> START
END
Next Tick
Promise
Promise Next Tick
setTimeout 1
setTimeout 2
setImmediate
readFile Next Tick
readFile setImmediate
51

52. Third circle START: phase timers

> START
END
Next Tick
Promise
Promise Next Tick
setTimeout 1
setTimeout 2
setImmediate
readFile Next Tick
readFile setImmediate
52

53. Third circle START: phase pending callbacks

> START
END
Next Tick
Promise
Promise Next Tick
setTimeout 1
setTimeout 2
setImmediate
readFile Next Tick
readFile setImmediate
readFile setTimeout
53

54. Third circle START: phase idle, prepare

> START
END
Next Tick
Promise
Promise Next Tick
setTimeout 1
setTimeout 2
setImmediate
readFile Next Tick
readFile setImmediate
readFile setTimeout
54

55. Third circle START: phase poll

> START
END
Next Tick
Promise
Promise Next Tick
setTimeout 1
setTimeout 2
setImmediate
readFile Next Tick
readFile setImmediate
readFile setTimeout
55

56. Third circle START: phase check

> START
END
Next Tick
Promise
Promise Next Tick
setTimeout 1
setTimeout 2
setImmediate
readFile Next Tick
readFile setImmediate
readFile setTimeout
56

57. Third circle START: phase close callbacks

> START
END
Next Tick
Promise
Promise Next Tick
setTimeout 1
setTimeout 2
setImmediate
readFile Next Tick
readFile setImmediate
readFile setTimeout
57

58. Third circle END

> START
END
Next Tick
Promise
Promise Next Tick
setTimeout 1
setTimeout 2
setImmediate
readFile Next Tick
readFile setImmediate
readFile setTimeout
58

59. Check loop alive condition

59

60. Check loop alive condition

60

61. Check loop alive condition: NO

61

62. EventLoop complete work

62

63. threadpool

THREADPOOL

64.

Threadpool can be used to run user code and get
notified in the loop thread.
Its default size is 4, but it can be changed at startup
time by setting the UV_THREADPOOL_SIZE
(process.env.UV_THREADPOOL_SIZE) environment
variable to any value (the absolute maximum is
128).
64

65. EventLoop and ThreadPool

65

66. Example

66

67. Example

67

68. EventLoop and ThreadPool

68

69. ThreadPool

69

70. ThreadPool

70

71. ThreadPool

71

72. ThreadPool

72

73. ThreadPool and EventLoop

73

74. Example

74

75. Example

75

76. materials

M AT E R I A L S
https://epa.ms/eventloop

77. Thank you for attention

T H A N K YO U F O R AT T E N T I O N
English     Русский Rules