SlideShare a Scribd company logo
1 of 69
Download to read offline
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 (20)

Spring Core
Spring CoreSpring Core
Spring Core
 
Node.Js: Basics Concepts and Introduction
Node.Js: Basics Concepts and Introduction Node.Js: Basics Concepts and Introduction
Node.Js: Basics Concepts and Introduction
 
Golang basics for Java developers - Part 1
Golang basics for Java developers - Part 1Golang basics for Java developers - Part 1
Golang basics for Java developers - Part 1
 
NodeJS for Beginner
NodeJS for BeginnerNodeJS for Beginner
NodeJS for Beginner
 
Introduction to Node.js
Introduction to Node.jsIntroduction to Node.js
Introduction to Node.js
 
Express js
Express jsExpress js
Express js
 
Web api
Web apiWeb api
Web api
 
Vue.js
Vue.jsVue.js
Vue.js
 
REST API Design & Development
REST API Design & DevelopmentREST API Design & Development
REST API Design & Development
 
Spring MVC Framework
Spring MVC FrameworkSpring MVC Framework
Spring MVC Framework
 
An Introduction to Maven
An Introduction to MavenAn Introduction to Maven
An Introduction to Maven
 
jQuery PPT
jQuery PPTjQuery PPT
jQuery PPT
 
Introduction to NodeJS
Introduction to NodeJSIntroduction to NodeJS
Introduction to NodeJS
 
Nodejs presentation
Nodejs presentationNodejs presentation
Nodejs presentation
 
Rest api with node js and express
Rest api with node js and expressRest api with node js and express
Rest api with node js and express
 
Angular js PPT
Angular js PPTAngular js PPT
Angular js PPT
 
Spring MVC
Spring MVCSpring MVC
Spring MVC
 
Node.js Express
Node.js  ExpressNode.js  Express
Node.js Express
 
Spring boot introduction
Spring boot introductionSpring boot introduction
Spring boot introduction
 
AngularJS for Beginners
AngularJS for BeginnersAngularJS for Beginners
AngularJS for Beginners
 

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

AI mind or machine power point presentation
AI mind or machine power point presentationAI mind or machine power point presentation
AI mind or machine power point presentationyogeshlabana357357
 
Continuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
Continuing Bonds Through AI: A Hermeneutic Reflection on ThanabotsContinuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
Continuing Bonds Through AI: A Hermeneutic Reflection on ThanabotsLeah Henrickson
 
JohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptxJohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptxJohnPollard37
 
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...TrustArc
 
Vector Search @ sw2con for slideshare.pptx
Vector Search @ sw2con for slideshare.pptxVector Search @ sw2con for slideshare.pptx
Vector Search @ sw2con for slideshare.pptxjbellis
 
ChatGPT and Beyond - Elevating DevOps Productivity
ChatGPT and Beyond - Elevating DevOps ProductivityChatGPT and Beyond - Elevating DevOps Productivity
ChatGPT and Beyond - Elevating DevOps ProductivityVictorSzoltysek
 
2024 May Patch Tuesday
2024 May Patch Tuesday2024 May Patch Tuesday
2024 May Patch TuesdayIvanti
 
How to Check GPS Location with a Live Tracker in Pakistan
How to Check GPS Location with a Live Tracker in PakistanHow to Check GPS Location with a Live Tracker in Pakistan
How to Check GPS Location with a Live Tracker in Pakistandanishmna97
 
Microsoft CSP Briefing Pre-Engagement - Questionnaire
Microsoft CSP Briefing Pre-Engagement - QuestionnaireMicrosoft CSP Briefing Pre-Engagement - Questionnaire
Microsoft CSP Briefing Pre-Engagement - QuestionnaireExakis Nelite
 
“Iamnobody89757” Understanding the Mysterious of Digital Identity.pdf
“Iamnobody89757” Understanding the Mysterious of Digital Identity.pdf“Iamnobody89757” Understanding the Mysterious of Digital Identity.pdf
“Iamnobody89757” Understanding the Mysterious of Digital Identity.pdfMuhammad Subhan
 
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptxHarnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptxFIDO Alliance
 
Working together SRE & Platform Engineering
Working together SRE & Platform EngineeringWorking together SRE & Platform Engineering
Working together SRE & Platform EngineeringMarcus Vechiato
 
AI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by AnitarajAI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by AnitarajAnitaRaj43
 
Tales from a Passkey Provider Progress from Awareness to Implementation.pptx
Tales from a Passkey Provider  Progress from Awareness to Implementation.pptxTales from a Passkey Provider  Progress from Awareness to Implementation.pptx
Tales from a Passkey Provider Progress from Awareness to Implementation.pptxFIDO Alliance
 
Introduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDMIntroduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDMKumar Satyam
 
Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...
Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...
Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...ScyllaDB
 
الأمن السيبراني - ما لا يسع للمستخدم جهله
الأمن السيبراني - ما لا يسع للمستخدم جهلهالأمن السيبراني - ما لا يسع للمستخدم جهله
الأمن السيبراني - ما لا يسع للمستخدم جهلهMohamed Sweelam
 
The Ultimate Prompt Engineering Guide for Generative AI: Get the Most Out of ...
The Ultimate Prompt Engineering Guide for Generative AI: Get the Most Out of ...The Ultimate Prompt Engineering Guide for Generative AI: Get the Most Out of ...
The Ultimate Prompt Engineering Guide for Generative AI: Get the Most Out of ...SOFTTECHHUB
 
WebRTC and SIP not just audio and video @ OpenSIPS 2024
WebRTC and SIP not just audio and video @ OpenSIPS 2024WebRTC and SIP not just audio and video @ OpenSIPS 2024
WebRTC and SIP not just audio and video @ OpenSIPS 2024Lorenzo Miniero
 
ADP Passwordless Journey Case Study.pptx
ADP Passwordless Journey Case Study.pptxADP Passwordless Journey Case Study.pptx
ADP Passwordless Journey Case Study.pptxFIDO Alliance
 

Recently uploaded (20)

AI mind or machine power point presentation
AI mind or machine power point presentationAI mind or machine power point presentation
AI mind or machine power point presentation
 
Continuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
Continuing Bonds Through AI: A Hermeneutic Reflection on ThanabotsContinuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
Continuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
 
JohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptxJohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptx
 
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
 
Vector Search @ sw2con for slideshare.pptx
Vector Search @ sw2con for slideshare.pptxVector Search @ sw2con for slideshare.pptx
Vector Search @ sw2con for slideshare.pptx
 
ChatGPT and Beyond - Elevating DevOps Productivity
ChatGPT and Beyond - Elevating DevOps ProductivityChatGPT and Beyond - Elevating DevOps Productivity
ChatGPT and Beyond - Elevating DevOps Productivity
 
2024 May Patch Tuesday
2024 May Patch Tuesday2024 May Patch Tuesday
2024 May Patch Tuesday
 
How to Check GPS Location with a Live Tracker in Pakistan
How to Check GPS Location with a Live Tracker in PakistanHow to Check GPS Location with a Live Tracker in Pakistan
How to Check GPS Location with a Live Tracker in Pakistan
 
Microsoft CSP Briefing Pre-Engagement - Questionnaire
Microsoft CSP Briefing Pre-Engagement - QuestionnaireMicrosoft CSP Briefing Pre-Engagement - Questionnaire
Microsoft CSP Briefing Pre-Engagement - Questionnaire
 
“Iamnobody89757” Understanding the Mysterious of Digital Identity.pdf
“Iamnobody89757” Understanding the Mysterious of Digital Identity.pdf“Iamnobody89757” Understanding the Mysterious of Digital Identity.pdf
“Iamnobody89757” Understanding the Mysterious of Digital Identity.pdf
 
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptxHarnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
 
Working together SRE & Platform Engineering
Working together SRE & Platform EngineeringWorking together SRE & Platform Engineering
Working together SRE & Platform Engineering
 
AI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by AnitarajAI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by Anitaraj
 
Tales from a Passkey Provider Progress from Awareness to Implementation.pptx
Tales from a Passkey Provider  Progress from Awareness to Implementation.pptxTales from a Passkey Provider  Progress from Awareness to Implementation.pptx
Tales from a Passkey Provider Progress from Awareness to Implementation.pptx
 
Introduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDMIntroduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDM
 
Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...
Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...
Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...
 
الأمن السيبراني - ما لا يسع للمستخدم جهله
الأمن السيبراني - ما لا يسع للمستخدم جهلهالأمن السيبراني - ما لا يسع للمستخدم جهله
الأمن السيبراني - ما لا يسع للمستخدم جهله
 
The Ultimate Prompt Engineering Guide for Generative AI: Get the Most Out of ...
The Ultimate Prompt Engineering Guide for Generative AI: Get the Most Out of ...The Ultimate Prompt Engineering Guide for Generative AI: Get the Most Out of ...
The Ultimate Prompt Engineering Guide for Generative AI: Get the Most Out of ...
 
WebRTC and SIP not just audio and video @ OpenSIPS 2024
WebRTC and SIP not just audio and video @ OpenSIPS 2024WebRTC and SIP not just audio and video @ OpenSIPS 2024
WebRTC and SIP not just audio and video @ OpenSIPS 2024
 
ADP Passwordless Journey Case Study.pptx
ADP Passwordless Journey Case Study.pptxADP Passwordless Journey Case Study.pptx
ADP Passwordless Journey Case Study.pptx
 

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.