48
atx node.js August Meetup

Background processes and tasks in an async world

Embed Size (px)

Citation preview

Page 1: Background processes and tasks in an async world

atx node.js

August Meetup

Page 2: Background processes and tasks in an async world

background jobsin an async world

Cody Stoltman@particlebanana

Page 3: Background processes and tasks in an async world

sponsors

Page 4: Background processes and tasks in an async world

who are you?

Cody Stoltman@particlebanana

co-founder of Treeline

Sails.js core team

maintainer of Waterline ORM

Page 5: Background processes and tasks in an async world

what’s a background job?

Page 6: Background processes and tasks in an async world

browser /foo/bar

does some stuff

normal request/response lifecycle

Page 7: Background processes and tasks in an async world

browser /foo/bar

does some stuff

slow function

long running process lifecycle

Page 8: Background processes and tasks in an async world

browser /foo/bar

does some stuff

slow function

Page 9: Background processes and tasks in an async world

this isn’t rubynode is async

we have callbacks

Page 10: Background processes and tasks in an async world

what does blocking mean?

Page 11: Background processes and tasks in an async world

browser 1

traditionally

browser 2

server

do something

Page 12: Background processes and tasks in an async world

browser 1

with node.js

browser 2

server

do something

browser 3

Page 13: Background processes and tasks in an async world

browser /foo/bar

does some stuff

slow fuction

Page 14: Background processes and tasks in an async world

how can we handle that function?

• callbacks• just ignore the callback

• over engineer something related to process spawning

Page 15: Background processes and tasks in an async world
Page 16: Background processes and tasks in an async world

why this isn’t ideal

we aren’t blocking so the server can keep responding

but we have to wait until the process is done before responding to the client’s request

Page 17: Background processes and tasks in an async world

how can we handle that function?

• callbacks• just ignore the callback

• over engineer something related to process spawning

Page 18: Background processes and tasks in an async world

browser /foo/bar

does some stuff

slow function

Page 19: Background processes and tasks in an async world
Page 20: Background processes and tasks in an async world

why this isn’t ideal

the client get’s data back but we have no clue as to the success/failure of the task.

we could add logs but we have no way to give the client feedback or to re-try.

Page 21: Background processes and tasks in an async world

how can we handle that function?

• callbacks• just ignore the callback

• over engineer something related to process spawning

Page 22: Background processes and tasks in an async world

let’s learn from our synchronous, threaded brothers and sisters

whats the answer then?

Page 23: Background processes and tasks in an async world

how can we handle that function?

• remote queues

Page 24: Background processes and tasks in an async world

remote queue

queue worker

slow function

server

does some stuff

job

browser

job

Page 25: Background processes and tasks in an async world

why this is better

We can observe our queue

We can scale out horizontally and add capacity as needed

Our queue can be preserved so no jobs are abandoned

Page 26: Background processes and tasks in an async world

scalable remote queue

server server server server

worker worker worker worker

load balancer

queue

Page 27: Background processes and tasks in an async world

back to that event loop thing

Page 28: Background processes and tasks in an async world
Page 29: Background processes and tasks in an async world

call stack libuv api

callback queue

event loop

.on()

handler

handler ()

Page 30: Background processes and tasks in an async world

lets see what blocking actually means

Page 31: Background processes and tasks in an async world

call stack libuv api

callback queue

event loop

.on()

handler

handler ()

delayFn ()

handler handler handler

handler ()

Page 32: Background processes and tasks in an async world

so synchronous code blocks the event loop, don’t do that

what happens if we don’t block it?

Page 33: Background processes and tasks in an async world

call stack libuv api

callback queue

event loop

.on()

handler

handler()

sendEmail() queue

cbhandler handler

cb()handler()

sendEmail() queue

cb

Page 34: Background processes and tasks in an async world

remote queues are good because they let you do more, faster

so more work in the event loop means less concurrency

Page 35: Background processes and tasks in an async world

when should you move a function into a background job?

Page 36: Background processes and tasks in an async world

if for any reason it will block the event loop

Page 37: Background processes and tasks in an async world

it will prevent the response from returning in a timely manner

Page 38: Background processes and tasks in an async world

node modules

node-resquegithub.com/taskrabbit/node-resque

kuegithub.com/Automattic/kue

bullgithub.com/OptimalBits/bull

Page 39: Background processes and tasks in an async world

most queues are built on redis

Page 40: Background processes and tasks in an async world

Disque

github.com/antirez/disque

Page 41: Background processes and tasks in an async world

you could also build on a traditional message queue

Page 42: Background processes and tasks in an async world

summary

remote queues in node same as anywhere else

actually better because you can do more on a single cpu simultaneously(example: sending emails)

Page 43: Background processes and tasks in an async world

scheduled tasks

Page 44: Background processes and tasks in an async world

you need a database backed scheduler

Page 45: Background processes and tasks in an async world

someone should write one

agenda (mongo only)github.com/rschmukler/agenda

until then

Page 46: Background processes and tasks in an async world

demo

Page 47: Background processes and tasks in an async world

thanks!

Phillip Roberts (@latentflip)github.com/latentflip/loupe

Evan Tahler (@evantahler)blog.evantahler.com

Page 48: Background processes and tasks in an async world

atx node.js

August Meetup