Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
NodeJS Concurrency
1. Concurrency in NodeJS
PeterGriess, Yahoo! Inc.
pg@std.in | @pgriess| http://blog.std.in
2. NodeJS Concurrency Model
• Single process
• Single thread
• Non-preemptive
• Conncurrency provided by multiple processes
3. The Bad
• Can’t utilize multiple cores by default
– Modern server-class hardware is at least 4x
• Easy to stall request processing
– Invoke a method that blocks (there are some)
– Run some CPU-intensive code
4. The Good
• No race conditions (almost)
• No locking strategies to get wrong
• Comparatively easy to reason about
• Processes are a powerful sandbox
• QoS: nice(2), ionice(1), etc.
• Permissions: chroot(2), setuid(2), etc.
• Fault isolation
5. child_process
• Node has an API for creating child
processes, child_process.spawn()
• It has problems
– Very UNIX; not friendly to JavaScript developers
– IPC is DIY (transport? data format?)
6. Web Workers
• A rich but (mostly) easy-to-use JavaScript API
– Spawn new workers
– Send and receive messages to/from workers
– Worker lifecycle management (creation,
termination, etc)
• A Worker executes a JavaScript file/URL
• Separate process and execution context
• A W3C standard, part of the “HTML5” bucket;
designed for browsers
7. Shameless plug: node-webworker
• A Web Workers implementation for NodeJS
• npm install webworker
• Interested about the design?
– http://blog.std.in/2010/07/08/nodejs-webworker-
design/
11. Ping/pong example: hilights
• Easily spawn a new process!
• Pass complex messages!
• Exceptions bubble up to the parent!
• Graceful shutdown!
• This code will work in a browser with (almost)
no changes. Hooray standard APIs!
12. node-webworkers: Extensions
• Gracefully shut down workers with onclose()
• Watch for worker termination with onexit()
• Send file descriptors with postMessage()