Hi, I am Nachiket Mehta.
- I am a frontend engineer at Mediafly.
- We use Node.js for automation with Gulp as well as with NW.js, formerly known as node-webkit.
- Lately we have started using Node as a backend to create apis.
- I would like to take this opportunity to talk about my learning of Node.js and how it can be a great tool to solve certain problems.
Agenda
I/O
Problem
What is Node.js
Single threaded nature
Asynchronous
How node helps with I/O
Event Loop
Callback model
Backend of Frontend
All about I/O
Sample API using express
I want to cover the following:
- I/O and its impacts on an application
- What is Node.js? What makes Node.js different?
- How Node.js's single threaded model works and how it embraces asynchronous programming
- Single threaded nature and the event loop
- What it means to build a backend of a frontend
- Node.js as a backend for a frontend
Problem with I/O
- This slide is taken from JSConf 2009.
- By the creator of Node.js, Ryan Dahl.
- Shows how many clock cycles per operation.
- These are not hard and fast numbers, but magnitudes is what we care about.
- When we get to the Disk and Network, we are talking 41M and 240M cycles!
Waiting...
Very expensive to scale this with threads
Most threads will just be waiting for I/O
Lot of context switching overhead
- Node assumes that if you are an I/O bound app, you spend a lot of time waiting.
Here, the green bars are the actual time your app is doing something.
Gray bars represent the time spent while waiting.
Here is what happens in this example:
1) route a request, then
wait wait wait
2) query a db
wait wait wait
3) process results
wait wait
4) log to a file
wait wait
5) format and send the response
Now that we have the problem in front of us, let's see what Node is and how it can help us solve it.
What is Node.js?
Node.js is a server-side Javascript platform
Based on Google's V8 engine (Chrome browser runs on it)
However, this is only for the server-side
It is different from some traditional server-side technologies such as ASP.NET, Ruby on Rails, etc.
Let us talk about these differences to highlight Node's capabilities.
Node.js Event Loop
- Here is the conceptual model.
- There is a queue of tasks that need to be executed.
- The event loop will grab something from the top of the queue and execute it.
- Once it finishes the task, it pulls out the next event from the queue and executes it.
- This is an important concept to understand before digging deeper into Node.js
- Lets look at how all of this is possible with Javascript. Node.js leverages this single-threaded nature of Javascript.
- What we mean by that is that there is no concept of threads in Javascript. However, the language allows for asynchronous programming.
Let's look at an example:
- How many of you have done something like this? .
If you understand the concept of a callback function, Node.js is all about callbacks.
- Let's create a simple http server and fire it up!
http.createServer(function(req, res){
res.WriteHead(200, {'Content-Type': 'plain/text'});
res.end('Hello World');
}).listen(1337);
Single Page Apps and their backends
SPAs are a great way to separate concerns
They need backends that transform and serve data
Backend of a SPA frontend typically needs to:
Serving up JSON data
Serving static files (HTML, CSS and Javascript)
Calling other HTTP services
Looking up things in a database
Accessing the disk for other misc. things
Mostly I/O concerns
Node can help with I/O and Javascript
Added benefit of being able to use Javascript both client and server side
- Alright, quick show of hands. How many of you are building some kind of single page application?
- Ok, so you guys must be familiar with a backend of a frontend then? Alright!
- Since browsers are becoming so much more capable, we are able to just serve up the JSON data to the client and not concern ourselves with rendering on the server-side.
- Backend of a SPA frontend is specialized to do certain things. Lets look at some of these needs.
- All these concerns are I/O concerns. Typically these backends will spend very little time actually processing anything, but rather access data and transform it per the client's needs. Which means, they will just sit and wait for things to be returned to them. This is where Node.js can play an important role. Not only that frontend developers get an already well understood language, but it is really tailored for this purpose as well!
Conclusion
Node.js, like any tool, is not always the right choice
Not suitable for a system that does some intensive processing
I/O bound apps can benefit from Node.js
- Use the write tool for the job
- Understand the costs of I/O
- Another awesome feature is streams. But that's a whole separate topic of its own.