42
3 rd Generation Web Application Platforms Naresh Chintalcheru

3rd Generation Web Application Platforms

Embed Size (px)

DESCRIPTION

3rd Generation Web Application Platforms- Node.js

Citation preview

Page 1: 3rd Generation Web Application Platforms

3rd Generation Web Application PlatformsNaresh Chintalcheru

Page 2: 3rd Generation Web Application Platforms

1st Generation

Page 3: 3rd Generation Web Application Platforms

CGI : Common Gateway Interface

Page 4: 3rd Generation Web Application Platforms

CGI Scripting languages▪CGI C▪CGI Perl▪CGI TCL▪CGI Basic

Page 5: 3rd Generation Web Application Platforms

One Process per User Request

[Operating System Processes, Threads, Semaphores, Mutexes and Barriers, are the scheduling mechanisms to acquire the time slices of the CPU. The Processes are expensive and consume more resources than Threads]

Page 6: 3rd Generation Web Application Platforms

Drawbacks:

▪ The CGI script interpretation (compile + execution) happens at the request processing time.

▪ One user request spawns one Process, locking all the allocated resources even though request processing might not need it.

▪ The CGI spawned Process is outside the Web Server control.

Page 7: 3rd Generation Web Application Platforms

2nd Generation

Page 8: 3rd Generation Web Application Platforms

ISAPI Engines

ISAPI : Internet Server Application Programming Interface

Page 9: 3rd Generation Web Application Platforms

2nd Generation Platforms

▪ J2EE▪ .Net▪ LAMP▪ Ruby on Rails▪ Groovy on Grails

Page 10: 3rd Generation Web Application Platforms

What is common in the 2nd generation platforms ?

Page 11: 3rd Generation Web Application Platforms

Multi-Threaded Runtime

Page 12: 3rd Generation Web Application Platforms

One Thread per User Request

Page 14: 3rd Generation Web Application Platforms

JEE Request-To-Thread

Spring DispatcherServlet

JSF FacesServlet

Struts ActionServlet

Request 1

Request 2

Request .. n

One Servlet instance on the App Server spawns a new thread for each user request

Thread 1

Thread 2

Thread .. n

Application Server

Page 15: 3rd Generation Web Application Platforms

Drawbacks:

▪ One thread per user request locks the resources allocated to the thread at the time (wait) of I/O operations.

▪ Context-switching between threads are expensive.

Page 16: 3rd Generation Web Application Platforms

3rd Generation

Page 17: 3rd Generation Web Application Platforms

Single Thread Runtime

Page 18: 3rd Generation Web Application Platforms

One Thread for [many] Users Request

Page 20: 3rd Generation Web Application Platforms

How it is possible one Thread can serve multiple users ?

Page 21: 3rd Generation Web Application Platforms

▪Lets see how many cycles it takes for a Disk I/O

Page 23: 3rd Generation Web Application Platforms

▪It takes around 41 million cycles to do a disk I/O.

▪For a network call 240 million cycles ….

“Understanding Node Event-Loop” – Must read excellent blog post from Mixu.

Page 24: 3rd Generation Web Application Platforms

▪Swim into the micro millisecond world.

▪Everything in the multi-threaded server is …..

Page 25: 3rd Generation Web Application Platforms

Waiting … Waiting … Waiting … Waiting …Waiting … Waiting … Waiting … Waiting …Waiting … Waiting … Waiting … Waiting …Waiting … Waiting … Waiting … Waiting … Waiting … Waiting … Waiting … Waiting . Waiting … Waiting … Waiting … WaitWaiting…Waiting…Waiting…WaitWaiting … Waiting …Wait

Page 26: 3rd Generation Web Application Platforms

▪While the user request is waiting for an I/O operation, the 3rd generation platforms use Non-Blocking I/O based call-backs managed by an Event-Loop and the Thread will move on to the next request.

▪The whole thing happens so fast that one Thread will be able to respond to multiple user requests.

Page 27: 3rd Generation Web Application Platforms

3rd generation platforms

▪Node.js for JavaScript▪Vert.x for Java▪Libevent for C▪Twisted for Python▪EventMachine for Ruby

Page 28: 3rd Generation Web Application Platforms

3rd generation platforms

The rest of the presentation will focus on

Node.js

Page 29: 3rd Generation Web Application Platforms

Unbelievable Concurrency ?

▪ Assuming each thread has 2 MB of memory running on 8 GB of RAM puts it at a theoretical maximum of 4000 concurrent connections, plus the cost of context-switching between threads.

▪ That’s the scenario you typically deal with in traditional web-serving techniques. By avoiding all that, Node.js achieves scalability levels of over

1Million concurrent connections (as a proof-of-concept).

Page 30: 3rd Generation Web Application Platforms

The 3rd generation platforms use the combination below to efficiently utilize the OS resources.

Single Thread Non-Blocking I/O

Event Loop

Page 31: 3rd Generation Web Application Platforms

Node.js▪ Platform based Server-side JavaScript▪ Event-Driven Concurrency Model▪ Use single thread and non-blocking I/O▪ JSON and JavaScript are practically siblings that translate to Nodes▪ JavaScript is a dynamically typed language▪ Real-time web applications employing server-side push and two-way connections where server and client can initiate communication

▪ I/O Operations executed in parallel but not the code

Page 32: 3rd Generation Web Application Platforms

▪So throw-out JEE/.Net applications and rewrite in node.js ?

Page 33: 3rd Generation Web Application Platforms

Node.js makes it an excellent choice

for I/O based Applications.

What are I/O based Applications?Applications which connect to databases, make web service calls, use files for logging and make connections to MQ, Mail & LDAP servers.

Page 34: 3rd Generation Web Application Platforms

Node.js is not a good choice for CPU intensive Applications.

(Node pundits argue that CPU intensive components should be evented and passed onto another thread)

What are CPU intensive Applications?Applications which does lot of data processing on the App server and complex calculations which need more CPU time.

Page 35: 3rd Generation Web Application Platforms

Node.js itself is not Non-Blockingfunction getAge(user_id) { sql = 'SELECT age FROM users WHERE id = ' + user_id; connection.query(sql, function(err, rows, fields) { if (err) throw err; return rows[0].age; });return 0;}

[The above database call code will starve the thread and node will not respond to other users]

Page 36: 3rd Generation Web Application Platforms

Below Node.js code will unblock I/O and respond to other users

function getAge(user_id, callback) { sql = 'SELECT age FROM users WHERE id = ' + user_id; connection.query(sql, function(err, rows, fields) { if (err) throw err; var age = rows[0].age; callback(age); });}

[The above database call code uses callbacks and unblock the I/O]

Page 37: 3rd Generation Web Application Platforms

Programming for Node.js requires clear understanding of:

▪Asynchronous code▪Event-Driven▪Non-Blocking I/O▪Callbacks and Promises

Page 38: 3rd Generation Web Application Platforms

Who is using Node.js ?

Paypal, LinkedIn, eBay,

Yahoo, Walmart ……………

http://nodejs.org/industry/

Page 39: 3rd Generation Web Application Platforms

Node.js does not frameworks ?

Ever growing list of presentation, data persistence and many more frameworks ……………

http://nodeframework.com/

Page 40: 3rd Generation Web Application Platforms

With the ever growing cost of hardware, power to cool them and long maintenance cycles ….

How long will enterprises withstand the temptation of

3rd Generation Platform ?

Page 41: 3rd Generation Web Application Platforms

Disclaimer:The views and opinions expressed in this article are those of the author. The metrics and numbers used in this presentation are from the open blogs which are not validated. This is a technical presentation which will make you think about the new platform options in the technology.

Page 42: 3rd Generation Web Application Platforms

References:▪ http://labs.vmware.com/vmtj/autonomous-resource-sharing-for-multi-

threaded-workloads-in-virtualized-servers▪ http://adamgent.com/post/10440924094/does-java-have-an-answer-to-

node-js▪ http://www.toptal.com/nodejs/why-the-hell-would-i-use-node-js▪ http://blog.caustik.com/2012/08/19/node-js-w1m-concurrent-connections/▪ http://bijoor.me/2013/06/09/java-ee-threads-vs-node-js-which-is-better-for-

concurrent-data-processing-operations/▪ http://www.ibm.com/developerworks/java/library/j-nodejs/index.html▪ http://www.qnx.com/developers/docs/6.4.1

/neutrino/getting_started/s1_procs.html#More_synchronization▪ http://zef.me/5390/decoupling-events-vs-dependency-injection▪ http://highscalability.com/blog/2013/3/18/beyond-threads-and-callbacks-

application-architecture-pros-a.html▪ http://stackoverflow.com/questions/4296505/understanding-promises-in-

node-js