SlideShare a Scribd company logo
node.js
ry@tinyclouds.org
November 8, 2009
node.js in brief:
Server-side Javascript
Built on Google’s V8
Evented, non-blocking I/O. Similar to
EventMachine or Twisted.
CommonJS module system.
8000 lines of C/C++, 2000 lines of
Javascript, 14 contributors.
I/O needs to be done differently.
Many web applications have code
like this:
var result =
db.query("select * from T");
// use result
What is the software doing while it
queries the database?
In many cases, just waiting for the
response.
I/O latency
L1: 3 cycles
L2: 14 cycles
RAM: 250 cycles
DISK: 41,000,000 cycles
NETWORK: 240,000,000 cycles
Better software can multitask.
Other threads of execution can run
while waiting.
Is that the best that can be done?
A look at Apache and NGINX.
Apache vs NGINX
concurrency × reqs/sec
http://blog.webfaction.com/a-little-holiday-present
Apache vs NGINX
concurrency × memory
http://blog.webfaction.com/a-little-holiday-present
Apache vs NGINX
The difference?
Apache uses one thread per
connection.
NGINX doesn’t use threads. It uses
an event loop.
Context switching is not free
Execution stacks take up memory
For massive concurrency, cannot
use an OS thread for each
connection.
Green threads or coroutines can
improve the situation dramatically
BUT there is still machinery involved
to create the illusion of holding
execution on I/O.
Threaded concurrency is a leaky
abstraction.
Code like this
var result = db.query("select..");
// use result
either blocks the entire process or
implies multiple execution stacks.
But a line of code like this
db.query("select..", function (result) {
// use result
});
allows the program to return to the
event loop immediately.
No machinery required.
db.query("select..", function (result) {
// use result
});
This is how I/O should be done.
So why isn’t everyone using event
loops, callbacks, and non-blocking
I/O?
For reasons both cultural and
infrastructural.
Cultural Bias
We’re taught I/O with this:
1 puts("Enter your name: ");
2 var name = gets();
3 puts("Name: " + name);
We’re taught to demand input and do
nothing until we have it.
Cultural Bias
Code like
1 puts("Enter your name: ");
2 gets(function (name) {
3 puts("Name: " + name);
4 });
is rejected as too complicated.
Missing Infrastructure
So why isn’t everyone using event
loops?
Single threaded event loops require
I/O to be non-blocking
Most libraries are not.
Missing Infrastructure
POSIX async file I/O not available.
Man pages don’t state if a function will access the
disk. (e.g getpwuid())
No closures or anonymous functions in C; makes
callbacks difficult.
Database libraries (e.g. libmysql client) do not
provide support for asynchronous queries
Asynchronous DNS resolution not standard on most
systems.
Too Much Infrastructure
EventMachine, Twisted, AnyEvent
provide very good event loop
platforms.
Easy to create efficent servers.
But users are confused how to
combine with other available
libraries.
Too Much Infrastructure
Users still require expert knowledge
of event loops, non-blocking I/O.
Javascript designed specifically to be
used with an event loop:
Anonymous functions, closures.
Only one callback at a time.
I/O through DOM event callbacks.
The culture of Javascript is already
geared towards evented
programming.
This is the node.js project:
To provide a purely evented,
non-blocking infrastructure to
script highly concurrent programs.
Design Goals
No function should direct perform I/O.
To receive info from disk, network, or
another process there must be a
callback.
Design Goals
Low-level.
Stream everything; never force the
buffering of data.
Do not remove functionality present
at the POSIX layer. For example,
support half-closed TCP
connections.
Design Goals
Have built-in support for the most
important protocols:
TCP, DNS, HTTP
Design Goals
Support many HTTP features.
Chunked requests and responses.
Keep-alive.
Hang requests for comet
applications.
Design Goals
The API should be both familiar to
client-side JS programmers and
old school UNIX hackers.
Be platform independent.
Usage and
Examples
(using node 0.1.16)
Download, configure, compile, and
make install it.
http://nodejs.org/
No dependencies other than Python
for the build system. V8 is included in
the distribution.
1 var sys = require("sys");
2
3 setTimeout(function () {
4 sys.puts("world");
5 }, 2000);
6 sys.puts("hello");
A program which prints “hello”, waits
2 seconds, outputs “world”, and then
exits.
1 var sys = require("sys");
2
3 setTimeout(function () {
4 sys.puts("world");
5 }, 2000);
6 sys.puts("hello");
Node exits automatically when there
is nothing else to do.
% node hello_world.js
hello
2 seconds later...
% node hello_world.js
hello
world
%
Change the “hello world” program to
loop forever, but print an exit
message when the user kills it.
We will use the special object
process and the "SIGINT"
signal.
1 puts = require("sys").puts;
2
3 setInterval(function () {
4 puts("hello");
5 }, 500);
6
7 process.addListener("SIGINT",
8 function () {
9 puts("good bye");
10 process.exit(0)
11 });
process.addListener("SIGINT", ...);
The process object emits an event
when it receives a signal. Like in the
DOM, you need only add a listener to
catch them.
Also:
process.pid
process.ARGV
process.ENV
process.cwd()
process.memoryUsage()
Like process, many other objects
in Node emit events.
A TCP server emits a
"connection" event each time
someone connects.
An HTTP upload emits a "body"
event on each packet.
All objects which emit events are are
instances of
process.EventEmitter.
Write a program which:
starts a TCP server on port 8000
send the peer a message
close the connection
1 var tcp = require("tcp");
2
3 var s = tcp.createServer();
4 s.addListener("connection",
5 function (c) {
6 c.send("hello!");
7 c.close();
8 });
9
10 s.listen(8000);
% node server.js &
[1] 9120
% telnet localhost 8000
Trying 127.0.0.1...
Connected to localhost.
Escape character is ’ˆ]’.
hello!
Connection closed by foreign host.
%
The "connection" listener can
be provided as the first argument to
tcp.createServer(), so the
program can be simplified:
1 var tcp = require("tcp");
2 tcp.createServer(function (c) {
3 c.send("hello!n");
4 c.close();
5 }).listen(8000);
File I/O is non-blocking too.
(Something typically hard to do.)
As an example, a program that
outputs the last time /etc/passwd
was modified:
1 var stat = require("posix").stat,
2 puts = require("sys").puts;
3
4 var promise = stat("/etc/passwd");
5
6 promise.addCallback(function (s) {
7 puts("modified: " + s.mtime);
8 });
A promise is a kind of
EventEmitter which emits either
"success" or "error". (But not
both.)
All file operations return a promise.
promise.addCallback(cb)
is just API sugar for
promise.addListener("success", cb)
Simple HTTP Server:
1 var http = require("http");
2
3 http.createServer(function (req,res) {
4 res.sendHeader(200,
5 {"Content-Type": "text/plain"});
6 res.sendBody("Hellorn");
7 res.sendBody("Worldrn");
8 res.finish();
9 }).listen(8000);
% node http_server.js &
[4] 27355
% curl -i http://localhost:8000/
HTTP/1.1 200 OK
Content-Type: text/plain
Connection: keep-alive
Transfer-Encoding: chunked
Hello
World
%
Streaming HTTP Server:
1 var http = require("http");
2 http.createServer(function (req,res) {
3 res.sendHeader(200,
4 {"Content-Type": "text/plain"});
5
6 res.sendBody("Hel");
7 res.sendBody("lorn");
8
9 setTimeout(function () {
10 res.sendBody("Worldrn");
11 res.finish();
12 }, 2000);
13 }).listen(8000);
% node http_server2.js &
[4] 27355
% curl http://localhost:8000/
Hello
Two seconds later...
% node http_server2.js &
[4] 27355
% curl http://localhost:8000/
Hello
World
%
1 var sys = require("sys");
2 sys.exec("ls -l /")
3 .addCallback(function (output) {
4 sys.puts(output);
5 });
Programs can be run with
sys.exec()
But Node never forces buffering
∃ a lower-level facility to stream data
through the STDIO of the child
procresses.
Simple IPC.
1 var puts = require("sys").puts;
2
3 var cat =
4 process.createChildProcess("cat");
5
6 cat.addListener("output",
7 function (data) {
8 if (data) sys.puts(data);
9 });
10
11 cat.write("hello ");
12 cat.write("worldn");
13 cat.close();
Demo / Experiment
An IRC Daemon written in javascript.
irc.nodejs.org
#node.js
Source code:
http://tinyurl.com/ircd-js
http://gist.github.com/a3d0bbbff196af633995
Internal Design
V8 (Google)
libev event loop library (Marc Lehmann)
libeio thread pool library (Marc Lehmann)
http-parser a ragel HTTP parser (Me)
evcom stream socket library on top of libev
(Me)
udns non-blocking DNS resolver (Michael
Tokarev)
Blocking (or possibly blocking)
system calls are executed in the
thread pool.
Signal handlers and thread pool
callbacks are marshaled back into
the main thread via a pipe.
% node myscript.js < hugefile.txt
STDIN_FILENO will refer to a file.
Cannot select() on files;
read() will block.
Solution: Start a pipe, and a
“pumping thread”.
Pump data from blocking fd into pipe.
Main thread can poll for data on the
pipe.
(See deps/coupling if you’re interested)
Future
Fix API warts.
More modularity; break Node into shared objects.
Include libraries for common databases in
distribution.
Improve performance.
TLS support
Web Worker-like API. (Probably using
ChildProcess)
Future
Version 0.2 in late December or
January.
Core API will be frozen.
Questions...?
http://nodejs.org/
ry@tinyclouds.org

More Related Content

What's hot

Introduction to Node js
Introduction to Node jsIntroduction to Node js
Introduction to Node jsAkshay Mathur
 
Node.js Express
Node.js  ExpressNode.js  Express
Node.js ExpressEyal Vardi
 
Introduction to ajax
Introduction to ajaxIntroduction to ajax
Introduction to ajaxNir Elbaz
 
Java Persistence API (JPA) Step By Step
Java Persistence API (JPA) Step By StepJava Persistence API (JPA) Step By Step
Java Persistence API (JPA) Step By StepGuo Albert
 
Java EE Introduction
Java EE IntroductionJava EE Introduction
Java EE Introductionejlp12
 
Use Node.js to create a REST API
Use Node.js to create a REST APIUse Node.js to create a REST API
Use Node.js to create a REST APIFabien Vauchelles
 
Nodejs Explained with Examples
Nodejs Explained with ExamplesNodejs Explained with Examples
Nodejs Explained with ExamplesGabriele Lana
 
Document Object Model
Document Object ModelDocument Object Model
Document Object Modelchomas kandar
 
Angular - Chapter 1 - Introduction
 Angular - Chapter 1 - Introduction Angular - Chapter 1 - Introduction
Angular - Chapter 1 - IntroductionWebStackAcademy
 
Building an API with Django and Django REST Framework
Building an API with Django and Django REST FrameworkBuilding an API with Django and Django REST Framework
Building an API with Django and Django REST FrameworkChristopher Foresman
 
Node.js File system & Streams
Node.js File system & StreamsNode.js File system & Streams
Node.js File system & StreamsEyal Vardi
 
What Is Express JS?
What Is Express JS?What Is Express JS?
What Is Express JS?Simplilearn
 

What's hot (20)

Introduction to Node js
Introduction to Node jsIntroduction to Node js
Introduction to Node js
 
Basics of JavaScript
Basics of JavaScriptBasics of JavaScript
Basics of JavaScript
 
Node.js Express
Node.js  ExpressNode.js  Express
Node.js Express
 
Introduction Node.js
Introduction Node.jsIntroduction Node.js
Introduction Node.js
 
Jdbc ppt
Jdbc pptJdbc ppt
Jdbc ppt
 
Introduction to ajax
Introduction to ajaxIntroduction to ajax
Introduction to ajax
 
Java Persistence API (JPA) Step By Step
Java Persistence API (JPA) Step By StepJava Persistence API (JPA) Step By Step
Java Persistence API (JPA) Step By Step
 
Express js
Express jsExpress js
Express js
 
Introduction à JPA (Java Persistence API )
Introduction à JPA  (Java Persistence API )Introduction à JPA  (Java Persistence API )
Introduction à JPA (Java Persistence API )
 
Dom
DomDom
Dom
 
RESTful Web Services
RESTful Web ServicesRESTful Web Services
RESTful Web Services
 
Java EE Introduction
Java EE IntroductionJava EE Introduction
Java EE Introduction
 
Use Node.js to create a REST API
Use Node.js to create a REST APIUse Node.js to create a REST API
Use Node.js to create a REST API
 
Nodejs Explained with Examples
Nodejs Explained with ExamplesNodejs Explained with Examples
Nodejs Explained with Examples
 
AngularJS
AngularJSAngularJS
AngularJS
 
Document Object Model
Document Object ModelDocument Object Model
Document Object Model
 
Angular - Chapter 1 - Introduction
 Angular - Chapter 1 - Introduction Angular - Chapter 1 - Introduction
Angular - Chapter 1 - Introduction
 
Building an API with Django and Django REST Framework
Building an API with Django and Django REST FrameworkBuilding an API with Django and Django REST Framework
Building an API with Django and Django REST Framework
 
Node.js File system & Streams
Node.js File system & StreamsNode.js File system & Streams
Node.js File system & Streams
 
What Is Express JS?
What Is Express JS?What Is Express JS?
What Is Express JS?
 

Viewers also liked

Presentationrailways 120309144032-phpapp02
Presentationrailways 120309144032-phpapp02Presentationrailways 120309144032-phpapp02
Presentationrailways 120309144032-phpapp02Durga Rai
 
Music vidio media
Music vidio media Music vidio media
Music vidio media Wilko18
 
Media music video power
Media music video powerMedia music video power
Media music video powerWilko18
 
Sample Results 2014- RMCN
Sample Results 2014- RMCNSample Results 2014- RMCN
Sample Results 2014- RMCNdavidallsup3
 
Media music video drake
Media music video drake Media music video drake
Media music video drake Wilko18
 
11.1renewableandnonrenewableresources
11.1renewableandnonrenewableresources11.1renewableandnonrenewableresources
11.1renewableandnonrenewableresourcesDurga Rai
 
Business research (1)
Business research (1)Business research (1)
Business research (1)007donmj
 
Ricochet: Context and Complementarity-Aware, Ontology-based POIs Recommender ...
Ricochet: Context and Complementarity-Aware, Ontology-based POIs Recommender ...Ricochet: Context and Complementarity-Aware, Ontology-based POIs Recommender ...
Ricochet: Context and Complementarity-Aware, Ontology-based POIs Recommender ...Chun LU
 
Bds.en.13803.1.2010
Bds.en.13803.1.2010Bds.en.13803.1.2010
Bds.en.13803.1.2010Durga Rai
 

Viewers also liked (13)

Presentationrailways 120309144032-phpapp02
Presentationrailways 120309144032-phpapp02Presentationrailways 120309144032-phpapp02
Presentationrailways 120309144032-phpapp02
 
Music vidio media
Music vidio media Music vidio media
Music vidio media
 
Media music video power
Media music video powerMedia music video power
Media music video power
 
Sample Results 2014- RMCN
Sample Results 2014- RMCNSample Results 2014- RMCN
Sample Results 2014- RMCN
 
Media music video drake
Media music video drake Media music video drake
Media music video drake
 
osipov yury 24.08.2015
osipov yury 24.08.2015osipov yury 24.08.2015
osipov yury 24.08.2015
 
11.1renewableandnonrenewableresources
11.1renewableandnonrenewableresources11.1renewableandnonrenewableresources
11.1renewableandnonrenewableresources
 
15 a lei moral
15   a lei moral15   a lei moral
15 a lei moral
 
Drones presentation
Drones presentationDrones presentation
Drones presentation
 
Business research (1)
Business research (1)Business research (1)
Business research (1)
 
Ricochet: Context and Complementarity-Aware, Ontology-based POIs Recommender ...
Ricochet: Context and Complementarity-Aware, Ontology-based POIs Recommender ...Ricochet: Context and Complementarity-Aware, Ontology-based POIs Recommender ...
Ricochet: Context and Complementarity-Aware, Ontology-based POIs Recommender ...
 
20 O sabado
20 O sabado 20 O sabado
20 O sabado
 
Bds.en.13803.1.2010
Bds.en.13803.1.2010Bds.en.13803.1.2010
Bds.en.13803.1.2010
 

Similar to Original slides from Ryan Dahl's NodeJs intro talk

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
 
soft-shake.ch - Hands on Node.js
soft-shake.ch - Hands on Node.jssoft-shake.ch - Hands on Node.js
soft-shake.ch - Hands on Node.jssoft-shake.ch
 
Writing robust Node.js applications
Writing robust Node.js applicationsWriting robust Node.js applications
Writing robust Node.js applicationsTom Croucher
 
Node.js for Rubists
Node.js for RubistsNode.js for Rubists
Node.js for RubistsSagiv Ofek
 
Nodejs and WebSockets
Nodejs and WebSocketsNodejs and WebSockets
Nodejs and WebSocketsGonzalo Ayuso
 
introduction to node.js
introduction to node.jsintroduction to node.js
introduction to node.jsorkaplan
 
Node.js - async for the rest of us.
Node.js - async for the rest of us.Node.js - async for the rest of us.
Node.js - async for the rest of us.Mike Brevoort
 
Introduction to Node.js
Introduction to Node.jsIntroduction to Node.js
Introduction to Node.jsRichard Lee
 
GeekCampSG - Nodejs , Websockets and Realtime Web
GeekCampSG - Nodejs , Websockets and Realtime WebGeekCampSG - Nodejs , Websockets and Realtime Web
GeekCampSG - Nodejs , Websockets and Realtime WebBhagaban Behera
 
Building and Scaling Node.js Applications
Building and Scaling Node.js ApplicationsBuilding and Scaling Node.js Applications
Building and Scaling Node.js ApplicationsOhad Kravchick
 
Introduce about Nodejs - duyetdev.com
Introduce about Nodejs - duyetdev.comIntroduce about Nodejs - duyetdev.com
Introduce about Nodejs - duyetdev.comVan-Duyet Le
 
Node js presentation
Node js presentationNode js presentation
Node js presentationmartincabrera
 
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
 
Nodejs a-practical-introduction-oredev
Nodejs a-practical-introduction-oredevNodejs a-practical-introduction-oredev
Nodejs a-practical-introduction-oredevFelix Geisendörfer
 
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
 
Why Nodejs Guilin Shanghai
Why Nodejs Guilin ShanghaiWhy Nodejs Guilin Shanghai
Why Nodejs Guilin ShanghaiJackson Tian
 
Why Node.js
Why Node.jsWhy Node.js
Why Node.jsguileen
 

Similar to Original slides from Ryan Dahl's NodeJs intro talk (20)

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
 
soft-shake.ch - Hands on Node.js
soft-shake.ch - Hands on Node.jssoft-shake.ch - Hands on Node.js
soft-shake.ch - Hands on Node.js
 
Writing robust Node.js applications
Writing robust Node.js applicationsWriting robust Node.js applications
Writing robust Node.js applications
 
Node.js for Rubists
Node.js for RubistsNode.js for Rubists
Node.js for Rubists
 
NodeJS
NodeJSNodeJS
NodeJS
 
Nodejs and WebSockets
Nodejs and WebSocketsNodejs and WebSockets
Nodejs and WebSockets
 
introduction to node.js
introduction to node.jsintroduction to node.js
introduction to node.js
 
Node.js - async for the rest of us.
Node.js - async for the rest of us.Node.js - async for the rest of us.
Node.js - async for the rest of us.
 
Introduction to Node.js
Introduction to Node.jsIntroduction to Node.js
Introduction to Node.js
 
GeekCampSG - Nodejs , Websockets and Realtime Web
GeekCampSG - Nodejs , Websockets and Realtime WebGeekCampSG - Nodejs , Websockets and Realtime Web
GeekCampSG - Nodejs , Websockets and Realtime Web
 
Building and Scaling Node.js Applications
Building and Scaling Node.js ApplicationsBuilding and Scaling Node.js Applications
Building and Scaling Node.js Applications
 
Node.js
Node.jsNode.js
Node.js
 
Introduce about Nodejs - duyetdev.com
Introduce about Nodejs - duyetdev.comIntroduce about Nodejs - duyetdev.com
Introduce about Nodejs - duyetdev.com
 
Node js presentation
Node js presentationNode js presentation
Node js presentation
 
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...
 
Nodejs a-practical-introduction-oredev
Nodejs a-practical-introduction-oredevNodejs a-practical-introduction-oredev
Nodejs a-practical-introduction-oredev
 
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
 
node.js - Fast event based web application development
node.js - Fast event based web application developmentnode.js - Fast event based web application development
node.js - Fast event based web application development
 
Why Nodejs Guilin Shanghai
Why Nodejs Guilin ShanghaiWhy Nodejs Guilin Shanghai
Why Nodejs Guilin Shanghai
 
Why Node.js
Why Node.jsWhy Node.js
Why Node.js
 

Recently uploaded

Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesThousandEyes
 
ODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User GroupODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User GroupCatarinaPereira64715
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...Product School
 
Exploring UiPath Orchestrator API: updates and limits in 2024 🚀
Exploring UiPath Orchestrator API: updates and limits in 2024 🚀Exploring UiPath Orchestrator API: updates and limits in 2024 🚀
Exploring UiPath Orchestrator API: updates and limits in 2024 🚀DianaGray10
 
IoT Analytics Company Presentation May 2024
IoT Analytics Company Presentation May 2024IoT Analytics Company Presentation May 2024
IoT Analytics Company Presentation May 2024IoTAnalytics
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...Elena Simperl
 
Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...
Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...
Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...CzechDreamin
 
Demystifying gRPC in .Net by John Staveley
Demystifying gRPC in .Net by John StaveleyDemystifying gRPC in .Net by John Staveley
Demystifying gRPC in .Net by John StaveleyJohn Staveley
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...Product School
 
SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...
SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...
SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...CzechDreamin
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersSafe Software
 
10 Differences between Sales Cloud and CPQ, Blanka Doktorová
10 Differences between Sales Cloud and CPQ, Blanka Doktorová10 Differences between Sales Cloud and CPQ, Blanka Doktorová
10 Differences between Sales Cloud and CPQ, Blanka DoktorováCzechDreamin
 
AI revolution and Salesforce, Jiří Karpíšek
AI revolution and Salesforce, Jiří KarpíšekAI revolution and Salesforce, Jiří Karpíšek
AI revolution and Salesforce, Jiří KarpíšekCzechDreamin
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Product School
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Thierry Lestable
 
Powerful Start- the Key to Project Success, Barbara Laskowska
Powerful Start- the Key to Project Success, Barbara LaskowskaPowerful Start- the Key to Project Success, Barbara Laskowska
Powerful Start- the Key to Project Success, Barbara LaskowskaCzechDreamin
 
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptxIOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptxAbida Shariff
 
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo DiehlFuture Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo DiehlPeter Udo Diehl
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backElena Simperl
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfCheryl Hung
 

Recently uploaded (20)

Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
 
ODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User GroupODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User Group
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
 
Exploring UiPath Orchestrator API: updates and limits in 2024 🚀
Exploring UiPath Orchestrator API: updates and limits in 2024 🚀Exploring UiPath Orchestrator API: updates and limits in 2024 🚀
Exploring UiPath Orchestrator API: updates and limits in 2024 🚀
 
IoT Analytics Company Presentation May 2024
IoT Analytics Company Presentation May 2024IoT Analytics Company Presentation May 2024
IoT Analytics Company Presentation May 2024
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
 
Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...
Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...
Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...
 
Demystifying gRPC in .Net by John Staveley
Demystifying gRPC in .Net by John StaveleyDemystifying gRPC in .Net by John Staveley
Demystifying gRPC in .Net by John Staveley
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 
SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...
SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...
SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
 
10 Differences between Sales Cloud and CPQ, Blanka Doktorová
10 Differences between Sales Cloud and CPQ, Blanka Doktorová10 Differences between Sales Cloud and CPQ, Blanka Doktorová
10 Differences between Sales Cloud and CPQ, Blanka Doktorová
 
AI revolution and Salesforce, Jiří Karpíšek
AI revolution and Salesforce, Jiří KarpíšekAI revolution and Salesforce, Jiří Karpíšek
AI revolution and Salesforce, Jiří Karpíšek
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
 
Powerful Start- the Key to Project Success, Barbara Laskowska
Powerful Start- the Key to Project Success, Barbara LaskowskaPowerful Start- the Key to Project Success, Barbara Laskowska
Powerful Start- the Key to Project Success, Barbara Laskowska
 
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptxIOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
 
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo DiehlFuture Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
 

Original slides from Ryan Dahl's NodeJs intro talk

  • 2. node.js in brief: Server-side Javascript Built on Google’s V8 Evented, non-blocking I/O. Similar to EventMachine or Twisted. CommonJS module system. 8000 lines of C/C++, 2000 lines of Javascript, 14 contributors.
  • 3. I/O needs to be done differently.
  • 4. Many web applications have code like this: var result = db.query("select * from T"); // use result What is the software doing while it queries the database?
  • 5. In many cases, just waiting for the response.
  • 6. I/O latency L1: 3 cycles L2: 14 cycles RAM: 250 cycles DISK: 41,000,000 cycles NETWORK: 240,000,000 cycles
  • 7. Better software can multitask. Other threads of execution can run while waiting.
  • 8. Is that the best that can be done? A look at Apache and NGINX.
  • 9. Apache vs NGINX concurrency × reqs/sec http://blog.webfaction.com/a-little-holiday-present
  • 10. Apache vs NGINX concurrency × memory http://blog.webfaction.com/a-little-holiday-present
  • 11. Apache vs NGINX The difference? Apache uses one thread per connection. NGINX doesn’t use threads. It uses an event loop.
  • 12. Context switching is not free Execution stacks take up memory For massive concurrency, cannot use an OS thread for each connection.
  • 13. Green threads or coroutines can improve the situation dramatically BUT there is still machinery involved to create the illusion of holding execution on I/O.
  • 14. Threaded concurrency is a leaky abstraction.
  • 15. Code like this var result = db.query("select.."); // use result either blocks the entire process or implies multiple execution stacks.
  • 16. But a line of code like this db.query("select..", function (result) { // use result }); allows the program to return to the event loop immediately. No machinery required.
  • 17. db.query("select..", function (result) { // use result }); This is how I/O should be done.
  • 18. So why isn’t everyone using event loops, callbacks, and non-blocking I/O? For reasons both cultural and infrastructural.
  • 19. Cultural Bias We’re taught I/O with this: 1 puts("Enter your name: "); 2 var name = gets(); 3 puts("Name: " + name); We’re taught to demand input and do nothing until we have it.
  • 20. Cultural Bias Code like 1 puts("Enter your name: "); 2 gets(function (name) { 3 puts("Name: " + name); 4 }); is rejected as too complicated.
  • 21. Missing Infrastructure So why isn’t everyone using event loops? Single threaded event loops require I/O to be non-blocking Most libraries are not.
  • 22. Missing Infrastructure POSIX async file I/O not available. Man pages don’t state if a function will access the disk. (e.g getpwuid()) No closures or anonymous functions in C; makes callbacks difficult. Database libraries (e.g. libmysql client) do not provide support for asynchronous queries Asynchronous DNS resolution not standard on most systems.
  • 23. Too Much Infrastructure EventMachine, Twisted, AnyEvent provide very good event loop platforms. Easy to create efficent servers. But users are confused how to combine with other available libraries.
  • 24. Too Much Infrastructure Users still require expert knowledge of event loops, non-blocking I/O.
  • 25. Javascript designed specifically to be used with an event loop: Anonymous functions, closures. Only one callback at a time. I/O through DOM event callbacks.
  • 26. The culture of Javascript is already geared towards evented programming.
  • 27. This is the node.js project: To provide a purely evented, non-blocking infrastructure to script highly concurrent programs.
  • 28. Design Goals No function should direct perform I/O. To receive info from disk, network, or another process there must be a callback.
  • 29. Design Goals Low-level. Stream everything; never force the buffering of data. Do not remove functionality present at the POSIX layer. For example, support half-closed TCP connections.
  • 30. Design Goals Have built-in support for the most important protocols: TCP, DNS, HTTP
  • 31. Design Goals Support many HTTP features. Chunked requests and responses. Keep-alive. Hang requests for comet applications.
  • 32. Design Goals The API should be both familiar to client-side JS programmers and old school UNIX hackers. Be platform independent.
  • 34. Download, configure, compile, and make install it. http://nodejs.org/ No dependencies other than Python for the build system. V8 is included in the distribution.
  • 35. 1 var sys = require("sys"); 2 3 setTimeout(function () { 4 sys.puts("world"); 5 }, 2000); 6 sys.puts("hello"); A program which prints “hello”, waits 2 seconds, outputs “world”, and then exits.
  • 36. 1 var sys = require("sys"); 2 3 setTimeout(function () { 4 sys.puts("world"); 5 }, 2000); 6 sys.puts("hello"); Node exits automatically when there is nothing else to do.
  • 37. % node hello_world.js hello 2 seconds later... % node hello_world.js hello world %
  • 38. Change the “hello world” program to loop forever, but print an exit message when the user kills it. We will use the special object process and the "SIGINT" signal.
  • 39. 1 puts = require("sys").puts; 2 3 setInterval(function () { 4 puts("hello"); 5 }, 500); 6 7 process.addListener("SIGINT", 8 function () { 9 puts("good bye"); 10 process.exit(0) 11 });
  • 40. process.addListener("SIGINT", ...); The process object emits an event when it receives a signal. Like in the DOM, you need only add a listener to catch them.
  • 42. Like process, many other objects in Node emit events.
  • 43. A TCP server emits a "connection" event each time someone connects. An HTTP upload emits a "body" event on each packet.
  • 44. All objects which emit events are are instances of process.EventEmitter.
  • 45. Write a program which: starts a TCP server on port 8000 send the peer a message close the connection
  • 46. 1 var tcp = require("tcp"); 2 3 var s = tcp.createServer(); 4 s.addListener("connection", 5 function (c) { 6 c.send("hello!"); 7 c.close(); 8 }); 9 10 s.listen(8000);
  • 47. % node server.js & [1] 9120 % telnet localhost 8000 Trying 127.0.0.1... Connected to localhost. Escape character is ’ˆ]’. hello! Connection closed by foreign host. %
  • 48. The "connection" listener can be provided as the first argument to tcp.createServer(), so the program can be simplified:
  • 49. 1 var tcp = require("tcp"); 2 tcp.createServer(function (c) { 3 c.send("hello!n"); 4 c.close(); 5 }).listen(8000);
  • 50. File I/O is non-blocking too. (Something typically hard to do.)
  • 51. As an example, a program that outputs the last time /etc/passwd was modified: 1 var stat = require("posix").stat, 2 puts = require("sys").puts; 3 4 var promise = stat("/etc/passwd"); 5 6 promise.addCallback(function (s) { 7 puts("modified: " + s.mtime); 8 });
  • 52. A promise is a kind of EventEmitter which emits either "success" or "error". (But not both.) All file operations return a promise.
  • 53. promise.addCallback(cb) is just API sugar for promise.addListener("success", cb)
  • 54. Simple HTTP Server: 1 var http = require("http"); 2 3 http.createServer(function (req,res) { 4 res.sendHeader(200, 5 {"Content-Type": "text/plain"}); 6 res.sendBody("Hellorn"); 7 res.sendBody("Worldrn"); 8 res.finish(); 9 }).listen(8000);
  • 55. % node http_server.js & [4] 27355 % curl -i http://localhost:8000/ HTTP/1.1 200 OK Content-Type: text/plain Connection: keep-alive Transfer-Encoding: chunked Hello World %
  • 56. Streaming HTTP Server: 1 var http = require("http"); 2 http.createServer(function (req,res) { 3 res.sendHeader(200, 4 {"Content-Type": "text/plain"}); 5 6 res.sendBody("Hel"); 7 res.sendBody("lorn"); 8 9 setTimeout(function () { 10 res.sendBody("Worldrn"); 11 res.finish(); 12 }, 2000); 13 }).listen(8000);
  • 57. % node http_server2.js & [4] 27355 % curl http://localhost:8000/ Hello Two seconds later... % node http_server2.js & [4] 27355 % curl http://localhost:8000/ Hello World %
  • 58. 1 var sys = require("sys"); 2 sys.exec("ls -l /") 3 .addCallback(function (output) { 4 sys.puts(output); 5 }); Programs can be run with sys.exec()
  • 59. But Node never forces buffering ∃ a lower-level facility to stream data through the STDIO of the child procresses. Simple IPC.
  • 60. 1 var puts = require("sys").puts; 2 3 var cat = 4 process.createChildProcess("cat"); 5 6 cat.addListener("output", 7 function (data) { 8 if (data) sys.puts(data); 9 }); 10 11 cat.write("hello "); 12 cat.write("worldn"); 13 cat.close();
  • 61. Demo / Experiment An IRC Daemon written in javascript. irc.nodejs.org #node.js Source code: http://tinyurl.com/ircd-js http://gist.github.com/a3d0bbbff196af633995
  • 63. V8 (Google) libev event loop library (Marc Lehmann) libeio thread pool library (Marc Lehmann) http-parser a ragel HTTP parser (Me) evcom stream socket library on top of libev (Me) udns non-blocking DNS resolver (Michael Tokarev)
  • 64. Blocking (or possibly blocking) system calls are executed in the thread pool. Signal handlers and thread pool callbacks are marshaled back into the main thread via a pipe.
  • 65. % node myscript.js < hugefile.txt STDIN_FILENO will refer to a file. Cannot select() on files; read() will block.
  • 66. Solution: Start a pipe, and a “pumping thread”. Pump data from blocking fd into pipe. Main thread can poll for data on the pipe. (See deps/coupling if you’re interested)
  • 67. Future Fix API warts. More modularity; break Node into shared objects. Include libraries for common databases in distribution. Improve performance. TLS support Web Worker-like API. (Probably using ChildProcess)
  • 68. Future Version 0.2 in late December or January. Core API will be frozen.