28
Emerging Paradigms - Server Side Event Driven Programming Kamal Hussain http://www.linkedin.com/in/hussainkamal/

Server Side Event Driven Programming

Embed Size (px)

DESCRIPTION

Emerging paradigms - Server side event driven programming.

Citation preview

Page 1: Server Side Event Driven Programming

Emerging Paradigms - Server Side Event Driven

Programming

Kamal Hussainhttp://www.linkedin.com/in/hussainkamal/

Page 2: Server Side Event Driven Programming

Agenda

● Linear Vs Nonlinear Code● Concurrency through threads and events● Event loop● PHP and Javascript● Node.js - closeup● Important concepts

Page 3: Server Side Event Driven Programming

We are used to ..

val client = new HttpClient()

val method = new GetMethod("http://www.google.com")

val statusCode = client.executeMethod(method)

println("Server responded with %d" . format(statusCode))

Page 4: Server Side Event Driven Programming

Event driven approach

var callback = function(data) {

console.log("firing callback " + data);

};

$.get('/endpoint', callback);

console.log('Did you see callback?');

Page 5: Server Side Event Driven Programming

Achieving scale and concurrency

● Multiple threads/processes● Size the threadpool correctly● Each thread is responsible for one task such

as serving a request● Asynchronous programming

Page 6: Server Side Event Driven Programming

Asynchronous with threads/processes

Page 7: Server Side Event Driven Programming

Asynchronous with events

Page 8: Server Side Event Driven Programming

Problems with Threads

● Hard to program● Memory requirements are high● Large overhead● Context switching● Priority inversion

Page 9: Server Side Event Driven Programming

Threads wait

udemy.com

Page 10: Server Side Event Driven Programming

Cost of IO

L1 Cache 3 Cycles

L2 Cache 14 Cycles

RAM 250 cycles

Disk 41 000 000 cycles

Network 240 000 000 cycles

Page 11: Server Side Event Driven Programming
Page 12: Server Side Event Driven Programming

Event Loop Architecture

courtsey: http://www.johanndutoit.net/

Page 13: Server Side Event Driven Programming

Writing synchronous Vs Asynchronous// Good: write files asynchronously

fs.writeFile('message.txt', 'Hello Node', function (err) {

console.log("It's saved and the server remains responsive!");

});

// BAD: write files synchronously

fs.writeFileSync('message.txt', 'Hello Node');

console.log("It's saved, but you just blocked ALL requests!");

This can cause performance drop from thousands of requests/seconds to a few dozen/second.http://engineering.linkedin.com/nodejs/blazing-fast-nodejs-10-performance-tips-linkedin-mobile

Page 14: Server Side Event Driven Programming

Tale of two languages● PHP was invented in 1994 by Rasmus

Lerdorfas as a replacement for CGI scripts● PHP was a substitute for single-threaded C

programs

● Brendan Eich developed Javascript in 1995 for a completely different purpose. JS was designed to run within Netscape Navigator and was primarily designed to handle events.

PHP -> eventless, Javascript -> eventful

Page 15: Server Side Event Driven Programming

Node.js

● Ryan Dahl invented Node.js in 2009 as a continuation of Javascript heritage.

● Node.js is modeled after multi-task, multi-page handling of a web server.

Page 16: Server Side Event Driven Programming

What's Node.js?"Node.js is a server-side software system designed for writing scalable Internet applications, notably web servers. Programs are written on the server side in JavaScript, using event-driven, asynchronous I/O to minimize overhead and maximize scalability."

- from wikipedia

Page 17: Server Side Event Driven Programming

Node.js - asynchronous I/O

● First class functions● Closures● Event loop● Callback counters

CPU intensive tasks are delegated to workers

Page 18: Server Side Event Driven Programming

PHP Way

$fp = fopen("fp.txt", 'w)

fwrite($fp, "hello world");

fclose($fp);

Page 19: Server Side Event Driven Programming

Node.js way

var fs = require('fs');

fs.open('fp.txt', 'w', 0666, function(error, fp) {

fw.write(fp, 'helloworld', null, 'utf-8', function() {

fs.close(fp, function(error) {

});

});

});

Page 20: Server Side Event Driven Programming

Node.js simple example

var http = require('http');

http.createServer(function(req, res) {

res.writeHead(200, {'Content-Type' : 'text/plain'});

res.end("Hello World\n");

}).listen(8000, "127.0.0.1");

console.log("Server running at http://127.0.0.1:8000");

Page 21: Server Side Event Driven Programming

Node.js Core APIs

EventsEventTransmitter

Event listener Event emitter Call back

Http

I/O

Page 22: Server Side Event Driven Programming

Node.js Workers

Synchronous call

Workers are blocked

Call returns

Page 23: Server Side Event Driven Programming

Workers Vs Events

Workers1 event per connectionN workers per CPU

EventsN connections per CPU1 process per CPU

Page 24: Server Side Event Driven Programming

Node.js Typical Applications

● Proxy

● API server○ REST API calls○ Simple transformations

See performance comparisons at:http://www.slideshare.net/FabianFrankDe/nodejs-performance-case-study

Page 25: Server Side Event Driven Programming

Concepts to learn

First class functionsLambdas - anonymous functionsClosuresNon-blocking IO

Page 26: Server Side Event Driven Programming

Deploying Node.js

http://www.slideshare.net/BenLin2/webconf-nodejsproductionarchitecture

Page 27: Server Side Event Driven Programming

Key takeaway

Learn Javascript and functional programming.

Future is brighter :)

Page 28: Server Side Event Driven Programming

Reference

● Node - Up and Running by Tom Hughes-Croucher, Mike Wilson - Oreilly

● Node.js for PHP Developers - Oreilly● Javascript: The definitive guide - Oreilly● LinkedIn, Netflix Blogs● http://architects.dzone.com/articles/nodejs-php-

programmers-1-event● https://www.udemy.com/lectures/understanding-the-

nodejs-event-loop-91298● https://speakerdeck.com/guldenpt/before-start-coding-

in-node-dot-js