Lessons Learned
from Node.js
Jason Yau
Director of Engineering
Samsung Accelerator
Tuesday, October 8, 13
1. Node.js
2. Methodology
3. Callbacks
4. Promises
Tuesday, October 8, 13
Node.js is javascript
on the server side.
It’s event-driven, non-
blocking I/O model
makes it FAST and
scalable.
Tuesday, ...
Tuesday, October 8, 13
In most languages,
the code we write is
synchronous.
aka Blocking
Tuesday, October 8, 13
Blocking
Tuesday, October 8, 13
Waiting
Tuesday, October 8, 13
In javascript, we do
things differently;
asynchronously.
aka Non-blocking
Tuesday, October 8, 13
Non-Blocking
Tuesday, October 8, 13
Parallel
Tuesday, October 8, 13
Fast
http://benchmarksgame.alioth.debian.org/u64/benchmark.php?test=all&lang=v8&lang2=yarv&data=u64
Tuesday, October 8, 13
Javascript
Tuesday, October 8, 13
Popular
Tuesday, October 8, 13
Methodology
Tuesday, October 8, 13
I/O Latency
• L1: 3 cycles
• L2: 14 cycles
• RAM: 250 cycles
• DISK: 41,000,000 cycles
• NETWORK: 240,000,000 cycles
Tuesd...
A theoretical request.
Request Process Results
Query DB or Web Service Write to log file
Format Response
= blocking / waiti...
Scale with Threads
Scale with Processes
Thread 1
Thread 2
Thread 3
Thread 4
Process 1
Process 2
Process 3
Process 4
Tuesda...
Scale with Event Loop
Handles many concurrent requests in a single
process / thread
Process 1
Thread Pool
Tuesday, October...
Analogy
Tuesday, October 8, 13
Event Queue Thread Pool
Event Loop
Filesystem
Network
Process
Other
Scale with Event Loop
Tuesday, October 8, 13
Challenges with
Callbacks
Tuesday, October 8, 13
Recurring Question
Tuesday, October 8, 13
Callbacks Example
Tuesday, October 8, 13
Pyramid of Doom
Tuesday, October 8, 13
Debugging
Tuesday, October 8, 13
Parallel Example
Tuesday, October 8, 13
Promises
Tuesday, October 8, 13
What are promises?
A promise is an object that represents a one-time
event, typically the outcome of an async task like an...
Instead of calling a
passed callback,
return a promise.
Tuesday, October 8, 13
Callback example
Tuesday, October 8, 13
Return a promise
Tuesday, October 8, 13
Passed callback
Tuesday, October 8, 13
Return a promise
Tuesday, October 8, 13
Parallel I/O
Tuesday, October 8, 13
Promises are cleaner
Tuesday, October 8, 13
Instead of calling a
passed callback,
return a promise.
Promise me you’ll
remember promises.
Tuesday, October 8, 13
Thank You
Jason Yau
Director of Engineering, Samsung
work: jason@samsungaccelerator.com
Tuesday, October 8, 13
Upcoming SlideShare
Loading in …5
×

Lessons learned from Node.js - Callbacks / Promises

887 views

Published on

Lessons learned from node.js. Node.js methodology, working with callbacks, using promises. If you ever decide to one day sit down and write in node.js just remember to use promises.

Instead of calling a passed callback, return a promise.

Published in: Technology, Business
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
887
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
12
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Lessons learned from Node.js - Callbacks / Promises

  1. 1. Lessons Learned from Node.js Jason Yau Director of Engineering Samsung Accelerator Tuesday, October 8, 13
  2. 2. 1. Node.js 2. Methodology 3. Callbacks 4. Promises Tuesday, October 8, 13
  3. 3. Node.js is javascript on the server side. It’s event-driven, non- blocking I/O model makes it FAST and scalable. Tuesday, October 8, 13
  4. 4. Tuesday, October 8, 13
  5. 5. In most languages, the code we write is synchronous. aka Blocking Tuesday, October 8, 13
  6. 6. Blocking Tuesday, October 8, 13
  7. 7. Waiting Tuesday, October 8, 13
  8. 8. In javascript, we do things differently; asynchronously. aka Non-blocking Tuesday, October 8, 13
  9. 9. Non-Blocking Tuesday, October 8, 13
  10. 10. Parallel Tuesday, October 8, 13
  11. 11. Fast http://benchmarksgame.alioth.debian.org/u64/benchmark.php?test=all&lang=v8&lang2=yarv&data=u64 Tuesday, October 8, 13
  12. 12. Javascript Tuesday, October 8, 13
  13. 13. Popular Tuesday, October 8, 13
  14. 14. Methodology Tuesday, October 8, 13
  15. 15. I/O Latency • L1: 3 cycles • L2: 14 cycles • RAM: 250 cycles • DISK: 41,000,000 cycles • NETWORK: 240,000,000 cycles Tuesday, October 8, 13
  16. 16. A theoretical request. Request Process Results Query DB or Web Service Write to log file Format Response = blocking / waiting Tuesday, October 8, 13
  17. 17. Scale with Threads Scale with Processes Thread 1 Thread 2 Thread 3 Thread 4 Process 1 Process 2 Process 3 Process 4 Tuesday, October 8, 13
  18. 18. Scale with Event Loop Handles many concurrent requests in a single process / thread Process 1 Thread Pool Tuesday, October 8, 13
  19. 19. Analogy Tuesday, October 8, 13
  20. 20. Event Queue Thread Pool Event Loop Filesystem Network Process Other Scale with Event Loop Tuesday, October 8, 13
  21. 21. Challenges with Callbacks Tuesday, October 8, 13
  22. 22. Recurring Question Tuesday, October 8, 13
  23. 23. Callbacks Example Tuesday, October 8, 13
  24. 24. Pyramid of Doom Tuesday, October 8, 13
  25. 25. Debugging Tuesday, October 8, 13
  26. 26. Parallel Example Tuesday, October 8, 13
  27. 27. Promises Tuesday, October 8, 13
  28. 28. What are promises? A promise is an object that represents a one-time event, typically the outcome of an async task like an AJAX call. When a promise is returned, at first it is in a pending state. Eventually it’s either: 1. resolved - The promise is fulfilled with a value 2. rejected - The promise is broken Tuesday, October 8, 13
  29. 29. Instead of calling a passed callback, return a promise. Tuesday, October 8, 13
  30. 30. Callback example Tuesday, October 8, 13
  31. 31. Return a promise Tuesday, October 8, 13
  32. 32. Passed callback Tuesday, October 8, 13
  33. 33. Return a promise Tuesday, October 8, 13
  34. 34. Parallel I/O Tuesday, October 8, 13
  35. 35. Promises are cleaner Tuesday, October 8, 13
  36. 36. Instead of calling a passed callback, return a promise. Promise me you’ll remember promises. Tuesday, October 8, 13
  37. 37. Thank You Jason Yau Director of Engineering, Samsung work: jason@samsungaccelerator.com Tuesday, October 8, 13

×