© 2015 IBM Corporation1
Java vs Node.js
For Enterprise Web Applications
Chris Bailey
IBM Runtime Technologies
© 2015 IBM Corporation2
What Languages do you use?
Java JavaScript Both
0
20
40
60
80
100
120
PercentageofAudience
© 2015 IBM Corporation3
What Runtimes do you use?
Server Java Applets JS in Browser Node.js Rhino Nashorn Avatar.js
0
20
40
60
80
100
120
PercentageofAudience
© 2015 IBM Corporation4
4
Chris Bailey
IBM Runtime Monitoring and Diagnostics Architect
–14 years working with Java and JVM technologies
–14 months working with Node.js and V8
Current Role(s):
–IBM Java monitoring and diagnostics
–Node Application Metrics project lead
Node Foundation Member
Benchmarking and Performance WGs
JavaOne RockStar
Author on performance and memory analysis
baileyc@uk.ibm.com
chrisbaileyibm
cnbailey
@seabaylea
@Chris__Bailey
© 2015 IBM Corporation5

Language Adoption

Deployment Modes

Code Development

Scalability

WebApplication Performance

Under the Hood

Enterprise Architectures
Agenda
© 2015 IBM Corporation6
6
Language Adoption
© 2015 IBM Corporation7
GitHub Adoption: Java
© 2015 IBM Corporation8
GitHub Adoption: JavaScript
© 2015 IBM Corporation9
modulecounts.com
© 2015 IBM Corporation10
StackOverflow User Survey
© 2015 IBM Corporation11
Ratings based on the number of skilled engineers, courses and third party vendors.
Tiobe Community Programming Index
© 2015 IBM Corporation12
Indeed.com Job Trends: Java
© 2015 IBM Corporation13
Indeed.com Job Trends: JavaScript
© 2015 IBM Corporation14
Indeed.com Job Trends
Java
JavaScript
© 2015 IBM Corporation15

JavaScript has a large developer base
- #1 on GitHub with 45% more active repositories than Java
- #1 on modulecounts.com with 29% more NPM modules than
Maven
- #1 used language by StackOverflow survey responders
- #6 language on the Tiobe index

Java remains huge, particularly in the enterprise and on the server
- #2 on GitHub with 52% more active repositories than the next
language
- #3 on modulecounts with 73.8% more modules than the next
language
- #2 language on the Tiobe index
- #1 on indeed.com for developer jobs
Language Adoption
© 2015 IBM Corporation16
Deployment Modes
© 2015 IBM Corporation17

JavaScript is ubiquitous in the browser
- Supported in every browser
- Integration with HTML and CSS

JavaScript is not affected by negative publicity....
Unless it is absolutely necessary to run Java in web browsers, disable it as described
below, even after updating to 7u11. This will help mitigate other Java vulnerabilities that
may be discovered in the future.
This and previous Java vulnerabilities have been widely targeted by attackers, and
new Java vulnerabilities are likely to be discovered. To defend against this and future
Java vulnerabilities, consider disabling Java in web browsers…
Usage in the browser
© 2015 IBM Corporation18

Java has a long history on the server
- JPE launched in 1998

Java has rich platform support:
- Linux x86, Linux POWER, zLinux
- Windows, Mac OS, Solaris, AIX, z/OS

JavaScript is a nascent language on the server
- Limited platform support – although its growing
- No API support to interact with the OS

Part of the browser security model
- Frameworks like Node.js have changed that.
Usage on the server
© 2015 IBM Corporation19

Single Threaded Event based JavaScript framework
– Uses non-blocking asynchronous I/O

Wraps the Chrome V8 JavaScript engine with I/O interfaces
– Libuv provides interaction with OS/system

Designed to build scalable network applications
– Suited for real time delivery of data to distributed client

Available on a wide set of platforms:
- Linux on x86, ARM, Power and Z
- Windows, Mac OS, Solaris, SmartOS and AIX
libuvV8
Node Bindings
Node Standard Library
C
JavaScript
Server Side JavaScript: Node.js
© 2015 IBM Corporation20
Anatomy of a Node.js application
1. package.json
–
Tracks properties, dependencies, version info, commands, etc.
2. JavaScript source files (i.e. app.js)
•To install and execute:
> npm install
> node app.js
20
© 2015 IBM Corporation21
Code Development
© 2015 IBM Corporation22
var cluster = require('cluster');
var cpus = require('os').cpus().length;
var http = require('http');
if (cluster.isMaster) {
for (var i = 0; i < cpus; i++) {
cluster.fork();
}
cluster.on('death', function(worker) {
console.log("Worker" + worker.pid + "died");
});
} else {
http.createServer(function(request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World!n");
response.end();
}).listen(8080);
}
HTTP Server Example
© 2015 IBM Corporation23
var cluster = require('cluster');
var cpus = require('os').cpus().length;
var http = require('http');
if (cluster.isMaster) {
for (var i = 0; i < cpus; i++) {
cluster.fork();
}
cluster.on('death', function(worker) {
console.log("Worker" + worker.pid + "died");
});
} else {
http.createServer(function(request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World!n");
response.end();
}).listen(8080);
}
Clustered HTTP Server Example
© 2015 IBM Corporation24
Writing Node.js Code
CodeVolumeRelativetoJava
* based on benchmarksgame benchmarks
Node.js
Development Effort
(lower is better)
- 3x
*or other transactional service
© 2015 IBM Corporation25
Writing Node.js Code
regex-dna Spectral-norm fannkuch-redux fasta k-nucleotide Binary-trees n-body Reverse-complement
CodeVolumeRelativetoJava
* based on benchmarksgame benchmarks
Node.js
Development Effort
(lower is better)
- 3x
Avg 1/3rd
less code
*or other transactional service
© 2015 IBM Corporation26
Writing Node.js Code
regex-dna Spectral-norm fannkuch-redux fasta k-nucleotide Binary-trees n-body Reverse-complement
CodeVolumeRelativetoJava
* based on benchmarksgame benchmarks
Node.js
Development Effort
(lower is better)
- 3x
●
Node.js has higher developer productivity
Many applications developed with significantly less code
●
Rich module system simplifies development
Reduces need to develop custom code
Avg 1/3rd
less code
*or other transactional service
© 2015 IBM Corporation27
Scalability
© 2015 IBM Corporation28

One thread (or process) per connection
- Each thread waits on a response
- Scalability determined by the number
of threads

Each thread:
- consumes memory
- is relatively idle

Number of concurrent customers
determined by number of depot workers

Additional customers wait in a queue with no
response
Typical approach to I/O
© 2015 IBM Corporation29

One thread multiplexes for multiple
requests
- No waiting for a response
- Handles return from I/O when
notified

Scalability determined by:
- CPU usage
- “Back end” responsiveness

Number of concurrent customers
determined by how fast the food Server
can work

Or until the kitchen gets slammed
Asycnhronous Non-Blocking I/O
© 2015 IBM Corporation30

JavaScript is already event based in the browser
- eg. onClick and onMouseOver events

First class functions and closures fit well with events
- Easy to create and pass function callbacks
- Easy to execute callbacks in the context of the event

Node.js execution is based on an event loop
- Asynchronous I/O built in from the ground up

Node.js execution uses a single thread
- No need to worry about locking or shared data
- Most machines are now multi-CPU, so cluster capabilities are
provided
JavaScript and Asynchronous I/O
© 2015 IBM Corporation31
var http = require('http');
var server = http.createServer();
server.listen(8080);
server.on('request', function(request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World!n");
response.end();
});
server.on('connection', function(socket) {});
server.on('close', function() {});
server.on('connect', function(socket) {});
server.on('upgrade', function(request, socket, head) {});
server.on('clientError', function(exception, socket) {});
Event based programming
© 2015 IBM Corporation32
WebApp Performance
© 2015 IBM Corporation33

JSON serialization of a
newly instantiated object

Maps
- Key of message
- Value of Hello,
World!

Example response:
Results from TechEmpower.com Round 9 tests (2014-05-01)
JSON Serialization
© 2015 IBM Corporation34

JSON serialization of a
newly instantiated object

Maps
- Key of message
- Value of Hello,
World!

Example response:
Results from TechEmpower.com Round 9 tests (2014-05-01)
JSON Serialization
JavaScript
Java
© 2015 IBM Corporation35
JavaScript WebApp Performance
-100
-80
-60
-40
-20
0
20
40
-75
Node.js Performance
(Compared to Java)
JSON Serialization
%ageofJavaPerformance
© 2015 IBM Corporation36

Fetches single row from
simple database table

Row serialized as JSON

Example response:
Results from TechEmpower.com Round 9 tests (2014-05-01)
Single Query
© 2015 IBM Corporation37

Fetches single row from
simple database table

Row serialized as JSON

Example response:
Results from TechEmpower.com Round 9 tests (2014-05-01)
Single Query
JavaScript
Java
© 2015 IBM Corporation38
JavaScript WebApp Performance
-100
-80
-60
-40
-20
0
20
40
-60.5
Node.js Performance
(Compared to Java)
JSON Serialization
Single Query
%ageofJavaPerformance
© 2015 IBM Corporation39

Fetches multiple rows from
a simple database table

Rows serialized as JSON

Example response:
Results from TechEmpower.com Round 9 tests (2014-05-01)
Multiple Queries
© 2015 IBM Corporation40

Fetches multiple rows from
a simple database table

Rows serialized as JSON

Example response:
Results from TechEmpower.com Round 9 tests (2014-05-01)
Multiple Queries
JavaScript
Java
© 2015 IBM Corporation41
JavaScript WebApp Performance
-100
-80
-60
-40
-20
0
20
40
-18
Node.js Performance
(Compared to Java)
JSON Serialization
Single Query
Multiple Queries
%ageofJavaPerformance
© 2015 IBM Corporation42

Fetches multiple rows from
a simple database table

Converts rows to objects
and modifies one attribute
of each object

Updates each associated
row and serializes as
JSON

Example Response:
Data Updates
Results from TechEmpower.com Round 9 tests (2014-05-01)
© 2015 IBM Corporation43

Fetches multiple rows from
a simple database table

Converts rows to objects
and modifies one attribute
of each object

Updates each associated
row and serializes as
JSON

Example Response:
Data Updates
Results from TechEmpower.com Round 9 tests (2014-05-01)
JavaScript
Java
© 2015 IBM Corporation44
JavaScript WebApp Performance
-100
-80
-60
-40
-20
0
20
40
28
Node.js Performance
(Compared to Java)
JSON Serialization
Single Query
Multiple Queries
Data Updates
%ageofJavaPerformance
© 2015 IBM Corporation45

Computation speed is (much) slower than Java

I/O speed is higher than Java
JavaScript WebApp Performance
-100
-80
-60
-40
-20
0
20
40
-75 -60.5 -18
28
Node.js Performance
(Compared to Java)
JSON Serialization
Single Query
Multiple Queries
Data Updates
%ageofJavaPerformance
More
Computation
More
I/O
© 2015 IBM Corporation46
JavaScript
on the JVM?
© 2015 IBM Corporation47
Nashorn and Avatar.js

Nashorn JavaScript engine delivered in JDK8
– Utilizes new JVM level features
for performance

Avatar.js provides Node.js support on Nashorn
© 2015 IBM Corporation48
Nashorn and Avatar.js

Nashorn JavaScript engine delivered in JDK8
– Utilizes new JVM level features
for performance

Avatar.js provides Node.js support on Nashorn

Results of “Octane” JavaScript benchmark*:
– Node.js is 4.8x faster
– Avatar.js is >10x larger
* Using Java 8 pre-u20
© 2015 IBM Corporation49
Nashorn and Avatar.js

Nashorn JavaScript engine delivered in JDK8
– Utilizes new JVM level features
for performance

Avatar.js provides Node.js support on Nashorn

Results of “Octane” JavaScript benchmark*:
– Node.js is 4.8x faster
– Avatar.js is >10x larger
* Using Java 8 pre-u40
Feb 12th
, 2015: Avatar is “put on hold”
https://blogs.oracle.com/theaquarium/entry/project_avatar_update
© 2015 IBM Corporation50
JavaScript Performance
Under the Hood
© 2015 IBM Corporation51

Dynamic nature of JavaScript makes JIT optimizations more difficult
– Any variable could be a function or data
– Variables must be tested to determine how to handle handle them
– Variables can change, so what worked previously many not apply in future

Example: the use of '+':
• number + number → addition
• string involved? → concatenation
• objects involved? → convert to primitives then addition or
concatenation
• Functions involved? → ?
JIT Compilation
© 2015 IBM Corporation52
Execution flow for '+' operator
int a
int b
int c = a + b
Addition
Return Result
Java
© 2015 IBM Corporation53
Execution flow for '+' operator
int a
int b
int c = a + b
Addition
Return Result
var a
var b
var c = a + b
First Time?
NO
Use Previous
Types
Worked?
Return Result
YES
Type of A?
NO
YES
Type of B?String Concatenate
String
or
Number
Number Type of B? Number Addition
UndefinedString
Java JavaScript
© 2015 IBM Corporation54

Dynamically typed languages are harder to manage under the hood

They subsequently have lower runtime performance for computational tasks

Highlighted in TechEmpower benchmarks:
Dynamically vs Statically Types Languages
-70
-60
-50
-40
-30
-20
-10
0
Dynamic vs Statically Typed Language Performance
JSON
Single
Multi
Updates
Bestdynamiccomparedtobeststaticasbaseline
© 2015 IBM Corporation55
Writing
Node.js Code
© 2015 IBM Corporation56
Not So Easy: JavaScript Syntax Fun
> '5' – 3
2 // Weak typing, implicit conversion
> '5' + 3
'53' // ...but not consistent
> '5' – '4'
1 // String minus String = Integer??
> '5' + + '4'
54 // Multiple +'s are ok
> 'Hello' + 'World'
'HelloWorld' // Ok, that's expected
> 'Hello' + + 'World'
'HelloNaN' // ...but that isn't
© 2015 IBM Corporation57
Not So Easy: JavaScript Syntax Fun
> '5' – 3
2 // Weak typing, implicit conversion
> '5' + 3
'53' // ...but not consistent
> '5' – '4'
1 // String minus String = Integer??
> '5' + + '4'
54 // Multiple +'s are ok
> 'Hello' + 'World'
'HelloWorld' // Ok, that's expected
> 'Hello' + + 'World'
'HelloNaN' // ...but that isn't
© 2015 IBM Corporation58
Not So Easy: JavaScript Syntax Fun
> '5' – 3
2 // Weak typing, implicit conversion
> '5' + 3
'53' // ...but not consistent
> '5' – '4'
1 // String minus String = Integer??
> '5' + + '4'
54 // Multiple +'s are ok
> 'Hello' + 'World'
'HelloWorld' // Ok, that's expected
> 'Hello' + + 'World'
'HelloNaN' // ...but that isn't
© 2015 IBM Corporation59
Not So Easy: JavaScript Syntax Fun
> '5' – 3
2 // Weak typing, implicit conversion
> '5' + 3
'53' // ...but not consistent
> '5' – '4'
1 // String minus String = Integer??
> '5' + + '4'
54 // Multiple +'s are ok
> 'Hello' + 'World'
'HelloWorld' // Ok, that's expected
> 'Hello' + + 'World'
'HelloNaN' // ...but that isn't
© 2015 IBM Corporation60
Not So Easy: JavaScript Syntax Fun
> '5' – 3
2 // Weak typing, implicit conversion
> '5' + 3
'53' // ...but not consistent
> '5' – '4'
1 // String minus String = Integer??
> '5' + + '4'
54 // Multiple +'s are ok
> 'Hello' + 'World'
'HelloWorld' // Ok, that's expected
> 'Hello' + + 'World'
'HelloNaN' // ...but that isn't
© 2015 IBM Corporation61
Not So Easy: JavaScript Syntax Fun
> '5' – 3
2 // Weak typing, implicit conversion
> '5' + 3
'53' // ...but not consistent
> '5' – '4'
1 // String minus String = Integer??
> '5' + + '4'
54 // Multiple +'s are ok
> 'Hello' + 'World'
'HelloWorld' // Ok, that's expected
> 'Hello' + + 'World'
'HelloNaN' // ...but that isn't
© 2015 IBM Corporation62
Not So Easy: JavaScript Syntax Fun
> '5' – 3
2 // Weak typing, implicit conversion
> '5' + 3
'53' // ...but not consistent
> '5' – '4'
1 // String minus String = Integer??
> '5' + + '4'
54 // Multiple +'s are ok
> 'Hello' + 'World'
'HelloWorld' // Ok, that's expected
> 'Hello' + + 'World'
'HelloNaN' // ...but that isn't
© 2015 IBM Corporation63
Not So Easy: JavaScript Syntax Fun
> '5' – 3
2 // Weak typing, implicit conversion
> '5' + 3
'53' // ...but not consistent
> '5' – '4'
1 // String minus String = Integer??
> '5' + + '4'
54 // Multiple +'s are ok
> 'Hello' + 'World'
'HelloWorld' // Ok, that's expected
> 'Hello' + + 'World'
'HelloNaN' // ...but that isn't
© 2015 IBM Corporation64
Not So Easy: JavaScript Syntax Fun
> '5' – 3
2 // Weak typing, implicit conversion
> '5' + 3
'53' // ...but not consistent
> '5' – '4'
1 // String minus String = Integer??
> '5' + + '4'
54 // Multiple +'s are ok
> 'Hello' + 'World'
'HelloWorld' // Ok, that's expected
> 'Hello' + + 'World'
'HelloNaN' // ...but that isn't
© 2015 IBM Corporation65
Not So Easy: JavaScript Syntax Fun
> '5' – 3
2 // Weak typing, implicit conversion
> '5' + 3
'53' // ...but not consistent
> '5' – '4'
1 // String minus String = Integer??
> '5' + + '4'
54 // Multiple +'s are ok
> 'Hello' + 'World'
'HelloWorld' // Ok, that's expected
> 'Hello' + + 'World'
'HelloNaN' // ...but that isn't
© 2015 IBM Corporation66
Not So Easy: JavaScript Syntax Fun
> '5' – 3
2 // Weak typing, implicit conversion
> '5' + 3
'53' // ...but not consistent
> '5' – '4'
1 // String minus String = Integer??
> '5' + + '4'
54 // Multiple +'s are ok
> 'Hello' + 'World'
'HelloWorld' // Ok, that's expected
> 'Hello' + + 'World'
'HelloNaN' // ...but that isn't
© 2015 IBM Corporation67
Not So Easy: JavaScript Syntax Fun
> '5' – 3
2 // Weak typing, implicit conversion
> '5' + 3
'53' // ...but not consistent
> '5' – '4'
1 // String minus String = Integer??
> '5' + + '4'
54 // Multiple +'s are ok
> 'Hello' + 'World'
'HelloWorld' // Ok, that's expected
> 'Hello' + + 'World'
'HelloNaN' // ...but that isn't
© 2015 IBM Corporation68
Not So Easy: JavaScript Syntax Fun
> '5' + - '5'
'5-2' // I can just about see that works
> var x = 3
undefined
> '5' – x + x
5 // Ok, that makes sense
> var x = 3
undefined
> '5' + x - x
50 // What???
© 2015 IBM Corporation69
Not So Easy: JavaScript Syntax Fun
> '5' + - '5'
'5-5' // I can just about see that works
> var x = 3
undefined
> '5' – x + x
5 // Ok, that makes sense
> var x = 3
undefined
> '5' + x - x
50 // What???
© 2015 IBM Corporation70
Not So Easy: JavaScript Syntax Fun
> '5' + - '5'
'5-2' // I can just about see that works
> var x = 3
undefined
> '5' – x + x
5 // Ok, that makes sense
> var x = 3
undefined
> '5' + x - x
50 // What???
© 2015 IBM Corporation71
Not So Easy: JavaScript Syntax Fun
> '5' + - '5'
'5-2' // I can just about see that works
> var x = 3
undefined
> '5' – x + x
5 // Ok, that makes sense
> var x = 3
undefined
> '5' + x - x
50 // What???
© 2015 IBM Corporation72
Not So Easy: JavaScript Syntax Fun
> '5' + - '5'
'5-2' // I can just about see that works
> var x = 3
undefined
> '5' – x + x
5 // Ok, that makes sense
> var x = 3
undefined
> '5' + x - x
50 // What???
© 2015 IBM Corporation73
Not So Easy: JavaScript Syntax Fun
> '5' + - '5'
'5-2' // I can just about see that works
> var x = 3
undefined
> '5' – x + x
5 // Ok, that makes sense
> var x = 3
undefined
> '5' + x - x
50 // What???
© 2015 IBM Corporation74
(Micro)Service
Topologies
© 2015 IBM Corporation75
Enterprise Application Platform – Web Applications
Operations and Management
Admin Analytics
LoadBalancer
LoadBalancer
HTTP
Monitoring ScalingAnalytics Diagnostics
© 2015 IBM Corporation76
Questions
© 2015 IBM Corporation77

JAX London 2015: Java vs Nodejs

  • 1.
    © 2015 IBMCorporation1 Java vs Node.js For Enterprise Web Applications Chris Bailey IBM Runtime Technologies
  • 2.
    © 2015 IBMCorporation2 What Languages do you use? Java JavaScript Both 0 20 40 60 80 100 120 PercentageofAudience
  • 3.
    © 2015 IBMCorporation3 What Runtimes do you use? Server Java Applets JS in Browser Node.js Rhino Nashorn Avatar.js 0 20 40 60 80 100 120 PercentageofAudience
  • 4.
    © 2015 IBMCorporation4 4 Chris Bailey IBM Runtime Monitoring and Diagnostics Architect –14 years working with Java and JVM technologies –14 months working with Node.js and V8 Current Role(s): –IBM Java monitoring and diagnostics –Node Application Metrics project lead Node Foundation Member Benchmarking and Performance WGs JavaOne RockStar Author on performance and memory analysis baileyc@uk.ibm.com chrisbaileyibm cnbailey @seabaylea @Chris__Bailey
  • 5.
    © 2015 IBMCorporation5  Language Adoption  Deployment Modes  Code Development  Scalability  WebApplication Performance  Under the Hood  Enterprise Architectures Agenda
  • 6.
    © 2015 IBMCorporation6 6 Language Adoption
  • 7.
    © 2015 IBMCorporation7 GitHub Adoption: Java
  • 8.
    © 2015 IBMCorporation8 GitHub Adoption: JavaScript
  • 9.
    © 2015 IBMCorporation9 modulecounts.com
  • 10.
    © 2015 IBMCorporation10 StackOverflow User Survey
  • 11.
    © 2015 IBMCorporation11 Ratings based on the number of skilled engineers, courses and third party vendors. Tiobe Community Programming Index
  • 12.
    © 2015 IBMCorporation12 Indeed.com Job Trends: Java
  • 13.
    © 2015 IBMCorporation13 Indeed.com Job Trends: JavaScript
  • 14.
    © 2015 IBMCorporation14 Indeed.com Job Trends Java JavaScript
  • 15.
    © 2015 IBMCorporation15  JavaScript has a large developer base - #1 on GitHub with 45% more active repositories than Java - #1 on modulecounts.com with 29% more NPM modules than Maven - #1 used language by StackOverflow survey responders - #6 language on the Tiobe index  Java remains huge, particularly in the enterprise and on the server - #2 on GitHub with 52% more active repositories than the next language - #3 on modulecounts with 73.8% more modules than the next language - #2 language on the Tiobe index - #1 on indeed.com for developer jobs Language Adoption
  • 16.
    © 2015 IBMCorporation16 Deployment Modes
  • 17.
    © 2015 IBMCorporation17  JavaScript is ubiquitous in the browser - Supported in every browser - Integration with HTML and CSS  JavaScript is not affected by negative publicity.... Unless it is absolutely necessary to run Java in web browsers, disable it as described below, even after updating to 7u11. This will help mitigate other Java vulnerabilities that may be discovered in the future. This and previous Java vulnerabilities have been widely targeted by attackers, and new Java vulnerabilities are likely to be discovered. To defend against this and future Java vulnerabilities, consider disabling Java in web browsers… Usage in the browser
  • 18.
    © 2015 IBMCorporation18  Java has a long history on the server - JPE launched in 1998  Java has rich platform support: - Linux x86, Linux POWER, zLinux - Windows, Mac OS, Solaris, AIX, z/OS  JavaScript is a nascent language on the server - Limited platform support – although its growing - No API support to interact with the OS  Part of the browser security model - Frameworks like Node.js have changed that. Usage on the server
  • 19.
    © 2015 IBMCorporation19  Single Threaded Event based JavaScript framework – Uses non-blocking asynchronous I/O  Wraps the Chrome V8 JavaScript engine with I/O interfaces – Libuv provides interaction with OS/system  Designed to build scalable network applications – Suited for real time delivery of data to distributed client  Available on a wide set of platforms: - Linux on x86, ARM, Power and Z - Windows, Mac OS, Solaris, SmartOS and AIX libuvV8 Node Bindings Node Standard Library C JavaScript Server Side JavaScript: Node.js
  • 20.
    © 2015 IBMCorporation20 Anatomy of a Node.js application 1. package.json – Tracks properties, dependencies, version info, commands, etc. 2. JavaScript source files (i.e. app.js) •To install and execute: > npm install > node app.js 20
  • 21.
    © 2015 IBMCorporation21 Code Development
  • 22.
    © 2015 IBMCorporation22 var cluster = require('cluster'); var cpus = require('os').cpus().length; var http = require('http'); if (cluster.isMaster) { for (var i = 0; i < cpus; i++) { cluster.fork(); } cluster.on('death', function(worker) { console.log("Worker" + worker.pid + "died"); }); } else { http.createServer(function(request, response) { response.writeHead(200, {"Content-Type": "text/plain"}); response.write("Hello World!n"); response.end(); }).listen(8080); } HTTP Server Example
  • 23.
    © 2015 IBMCorporation23 var cluster = require('cluster'); var cpus = require('os').cpus().length; var http = require('http'); if (cluster.isMaster) { for (var i = 0; i < cpus; i++) { cluster.fork(); } cluster.on('death', function(worker) { console.log("Worker" + worker.pid + "died"); }); } else { http.createServer(function(request, response) { response.writeHead(200, {"Content-Type": "text/plain"}); response.write("Hello World!n"); response.end(); }).listen(8080); } Clustered HTTP Server Example
  • 24.
    © 2015 IBMCorporation24 Writing Node.js Code CodeVolumeRelativetoJava * based on benchmarksgame benchmarks Node.js Development Effort (lower is better) - 3x *or other transactional service
  • 25.
    © 2015 IBMCorporation25 Writing Node.js Code regex-dna Spectral-norm fannkuch-redux fasta k-nucleotide Binary-trees n-body Reverse-complement CodeVolumeRelativetoJava * based on benchmarksgame benchmarks Node.js Development Effort (lower is better) - 3x Avg 1/3rd less code *or other transactional service
  • 26.
    © 2015 IBMCorporation26 Writing Node.js Code regex-dna Spectral-norm fannkuch-redux fasta k-nucleotide Binary-trees n-body Reverse-complement CodeVolumeRelativetoJava * based on benchmarksgame benchmarks Node.js Development Effort (lower is better) - 3x ● Node.js has higher developer productivity Many applications developed with significantly less code ● Rich module system simplifies development Reduces need to develop custom code Avg 1/3rd less code *or other transactional service
  • 27.
    © 2015 IBMCorporation27 Scalability
  • 28.
    © 2015 IBMCorporation28  One thread (or process) per connection - Each thread waits on a response - Scalability determined by the number of threads  Each thread: - consumes memory - is relatively idle  Number of concurrent customers determined by number of depot workers  Additional customers wait in a queue with no response Typical approach to I/O
  • 29.
    © 2015 IBMCorporation29  One thread multiplexes for multiple requests - No waiting for a response - Handles return from I/O when notified  Scalability determined by: - CPU usage - “Back end” responsiveness  Number of concurrent customers determined by how fast the food Server can work  Or until the kitchen gets slammed Asycnhronous Non-Blocking I/O
  • 30.
    © 2015 IBMCorporation30  JavaScript is already event based in the browser - eg. onClick and onMouseOver events  First class functions and closures fit well with events - Easy to create and pass function callbacks - Easy to execute callbacks in the context of the event  Node.js execution is based on an event loop - Asynchronous I/O built in from the ground up  Node.js execution uses a single thread - No need to worry about locking or shared data - Most machines are now multi-CPU, so cluster capabilities are provided JavaScript and Asynchronous I/O
  • 31.
    © 2015 IBMCorporation31 var http = require('http'); var server = http.createServer(); server.listen(8080); server.on('request', function(request, response) { response.writeHead(200, {"Content-Type": "text/plain"}); response.write("Hello World!n"); response.end(); }); server.on('connection', function(socket) {}); server.on('close', function() {}); server.on('connect', function(socket) {}); server.on('upgrade', function(request, socket, head) {}); server.on('clientError', function(exception, socket) {}); Event based programming
  • 32.
    © 2015 IBMCorporation32 WebApp Performance
  • 33.
    © 2015 IBMCorporation33  JSON serialization of a newly instantiated object  Maps - Key of message - Value of Hello, World!  Example response: Results from TechEmpower.com Round 9 tests (2014-05-01) JSON Serialization
  • 34.
    © 2015 IBMCorporation34  JSON serialization of a newly instantiated object  Maps - Key of message - Value of Hello, World!  Example response: Results from TechEmpower.com Round 9 tests (2014-05-01) JSON Serialization JavaScript Java
  • 35.
    © 2015 IBMCorporation35 JavaScript WebApp Performance -100 -80 -60 -40 -20 0 20 40 -75 Node.js Performance (Compared to Java) JSON Serialization %ageofJavaPerformance
  • 36.
    © 2015 IBMCorporation36  Fetches single row from simple database table  Row serialized as JSON  Example response: Results from TechEmpower.com Round 9 tests (2014-05-01) Single Query
  • 37.
    © 2015 IBMCorporation37  Fetches single row from simple database table  Row serialized as JSON  Example response: Results from TechEmpower.com Round 9 tests (2014-05-01) Single Query JavaScript Java
  • 38.
    © 2015 IBMCorporation38 JavaScript WebApp Performance -100 -80 -60 -40 -20 0 20 40 -60.5 Node.js Performance (Compared to Java) JSON Serialization Single Query %ageofJavaPerformance
  • 39.
    © 2015 IBMCorporation39  Fetches multiple rows from a simple database table  Rows serialized as JSON  Example response: Results from TechEmpower.com Round 9 tests (2014-05-01) Multiple Queries
  • 40.
    © 2015 IBMCorporation40  Fetches multiple rows from a simple database table  Rows serialized as JSON  Example response: Results from TechEmpower.com Round 9 tests (2014-05-01) Multiple Queries JavaScript Java
  • 41.
    © 2015 IBMCorporation41 JavaScript WebApp Performance -100 -80 -60 -40 -20 0 20 40 -18 Node.js Performance (Compared to Java) JSON Serialization Single Query Multiple Queries %ageofJavaPerformance
  • 42.
    © 2015 IBMCorporation42  Fetches multiple rows from a simple database table  Converts rows to objects and modifies one attribute of each object  Updates each associated row and serializes as JSON  Example Response: Data Updates Results from TechEmpower.com Round 9 tests (2014-05-01)
  • 43.
    © 2015 IBMCorporation43  Fetches multiple rows from a simple database table  Converts rows to objects and modifies one attribute of each object  Updates each associated row and serializes as JSON  Example Response: Data Updates Results from TechEmpower.com Round 9 tests (2014-05-01) JavaScript Java
  • 44.
    © 2015 IBMCorporation44 JavaScript WebApp Performance -100 -80 -60 -40 -20 0 20 40 28 Node.js Performance (Compared to Java) JSON Serialization Single Query Multiple Queries Data Updates %ageofJavaPerformance
  • 45.
    © 2015 IBMCorporation45  Computation speed is (much) slower than Java  I/O speed is higher than Java JavaScript WebApp Performance -100 -80 -60 -40 -20 0 20 40 -75 -60.5 -18 28 Node.js Performance (Compared to Java) JSON Serialization Single Query Multiple Queries Data Updates %ageofJavaPerformance More Computation More I/O
  • 46.
    © 2015 IBMCorporation46 JavaScript on the JVM?
  • 47.
    © 2015 IBMCorporation47 Nashorn and Avatar.js  Nashorn JavaScript engine delivered in JDK8 – Utilizes new JVM level features for performance  Avatar.js provides Node.js support on Nashorn
  • 48.
    © 2015 IBMCorporation48 Nashorn and Avatar.js  Nashorn JavaScript engine delivered in JDK8 – Utilizes new JVM level features for performance  Avatar.js provides Node.js support on Nashorn  Results of “Octane” JavaScript benchmark*: – Node.js is 4.8x faster – Avatar.js is >10x larger * Using Java 8 pre-u20
  • 49.
    © 2015 IBMCorporation49 Nashorn and Avatar.js  Nashorn JavaScript engine delivered in JDK8 – Utilizes new JVM level features for performance  Avatar.js provides Node.js support on Nashorn  Results of “Octane” JavaScript benchmark*: – Node.js is 4.8x faster – Avatar.js is >10x larger * Using Java 8 pre-u40 Feb 12th , 2015: Avatar is “put on hold” https://blogs.oracle.com/theaquarium/entry/project_avatar_update
  • 50.
    © 2015 IBMCorporation50 JavaScript Performance Under the Hood
  • 51.
    © 2015 IBMCorporation51  Dynamic nature of JavaScript makes JIT optimizations more difficult – Any variable could be a function or data – Variables must be tested to determine how to handle handle them – Variables can change, so what worked previously many not apply in future  Example: the use of '+': • number + number → addition • string involved? → concatenation • objects involved? → convert to primitives then addition or concatenation • Functions involved? → ? JIT Compilation
  • 52.
    © 2015 IBMCorporation52 Execution flow for '+' operator int a int b int c = a + b Addition Return Result Java
  • 53.
    © 2015 IBMCorporation53 Execution flow for '+' operator int a int b int c = a + b Addition Return Result var a var b var c = a + b First Time? NO Use Previous Types Worked? Return Result YES Type of A? NO YES Type of B?String Concatenate String or Number Number Type of B? Number Addition UndefinedString Java JavaScript
  • 54.
    © 2015 IBMCorporation54  Dynamically typed languages are harder to manage under the hood  They subsequently have lower runtime performance for computational tasks  Highlighted in TechEmpower benchmarks: Dynamically vs Statically Types Languages -70 -60 -50 -40 -30 -20 -10 0 Dynamic vs Statically Typed Language Performance JSON Single Multi Updates Bestdynamiccomparedtobeststaticasbaseline
  • 55.
    © 2015 IBMCorporation55 Writing Node.js Code
  • 56.
    © 2015 IBMCorporation56 Not So Easy: JavaScript Syntax Fun > '5' – 3 2 // Weak typing, implicit conversion > '5' + 3 '53' // ...but not consistent > '5' – '4' 1 // String minus String = Integer?? > '5' + + '4' 54 // Multiple +'s are ok > 'Hello' + 'World' 'HelloWorld' // Ok, that's expected > 'Hello' + + 'World' 'HelloNaN' // ...but that isn't
  • 57.
    © 2015 IBMCorporation57 Not So Easy: JavaScript Syntax Fun > '5' – 3 2 // Weak typing, implicit conversion > '5' + 3 '53' // ...but not consistent > '5' – '4' 1 // String minus String = Integer?? > '5' + + '4' 54 // Multiple +'s are ok > 'Hello' + 'World' 'HelloWorld' // Ok, that's expected > 'Hello' + + 'World' 'HelloNaN' // ...but that isn't
  • 58.
    © 2015 IBMCorporation58 Not So Easy: JavaScript Syntax Fun > '5' – 3 2 // Weak typing, implicit conversion > '5' + 3 '53' // ...but not consistent > '5' – '4' 1 // String minus String = Integer?? > '5' + + '4' 54 // Multiple +'s are ok > 'Hello' + 'World' 'HelloWorld' // Ok, that's expected > 'Hello' + + 'World' 'HelloNaN' // ...but that isn't
  • 59.
    © 2015 IBMCorporation59 Not So Easy: JavaScript Syntax Fun > '5' – 3 2 // Weak typing, implicit conversion > '5' + 3 '53' // ...but not consistent > '5' – '4' 1 // String minus String = Integer?? > '5' + + '4' 54 // Multiple +'s are ok > 'Hello' + 'World' 'HelloWorld' // Ok, that's expected > 'Hello' + + 'World' 'HelloNaN' // ...but that isn't
  • 60.
    © 2015 IBMCorporation60 Not So Easy: JavaScript Syntax Fun > '5' – 3 2 // Weak typing, implicit conversion > '5' + 3 '53' // ...but not consistent > '5' – '4' 1 // String minus String = Integer?? > '5' + + '4' 54 // Multiple +'s are ok > 'Hello' + 'World' 'HelloWorld' // Ok, that's expected > 'Hello' + + 'World' 'HelloNaN' // ...but that isn't
  • 61.
    © 2015 IBMCorporation61 Not So Easy: JavaScript Syntax Fun > '5' – 3 2 // Weak typing, implicit conversion > '5' + 3 '53' // ...but not consistent > '5' – '4' 1 // String minus String = Integer?? > '5' + + '4' 54 // Multiple +'s are ok > 'Hello' + 'World' 'HelloWorld' // Ok, that's expected > 'Hello' + + 'World' 'HelloNaN' // ...but that isn't
  • 62.
    © 2015 IBMCorporation62 Not So Easy: JavaScript Syntax Fun > '5' – 3 2 // Weak typing, implicit conversion > '5' + 3 '53' // ...but not consistent > '5' – '4' 1 // String minus String = Integer?? > '5' + + '4' 54 // Multiple +'s are ok > 'Hello' + 'World' 'HelloWorld' // Ok, that's expected > 'Hello' + + 'World' 'HelloNaN' // ...but that isn't
  • 63.
    © 2015 IBMCorporation63 Not So Easy: JavaScript Syntax Fun > '5' – 3 2 // Weak typing, implicit conversion > '5' + 3 '53' // ...but not consistent > '5' – '4' 1 // String minus String = Integer?? > '5' + + '4' 54 // Multiple +'s are ok > 'Hello' + 'World' 'HelloWorld' // Ok, that's expected > 'Hello' + + 'World' 'HelloNaN' // ...but that isn't
  • 64.
    © 2015 IBMCorporation64 Not So Easy: JavaScript Syntax Fun > '5' – 3 2 // Weak typing, implicit conversion > '5' + 3 '53' // ...but not consistent > '5' – '4' 1 // String minus String = Integer?? > '5' + + '4' 54 // Multiple +'s are ok > 'Hello' + 'World' 'HelloWorld' // Ok, that's expected > 'Hello' + + 'World' 'HelloNaN' // ...but that isn't
  • 65.
    © 2015 IBMCorporation65 Not So Easy: JavaScript Syntax Fun > '5' – 3 2 // Weak typing, implicit conversion > '5' + 3 '53' // ...but not consistent > '5' – '4' 1 // String minus String = Integer?? > '5' + + '4' 54 // Multiple +'s are ok > 'Hello' + 'World' 'HelloWorld' // Ok, that's expected > 'Hello' + + 'World' 'HelloNaN' // ...but that isn't
  • 66.
    © 2015 IBMCorporation66 Not So Easy: JavaScript Syntax Fun > '5' – 3 2 // Weak typing, implicit conversion > '5' + 3 '53' // ...but not consistent > '5' – '4' 1 // String minus String = Integer?? > '5' + + '4' 54 // Multiple +'s are ok > 'Hello' + 'World' 'HelloWorld' // Ok, that's expected > 'Hello' + + 'World' 'HelloNaN' // ...but that isn't
  • 67.
    © 2015 IBMCorporation67 Not So Easy: JavaScript Syntax Fun > '5' – 3 2 // Weak typing, implicit conversion > '5' + 3 '53' // ...but not consistent > '5' – '4' 1 // String minus String = Integer?? > '5' + + '4' 54 // Multiple +'s are ok > 'Hello' + 'World' 'HelloWorld' // Ok, that's expected > 'Hello' + + 'World' 'HelloNaN' // ...but that isn't
  • 68.
    © 2015 IBMCorporation68 Not So Easy: JavaScript Syntax Fun > '5' + - '5' '5-2' // I can just about see that works > var x = 3 undefined > '5' – x + x 5 // Ok, that makes sense > var x = 3 undefined > '5' + x - x 50 // What???
  • 69.
    © 2015 IBMCorporation69 Not So Easy: JavaScript Syntax Fun > '5' + - '5' '5-5' // I can just about see that works > var x = 3 undefined > '5' – x + x 5 // Ok, that makes sense > var x = 3 undefined > '5' + x - x 50 // What???
  • 70.
    © 2015 IBMCorporation70 Not So Easy: JavaScript Syntax Fun > '5' + - '5' '5-2' // I can just about see that works > var x = 3 undefined > '5' – x + x 5 // Ok, that makes sense > var x = 3 undefined > '5' + x - x 50 // What???
  • 71.
    © 2015 IBMCorporation71 Not So Easy: JavaScript Syntax Fun > '5' + - '5' '5-2' // I can just about see that works > var x = 3 undefined > '5' – x + x 5 // Ok, that makes sense > var x = 3 undefined > '5' + x - x 50 // What???
  • 72.
    © 2015 IBMCorporation72 Not So Easy: JavaScript Syntax Fun > '5' + - '5' '5-2' // I can just about see that works > var x = 3 undefined > '5' – x + x 5 // Ok, that makes sense > var x = 3 undefined > '5' + x - x 50 // What???
  • 73.
    © 2015 IBMCorporation73 Not So Easy: JavaScript Syntax Fun > '5' + - '5' '5-2' // I can just about see that works > var x = 3 undefined > '5' – x + x 5 // Ok, that makes sense > var x = 3 undefined > '5' + x - x 50 // What???
  • 74.
    © 2015 IBMCorporation74 (Micro)Service Topologies
  • 75.
    © 2015 IBMCorporation75 Enterprise Application Platform – Web Applications Operations and Management Admin Analytics LoadBalancer LoadBalancer HTTP Monitoring ScalingAnalytics Diagnostics
  • 76.
    © 2015 IBMCorporation76 Questions
  • 77.
    © 2015 IBMCorporation77