node.js, javascript and the future

103
javascript & the future

Upload: jeff-miccolis

Post on 30-Oct-2014

5.133 views

Category:

Technology


2 download

Tags:

DESCRIPTION

 

TRANSCRIPT

Page 1: node.js, javascript and the future

javascript & the future

Page 2: node.js, javascript and the future

Jeff MiccolisDevelopment Seed

Page 3: node.js, javascript and the future

Open AtriumFeaturesContextStrongarm

Page 4: node.js, javascript and the future

Jeff MiccolisMapBox

Page 5: node.js, javascript and the future
Page 6: node.js, javascript and the future

First, a brief apology.

Page 7: node.js, javascript and the future

Three “war” stories about things that are hard with php & drupal

Page 8: node.js, javascript and the future

...and easier in node.js

Page 9: node.js, javascript and the future

Two lessons I learned

Page 10: node.js, javascript and the future

...from Drupal and node.js work

Page 11: node.js, javascript and the future

SERVER SIDE JAVASCRIPT

Page 12: node.js, javascript and the future

V8

Page 13: node.js, javascript and the future

CHROME ‘S JAVASCRIPT ENGINE

Page 14: node.js, javascript and the future

ON YOUR SERVER

Page 15: node.js, javascript and the future

1. Sending lots of email

Page 16: node.js, javascript and the future

OpenAtrium

Page 17: node.js, javascript and the future
Page 18: node.js, javascript and the future

Single emailsDigests email

Page 19: node.js, javascript and the future

SMSXMPP (jabber)Twitteretc, etc, etc...

Page 20: node.js, javascript and the future

d.o/project/Messaging

channel independent messages

don’t send e-mails to users, send them 'messages'

delivered by mail, IM, SMS, etc...

Page 21: node.js, javascript and the future

Problems

Page 22: node.js, javascript and the future

Problems

Send an email to one user - 50 ms

28 users - 1.4 seconds

600 users - 30 seconds

Page 23: node.js, javascript and the future

Problems

Run cron every 5 minutes

and you can send a lot more emails

but if one run doesn’t complete...

Page 24: node.js, javascript and the future

<?php

$addresses = array( '[email protected]', '[email protected]', '[email protected]');

$message = 'I am writing to inform you of a...';$count = 0;

foreach($addresses as $sucker) { if (mail($sucker, 'New opportunity', $message)) { $count++; };}

echo "$count messages sent";

Page 25: node.js, javascript and the future

var mail = require('mail');

var addresses = [ '[email protected]', '[email protected]', '[email protected]'];

var message = 'I am writing to inform you of a...';var count = 0;

addresses.forEach(function(sucker, i) { mail(sucker, 'New opportunity', message, function(err) { if (!err) { count++; }

if (i == addresses.length - 1) { console.log(count +" messages sent"); } });});

Page 26: node.js, javascript and the future

// Attempt to send email.mail(sucker, 'New opportunity', message, function(err) { if (!err) { count++; }});

// Don't wait, get the next address NOW.console.log('next');

// Attempt to send email.if (mail($sucker, 'New opportunity', $message)) { $count++};

// Once email is sent, get the next address.echo 'next';

Page 27: node.js, javascript and the future

Send an email ~ 5 ms processing, 45 ms waiting

28 emails ~ 140 ms to process, done 45 ms later.

600 users ~ 3 seconds to process, done 45 ms later.

Stop waiting around

Page 28: node.js, javascript and the future

Stop waiting around

600 emails w/Php: ~30 seconds

600 emails w/node.js: ~3 seconds

Page 29: node.js, javascript and the future

Stop waiting around

DEMO

Page 30: node.js, javascript and the future

2. Handling feeds

Page 31: node.js, javascript and the future
Page 32: node.js, javascript and the future

The task

Fetch RSS feedsFetch original article

Tag items (calais)Tag items (watchlist)Tag items (wikipedia)

Geocode items

Page 33: node.js, javascript and the future

The scale

Thousands of feeds.

Millions of items.

Gigs and Gigs of data.

Page 34: node.js, javascript and the future

Problems

cron.php

Page 35: node.js, javascript and the future

maggie

Page 36: node.js, javascript and the future

maggied

multi-threaded python daemon

Page 37: node.js, javascript and the future

maggied

4 “retriever” workers get batches of 50 items.

they fetch/tag/geocode each item.

Page 38: node.js, javascript and the future

maggied

retrieve original story: 300ms

tag: 100ms

geocode: 150ms

TOTAL: 550ms

Page 39: node.js, javascript and the future

Nearly all of that 550ms is spent idle.

Stop waiting around

Page 40: node.js, javascript and the future

...but we could run “packs” of retrievers!

Page 41: node.js, javascript and the future

Is that really the best idea?

Page 42: node.js, javascript and the future

Replace the retrievers with a single HYPERACTIVE SQUID!

Page 43: node.js, javascript and the future
Page 44: node.js, javascript and the future

The squid runs up and down as fast as it can dealing with each item in turn.

It fires off any long running I/O operations and then moves on to the next item.

When the I/O operation reports progress, it does a little more work on behalf of the

corresponding item.

Page 45: node.js, javascript and the future

100% async

Event loop

Page 46: node.js, javascript and the future

100% async

Anything that leaves v8 takes a callback.

Page 47: node.js, javascript and the future

100% async

filesystem, network, stdio, timers, child processes

Page 48: node.js, javascript and the future

var request = require('request');

request('http://example.com', function (err, resp, body) { if (!err && resp.statusCode == 200) { console.log(body); }});

var fs = require('fs');

fs.readFile('/etc/passwd', function (err, data) { if (err) throw err; console.log(data);});

Page 49: node.js, javascript and the future

100% async

Limiting factors change:

how many open sockets are you allowed?how much bandwidth can you grab?

how fast can you issue requests?

Page 50: node.js, javascript and the future

3. Big files, long sessions.

Page 51: node.js, javascript and the future

What’s big?

gigabyte

Page 52: node.js, javascript and the future

What’s long?

hours

Page 53: node.js, javascript and the future

What’s a session?

HTTP

Page 54: node.js, javascript and the future

A category of stories...

Page 55: node.js, javascript and the future

Big uploads in php

upload_max_filesize

post_max_size

max_input_time

max_execution_time

Page 56: node.js, javascript and the future

Big uploads in php

this approach caps out ~500mb

Page 57: node.js, javascript and the future

Problems

Opens the door to DOS.

Tolerates application bloat.

Problems in production can get really bad.

Never gonna get gigabyte uploads.

Page 58: node.js, javascript and the future

Problems

Php makes you look elsewhere.

Page 59: node.js, javascript and the future

If only we could stream...

Page 60: node.js, javascript and the future

If only we could stream...

Page 61: node.js, javascript and the future

Streaming

Deal with things bucket by bucket

and you don’t need all that memory.

Page 62: node.js, javascript and the future

Streaming

write that files to disk as it comes in

...or stream it off to s3!

Page 63: node.js, javascript and the future

var formidable = require('formidable');var http = require('http');

http.createServer(function(req, res) { if (req.url == '/upload' && req.method == 'POST') {

var form = new formidable.IncomingForm(); form.parse(req); form.onPart = function(part) { part.addListener('data', function(chunk) { // Do cool stuff, like streaming! console.log(chunk.toString('utf8')); }); }; }}).listen(80);

Page 64: node.js, javascript and the future

_changes

Page 65: node.js, javascript and the future

_changes

A persistent connection to your database, which feeds you new data when it has some.

Page 66: node.js, javascript and the future

_changes

it’s amazing

Page 67: node.js, javascript and the future
Page 68: node.js, javascript and the future

MapBox uploads

1. Map uploads to s3

2. Save a record to CouchDB

3. “Downloader” listens for new maps

Page 69: node.js, javascript and the future

MapBox uploads

node.js process

very long-lived http connection

to CouchDB

Page 70: node.js, javascript and the future

Everything is different

non-blocking I/O & single event loop

Page 71: node.js, javascript and the future

4. Package Management

Page 72: node.js, javascript and the future

drush make

Package management for Drupal

Page 73: node.js, javascript and the future

drush make

d.o - project namespace

d.o - inclusive project policy

Page 74: node.js, javascript and the future

drush make

a way to stay sane.

Page 75: node.js, javascript and the future

drush make

...and is part of drush proper now!

Page 76: node.js, javascript and the future

drush make

But I’d been using Drupal for YEARS by then

Page 77: node.js, javascript and the future

pear

Page 78: node.js, javascript and the future

pear

PHP Extension and Application Repository

Page 79: node.js, javascript and the future

pear

high threshold for new projects

Page 80: node.js, javascript and the future

imagine if pear was...

wildly inclusive

awesomely useful

awesomely successful

Page 81: node.js, javascript and the future

npm

wildly inclusive

awesomely useful

awesomely successful

Page 82: node.js, javascript and the future

npm

node package manager

Page 83: node.js, javascript and the future

packages

pear: 584

d.o: 15, 296 (~3,600 for D7)

npm: 7,976 (2 years old!)

Page 84: node.js, javascript and the future

npm - package.json

{ "author": "Jeff Miccolis <[email protected]>", "name": "portal", "description": "Data Catalog", "version": "0.0.0", "engines": { "node": "~v0.6.6" }, "dependencies": { "couchapp": "https://github.com/.../attachment_operators", "underscore": "= 1.2.0", "request": "= 2.1.1" }}

Page 85: node.js, javascript and the future

npm

you’ll love it.

Page 86: node.js, javascript and the future

5. Nice hammer

Page 87: node.js, javascript and the future

Nice hammer...

“Javascript, really?”

Page 88: node.js, javascript and the future

Nice hammer...

“Clearly he’s overly excited about this async stuff”

Page 89: node.js, javascript and the future

Nice hammer...

“...and thinks it’ll work for everything.”

Page 90: node.js, javascript and the future

“Do it with Drupal”, eh?

Page 91: node.js, javascript and the future

node.js is bad for...

computationally heavy tasksdatabases

Page 92: node.js, javascript and the future

node.js is awesome for...

interacting with other services.

Page 93: node.js, javascript and the future

services like...

databases, mail servers, web services, web clients..

Page 94: node.js, javascript and the future

Other people’s words

http://substack.net/posts/b96642

http://blog.nelhage.com/2012/03/why-node-js-is-cool/

Page 95: node.js, javascript and the future

Limited surface area

“The primary focus of most node modules is on using, not extending... A big part of what makes node modules so great is how they tend to have really obvious entry points as a consequence of focusing on usability and limited surface area”

Page 96: node.js, javascript and the future

Callback Austerity

“Instead of the http server being an external service that we configure to run our code, it becomes just another tool in our arsenal”

Page 97: node.js, javascript and the future

Async by default

"The upshot of this pressure is that, since essentially every node.js library works this way,

you can pick and choose arbitrary node.js libraries and combine them in the same

program, without even having to think about the fact that you’re doing so."

Page 98: node.js, javascript and the future

Try it!

If nothing else you’ll get better at javascript.

Page 99: node.js, javascript and the future

Try it!

But I bet you’ll like it.

Page 100: node.js, javascript and the future

Thanks!

Page 101: node.js, javascript and the future

Photo credit due to these wonderful people who offer their photos on flickr under a creative commons license:

Spam - http://www.flickr.com/photos/olivabassa/Cows - http://www.flickr.com/photos/lynndombrowski/Dogs - http://www.flickr.com/photos/photosightfaces/Squid - http://www.flickr.com/photos/laughingsquid/Ants - http://www.flickr.com/photos/fuzzcat/Stream - http://www.flickr.com/photos/universalpops/Boxes - http://www.flickr.com/photos/sillydog/Pear - http://www.flickr.com/photos/reebob/Hammer - http://www.flickr.com/photos/kefraya

Page 102: node.js, javascript and the future
Page 103: node.js, javascript and the future

What did you think?Locate this session on theDrupalCon Denver website

http://denver2012.drupal.org/program

Click the “Take the Survey” link.

Thank You!