Lessons learned from Node.js - Callbacks / Promises

37
Lessons Learned from Node.js Jason Yau Director of Engineering Samsung Accelerator Tuesday, October 8, 13

description

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.

Transcript of Lessons learned from Node.js - Callbacks / Promises

Page 1: Lessons learned from Node.js - Callbacks / Promises

Lessons Learned from Node.js

Jason YauDirector of EngineeringSamsung Accelerator

Tuesday, October 8, 13

Page 2: Lessons learned from Node.js - Callbacks / Promises

1. Node.js

2. Methodology

3. Callbacks

4. Promises

Tuesday, October 8, 13

Page 3: Lessons learned from Node.js - Callbacks / Promises

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

Page 4: Lessons learned from Node.js - Callbacks / Promises

Tuesday, October 8, 13

Page 5: Lessons learned from Node.js - Callbacks / Promises

In most languages, the code we write is

synchronous.

aka Blocking

Tuesday, October 8, 13

Page 6: Lessons learned from Node.js - Callbacks / Promises

Blocking

Tuesday, October 8, 13

Page 7: Lessons learned from Node.js - Callbacks / Promises

Waiting

Tuesday, October 8, 13

Page 8: Lessons learned from Node.js - Callbacks / Promises

In javascript, we do things differently; asynchronously.

aka Non-blocking

Tuesday, October 8, 13

Page 9: Lessons learned from Node.js - Callbacks / Promises

Non-Blocking

Tuesday, October 8, 13

Page 10: Lessons learned from Node.js - Callbacks / Promises

Parallel

Tuesday, October 8, 13

Page 11: Lessons learned from Node.js - Callbacks / Promises

Fast

http://benchmarksgame.alioth.debian.org/u64/benchmark.php?test=all&lang=v8&lang2=yarv&data=u64

Tuesday, October 8, 13

Page 12: Lessons learned from Node.js - Callbacks / Promises

Javascript

Tuesday, October 8, 13

Page 13: Lessons learned from Node.js - Callbacks / Promises

Popular

Tuesday, October 8, 13

Page 14: Lessons learned from Node.js - Callbacks / Promises

Methodology

Tuesday, October 8, 13

Page 15: Lessons learned from Node.js - Callbacks / Promises

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

Page 16: Lessons learned from Node.js - Callbacks / Promises

A theoretical request.Request Process Results

Query DB or Web Service Write to log file

Format Response

= blocking / waitingTuesday, October 8, 13

Page 17: Lessons learned from Node.js - Callbacks / Promises

Scale with Threads

Scale with Processes

Thread 1Thread 2Thread 3Thread 4

Process 1Process 2Process 3Process 4

Tuesday, October 8, 13

Page 18: Lessons learned from Node.js - Callbacks / Promises

Scale with Event LoopHandles many concurrent requests in a single

process / thread

Process 1

Thread Pool

Tuesday, October 8, 13

Page 19: Lessons learned from Node.js - Callbacks / Promises

Analogy

Tuesday, October 8, 13

Page 20: Lessons learned from Node.js - Callbacks / Promises

Event Queue Thread Pool

Event Loop

FilesystemNetworkProcessOther

Scale with Event Loop

Tuesday, October 8, 13

Page 21: Lessons learned from Node.js - Callbacks / Promises

Challenges with Callbacks

Tuesday, October 8, 13

Page 22: Lessons learned from Node.js - Callbacks / Promises

Recurring Question

Tuesday, October 8, 13

Page 23: Lessons learned from Node.js - Callbacks / Promises

Callbacks Example

Tuesday, October 8, 13

Page 24: Lessons learned from Node.js - Callbacks / Promises

Pyramid of Doom

Tuesday, October 8, 13

Page 25: Lessons learned from Node.js - Callbacks / Promises

Debugging

Tuesday, October 8, 13

Page 26: Lessons learned from Node.js - Callbacks / Promises

Parallel Example

Tuesday, October 8, 13

Page 27: Lessons learned from Node.js - Callbacks / Promises

Promises

Tuesday, October 8, 13

Page 28: Lessons learned from Node.js - Callbacks / Promises

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 value2. rejected - The promise is broken

Tuesday, October 8, 13

Page 29: Lessons learned from Node.js - Callbacks / Promises

Instead of calling a passed callback, return a promise.

Tuesday, October 8, 13

Page 30: Lessons learned from Node.js - Callbacks / Promises

Callback example

Tuesday, October 8, 13

Page 31: Lessons learned from Node.js - Callbacks / Promises

Return a promise

Tuesday, October 8, 13

Page 32: Lessons learned from Node.js - Callbacks / Promises

Passed callback

Tuesday, October 8, 13

Page 33: Lessons learned from Node.js - Callbacks / Promises

Return a promise

Tuesday, October 8, 13

Page 34: Lessons learned from Node.js - Callbacks / Promises

Parallel I/O

Tuesday, October 8, 13

Page 35: Lessons learned from Node.js - Callbacks / Promises

Promises are cleaner

Tuesday, October 8, 13

Page 36: Lessons learned from Node.js - Callbacks / Promises

Instead of calling a passed callback, return a promise.

Promise me you’ll remember promises.

Tuesday, October 8, 13

Page 37: Lessons learned from Node.js - Callbacks / Promises

Thank YouJason Yau

Director of Engineering, Samsung work: [email protected]

Tuesday, October 8, 13