SlideShare a Scribd company logo
1 of 28
Download to read offline
Emerging Paradigms -
Server Side Event Driven
     Programming
            Kamal Hussain
  http://www.linkedin.com/in/hussainkamal/
Agenda
●   Linear Vs Nonlinear Code
●   Concurrency through threads and events
●   Event loop
●   PHP and Javascript
●   Node.js - closeup
●   Important concepts
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))
Event driven approach
var callback = function(data) {
   console.log("firing callback " +
data);
};

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

console.log('Did you see callback?');
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
Asynchronous with
threads/processes
Asynchronous with events
Problems with Threads
●   Hard to program
●   Memory requirements are high
●   Large overhead
●   Context switching
●   Priority inversion
Threads wait




      udemy.com
Cost of IO

L1 Cache 3 Cycles
L2 Cache 14 Cycles
    RAM 250 cycles
    Disk 41 000 000 cycles
 Network 240 000 000 cycles
Event Loop Architecture




      courtsey: http://www.johanndutoit.net/
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
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
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.
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
Node.js - asynchronous I/O
●   First class functions
●   Closures
●   Event loop
●   Callback counters

CPU intensive tasks are delegated to workers
PHP Way
$fp = fopen("fp.txt", 'w)
fwrite($fp, "hello world");
fclose($fp);
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) {
    });
  });
});
Node.js simple example
var http = require('http');
http.createServer(function(req, res) {
  res.writeHead(200, {'Content-Type' :
'text/plain'});
  res.end("Hello Worldn");
}).listen(8000, "127.0.0.1");

console.log("Server running at http:
//127.0.0.1:8000");
Node.js Core APIs
Events
  EventTransmitter
  Event listener
  Event emitter
  Call back

Http

I/O
Node.js Workers

Synchronous call

Workers are blocked

Call returns
Workers Vs Events
Workers
 1 event per connection
 N workers per CPU

Events
  N connections per CPU
  1 process per CPU
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
Concepts to learn
First class functions
Lambdas - anonymous functions
Closures
Non-blocking IO
Deploying Node.js




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



         Learn Javascript and
       functional programming.

         Future is brighter :)
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

More Related Content

What's hot

Node.js and How JavaScript is Changing Server Programming
Node.js and How JavaScript is Changing Server Programming  Node.js and How JavaScript is Changing Server Programming
Node.js and How JavaScript is Changing Server Programming Tom Croucher
 
introduction to node.js
introduction to node.jsintroduction to node.js
introduction to node.jsorkaplan
 
node.js: Javascript's in your backend
node.js: Javascript's in your backendnode.js: Javascript's in your backend
node.js: Javascript's in your backendDavid Padbury
 
Building servers with Node.js
Building servers with Node.jsBuilding servers with Node.js
Building servers with Node.jsConFoo
 
Node.js Explained
Node.js ExplainedNode.js Explained
Node.js ExplainedJeff Kunkle
 
Getting started with developing Nodejs
Getting started with developing NodejsGetting started with developing Nodejs
Getting started with developing NodejsPhil Hawksworth
 
RESTful API In Node Js using Express
RESTful API In Node Js using Express RESTful API In Node Js using Express
RESTful API In Node Js using Express Jeetendra singh
 
Node.js Workshop - Sela SDP 2015
Node.js Workshop  - Sela SDP 2015Node.js Workshop  - Sela SDP 2015
Node.js Workshop - Sela SDP 2015Nir Noy
 
All aboard the NodeJS Express
All aboard the NodeJS ExpressAll aboard the NodeJS Express
All aboard the NodeJS ExpressDavid Boyer
 
Non-blocking I/O, Event loops and node.js
Non-blocking I/O, Event loops and node.jsNon-blocking I/O, Event loops and node.js
Non-blocking I/O, Event loops and node.jsMarcus Frödin
 
Introduction to Node js
Introduction to Node jsIntroduction to Node js
Introduction to Node jsAkshay Mathur
 
OSCON 2011 - Node.js Tutorial
OSCON 2011 - Node.js TutorialOSCON 2011 - Node.js Tutorial
OSCON 2011 - Node.js TutorialTom Croucher
 
Nodejs Explained with Examples
Nodejs Explained with ExamplesNodejs Explained with Examples
Nodejs Explained with ExamplesGabriele Lana
 
Original slides from Ryan Dahl's NodeJs intro talk
Original slides from Ryan Dahl's NodeJs intro talkOriginal slides from Ryan Dahl's NodeJs intro talk
Original slides from Ryan Dahl's NodeJs intro talkAarti Parikh
 
Intro to Node.js (v1)
Intro to Node.js (v1)Intro to Node.js (v1)
Intro to Node.js (v1)Chris Cowan
 
Node js presentation
Node js presentationNode js presentation
Node js presentationmartincabrera
 
Intro to node and non blocking io
Intro to node and non blocking ioIntro to node and non blocking io
Intro to node and non blocking ioAmy Hua
 

What's hot (20)

Node.js and How JavaScript is Changing Server Programming
Node.js and How JavaScript is Changing Server Programming  Node.js and How JavaScript is Changing Server Programming
Node.js and How JavaScript is Changing Server Programming
 
Introduction Node.js
Introduction Node.jsIntroduction Node.js
Introduction Node.js
 
introduction to node.js
introduction to node.jsintroduction to node.js
introduction to node.js
 
node.js: Javascript's in your backend
node.js: Javascript's in your backendnode.js: Javascript's in your backend
node.js: Javascript's in your backend
 
Nodejs in Production
Nodejs in ProductionNodejs in Production
Nodejs in Production
 
Building servers with Node.js
Building servers with Node.jsBuilding servers with Node.js
Building servers with Node.js
 
Node.js Explained
Node.js ExplainedNode.js Explained
Node.js Explained
 
Getting started with developing Nodejs
Getting started with developing NodejsGetting started with developing Nodejs
Getting started with developing Nodejs
 
RESTful API In Node Js using Express
RESTful API In Node Js using Express RESTful API In Node Js using Express
RESTful API In Node Js using Express
 
Node.js Workshop - Sela SDP 2015
Node.js Workshop  - Sela SDP 2015Node.js Workshop  - Sela SDP 2015
Node.js Workshop - Sela SDP 2015
 
All aboard the NodeJS Express
All aboard the NodeJS ExpressAll aboard the NodeJS Express
All aboard the NodeJS Express
 
Non-blocking I/O, Event loops and node.js
Non-blocking I/O, Event loops and node.jsNon-blocking I/O, Event loops and node.js
Non-blocking I/O, Event loops and node.js
 
Introduction to Node js
Introduction to Node jsIntroduction to Node js
Introduction to Node js
 
OSCON 2011 - Node.js Tutorial
OSCON 2011 - Node.js TutorialOSCON 2011 - Node.js Tutorial
OSCON 2011 - Node.js Tutorial
 
Nodejs Explained with Examples
Nodejs Explained with ExamplesNodejs Explained with Examples
Nodejs Explained with Examples
 
Node ppt
Node pptNode ppt
Node ppt
 
Original slides from Ryan Dahl's NodeJs intro talk
Original slides from Ryan Dahl's NodeJs intro talkOriginal slides from Ryan Dahl's NodeJs intro talk
Original slides from Ryan Dahl's NodeJs intro talk
 
Intro to Node.js (v1)
Intro to Node.js (v1)Intro to Node.js (v1)
Intro to Node.js (v1)
 
Node js presentation
Node js presentationNode js presentation
Node js presentation
 
Intro to node and non blocking io
Intro to node and non blocking ioIntro to node and non blocking io
Intro to node and non blocking io
 

Similar to Server Side Event Driven Programming

Introduction to Node.js Platform
Introduction to Node.js PlatformIntroduction to Node.js Platform
Introduction to Node.js PlatformNaresh Chintalcheru
 
A language for the Internet: Why JavaScript and Node.js is right for Internet...
A language for the Internet: Why JavaScript and Node.js is right for Internet...A language for the Internet: Why JavaScript and Node.js is right for Internet...
A language for the Internet: Why JavaScript and Node.js is right for Internet...Tom Croucher
 
Node js
Node jsNode js
Node jshazzaz
 
Introduction to Node.js
Introduction to Node.jsIntroduction to Node.js
Introduction to Node.jsVikash Singh
 
Node.js introduction
Node.js introductionNode.js introduction
Node.js introductionParth Joshi
 
Developing realtime apps with Drupal and NodeJS
Developing realtime apps with Drupal and NodeJS Developing realtime apps with Drupal and NodeJS
Developing realtime apps with Drupal and NodeJS drupalcampest
 
JS Fest 2019: Comparing Node.js processes and threads for clustering HTTP, TC...
JS Fest 2019: Comparing Node.js processes and threads for clustering HTTP, TC...JS Fest 2019: Comparing Node.js processes and threads for clustering HTTP, TC...
JS Fest 2019: Comparing Node.js processes and threads for clustering HTTP, TC...Vitalii Kukhar
 
JS Fest 2019/Autumn. Виталий Кухар. Сравнение кластеризации HTTP, TCP и UDP н...
JS Fest 2019/Autumn. Виталий Кухар. Сравнение кластеризации HTTP, TCP и UDP н...JS Fest 2019/Autumn. Виталий Кухар. Сравнение кластеризации HTTP, TCP и UDP н...
JS Fest 2019/Autumn. Виталий Кухар. Сравнение кластеризации HTTP, TCP и UDP н...JSFestUA
 
Integrating Node.js with PHP
Integrating Node.js with PHPIntegrating Node.js with PHP
Integrating Node.js with PHPLee Boynton
 
Introduce about Nodejs - duyetdev.com
Introduce about Nodejs - duyetdev.comIntroduce about Nodejs - duyetdev.com
Introduce about Nodejs - duyetdev.comVan-Duyet Le
 
Node js introduction
Node js introductionNode js introduction
Node js introductionAlex Su
 
Node.js for Rubists
Node.js for RubistsNode.js for Rubists
Node.js for RubistsSagiv Ofek
 

Similar to Server Side Event Driven Programming (20)

Event driven programming -- Node.JS
Event driven programming -- Node.JSEvent driven programming -- Node.JS
Event driven programming -- Node.JS
 
Node.js
Node.jsNode.js
Node.js
 
NodeJS
NodeJSNodeJS
NodeJS
 
Introduction to Node.js Platform
Introduction to Node.js PlatformIntroduction to Node.js Platform
Introduction to Node.js Platform
 
Introduction to Node.js
Introduction to Node.jsIntroduction to Node.js
Introduction to Node.js
 
A language for the Internet: Why JavaScript and Node.js is right for Internet...
A language for the Internet: Why JavaScript and Node.js is right for Internet...A language for the Internet: Why JavaScript and Node.js is right for Internet...
A language for the Internet: Why JavaScript and Node.js is right for Internet...
 
Node js
Node jsNode js
Node js
 
Introduction to Node.js
Introduction to Node.jsIntroduction to Node.js
Introduction to Node.js
 
Nodejs
NodejsNodejs
Nodejs
 
Node.js introduction
Node.js introductionNode.js introduction
Node.js introduction
 
Intro to Node
Intro to NodeIntro to Node
Intro to Node
 
Developing realtime apps with Drupal and NodeJS
Developing realtime apps with Drupal and NodeJS Developing realtime apps with Drupal and NodeJS
Developing realtime apps with Drupal and NodeJS
 
JS Fest 2019: Comparing Node.js processes and threads for clustering HTTP, TC...
JS Fest 2019: Comparing Node.js processes and threads for clustering HTTP, TC...JS Fest 2019: Comparing Node.js processes and threads for clustering HTTP, TC...
JS Fest 2019: Comparing Node.js processes and threads for clustering HTTP, TC...
 
JS Fest 2019/Autumn. Виталий Кухар. Сравнение кластеризации HTTP, TCP и UDP н...
JS Fest 2019/Autumn. Виталий Кухар. Сравнение кластеризации HTTP, TCP и UDP н...JS Fest 2019/Autumn. Виталий Кухар. Сравнение кластеризации HTTP, TCP и UDP н...
JS Fest 2019/Autumn. Виталий Кухар. Сравнение кластеризации HTTP, TCP и UDP н...
 
Integrating Node.js with PHP
Integrating Node.js with PHPIntegrating Node.js with PHP
Integrating Node.js with PHP
 
Node js beginner
Node js beginnerNode js beginner
Node js beginner
 
Node.js 1, 2, 3
Node.js 1, 2, 3Node.js 1, 2, 3
Node.js 1, 2, 3
 
Introduce about Nodejs - duyetdev.com
Introduce about Nodejs - duyetdev.comIntroduce about Nodejs - duyetdev.com
Introduce about Nodejs - duyetdev.com
 
Node js introduction
Node js introductionNode js introduction
Node js introduction
 
Node.js for Rubists
Node.js for RubistsNode.js for Rubists
Node.js for Rubists
 

Recently uploaded

Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsNanddeep Nachan
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Jeffrey Haguewood
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Zilliz
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...Zilliz
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesrafiqahmad00786416
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024The Digital Insurer
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelDeepika Singh
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024The Digital Insurer
 

Recently uploaded (20)

Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 

Server Side Event Driven Programming

  • 1. Emerging Paradigms - Server Side Event Driven Programming Kamal Hussain http://www.linkedin.com/in/hussainkamal/
  • 2. Agenda ● Linear Vs Nonlinear Code ● Concurrency through threads and events ● Event loop ● PHP and Javascript ● Node.js - closeup ● Important concepts
  • 3. 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))
  • 4. Event driven approach var callback = function(data) { console.log("firing callback " + data); }; $.get('/endpoint', callback); console.log('Did you see callback?');
  • 5. 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
  • 8. Problems with Threads ● Hard to program ● Memory requirements are high ● Large overhead ● Context switching ● Priority inversion
  • 9. Threads wait udemy.com
  • 10. Cost of IO L1 Cache 3 Cycles L2 Cache 14 Cycles RAM 250 cycles Disk 41 000 000 cycles Network 240 000 000 cycles
  • 11.
  • 12. Event Loop Architecture courtsey: http://www.johanndutoit.net/
  • 13. 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
  • 14. 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
  • 15. 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.
  • 16. 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
  • 17. Node.js - asynchronous I/O ● First class functions ● Closures ● Event loop ● Callback counters CPU intensive tasks are delegated to workers
  • 18. PHP Way $fp = fopen("fp.txt", 'w) fwrite($fp, "hello world"); fclose($fp);
  • 19. 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) { }); }); });
  • 20. Node.js simple example var http = require('http'); http.createServer(function(req, res) { res.writeHead(200, {'Content-Type' : 'text/plain'}); res.end("Hello Worldn"); }).listen(8000, "127.0.0.1"); console.log("Server running at http: //127.0.0.1:8000");
  • 21. Node.js Core APIs Events EventTransmitter Event listener Event emitter Call back Http I/O
  • 22. Node.js Workers Synchronous call Workers are blocked Call returns
  • 23. Workers Vs Events Workers 1 event per connection N workers per CPU Events N connections per CPU 1 process per CPU
  • 24. 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
  • 25. Concepts to learn First class functions Lambdas - anonymous functions Closures Non-blocking IO
  • 27. Key takeaway Learn Javascript and functional programming. Future is brighter :)
  • 28. 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