Your SlideShare is downloading. ×
Server Side Event Driven Programming
Upcoming SlideShare
Loading in...5

Thanks for flagging this SlideShare!

Oops! An error has occurred.

Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Server Side Event Driven Programming


Published on

Emerging paradigms - Server side event driven programming.

Emerging paradigms - Server side event driven programming.

Published in: Technology
  • Be the first to comment

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

No notes for slide


  • 1. Emerging Paradigms -Server Side Event Driven Programming Kamal Hussain
  • 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("")val statusCode = client.executeMethod(method)println("Server responded with %d" .format(statusCode))
  • 4. Event driven approachvar 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
  • 6. Asynchronous withthreads/processes
  • 7. Asynchronous with events
  • 8. Problems with Threads● Hard to program● Memory requirements are high● Large overhead● Context switching● Priority inversion
  • 9. Threads wait
  • 10. Cost of IOL1 Cache 3 CyclesL2 Cache 14 Cycles RAM 250 cycles Disk 41 000 000 cycles Network 240 000 000 cycles
  • 11. Event Loop Architecture courtsey:
  • 12. Writing synchronous VsAsynchronous// Good: write files asynchronouslyfs.writeFile(message.txt, Hello Node, function (err) { console.log("Its saved and the server remains responsive!");});// BAD: write files synchronouslyfs.writeFileSync(message.txt, Hello Node);console.log("Its saved, but you just blocked ALL requests!");This can cause performance drop from thousands of requests/seconds to a fewdozen/second.
  • 13. 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
  • 14. 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.
  • 15. Whats Node.js?"Node.js is a server-side software system designed forwriting scalable Internet applications, notably web servers.Programs are written on the server side in JavaScript,using event-driven, asynchronous I/O to minimize overheadand maximize scalability." - from wikipedia
  • 16. Node.js - asynchronous I/O● First class functions● Closures● Event loop● Callback countersCPU intensive tasks are delegated to workers
  • 17. PHP Way$fp = fopen("fp.txt", w)fwrite($fp, "hello world");fclose($fp);
  • 18. Node.js wayvar fs = require(fs);, w, 0666,function(error, fp) { fw.write(fp, helloworld, null,utf-8, function() { fs.close(fp, function(error) { }); });});
  • 19. Node.js simple examplevar http = require(http);http.createServer(function(req, res) { res.writeHead(200, {Content-Type :text/plain}); res.end("Hello Worldn");}).listen(8000, "");console.log("Server running at");
  • 20. Node.js Core APIsEvents EventTransmitter Event listener Event emitter Call backHttpI/O
  • 21. Node.js WorkersSynchronous callWorkers are blockedCall returns
  • 22. Workers Vs EventsWorkers 1 event per connection N workers per CPUEvents N connections per CPU 1 process per CPU
  • 23. Node.js Typical Applications● Proxy● API server ○ REST API calls ○ Simple transformationsSee performance comparisons at:
  • 24. Concepts to learnFirst class functionsLambdas - anonymous functionsClosuresNon-blocking IO
  • 25. Deploying Node.js
  • 26. Key takeaway Learn Javascript and functional programming. Future is brighter :)
  • 27. 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● programmers-1-event● nodejs-event-loop-91298● in-node-dot-js