40
Introduction to M. Khurrum Qureshi Sr. Software Engineer

Intro to node and mongodb 1

Embed Size (px)

Citation preview

Page 1: Intro to node and mongodb   1

Introduction to

M. Khurrum QureshiSr. Software Engineer

Page 2: Intro to node and mongodb   1

Node’s Goal is to provide an easy way to build scalable

network programs.

Page 3: Intro to node and mongodb   1

Node.js is NOT anotherweb framework!

But you can create a web framework using NPM modules.

Page 4: Intro to node and mongodb   1

Node.js is…Web ServerTCP Server

Awesome Robot ControllerCommand Line Application

Proxy ServerStreaming ServerVoiceMail ServerMusic Machine

Anything that has to deal with high I/O

Page 5: Intro to node and mongodb   1

Node.js isServer Side JavaScript!

Page 6: Intro to node and mongodb   1

Node.js is

FUN!

Page 7: Intro to node and mongodb   1

Why Node.js?• Non Blocking I/O• Based on Chrome’s V8 Engines (FAST!)• 15,000+ Modules • Active Community (IRC, Mailing Lists, Twitter,

Github)• Mac, Linux and Windows (all first class citizens)• One Language for Frontend and Backend• JavaScript is the Language of the Web

Page 8: Intro to node and mongodb   1

Node.js Good Use Cases• JSON APIsBuilding light-weight REST / JSON api's is something where node.js really shines. Its non-blocking I/O model combined with JavaScript make it a great choice for wrapping other data sources such as databases or web services and exposing them via a JSON interface.

• Single Page AppsIf you are planning to write an AJAX heavy single page app (think gmail), node.js is a great fit as well. The ability to process many requests / seconds with low response times, as well as sharing things like validation code between the client and server make it a great choice for modern web applications that do lots of processing on the client.

Page 9: Intro to node and mongodb   1

Node.js Good Use Cases• Shelling out to Unix ToolsWith node.js still being young, it's tempting to re-invent all kinds of software for it. However, an even better approach is tapping into the vast universe of existing command line tools. Node's ability to spawn thousands of child processes and treating their outputs as a stream makes it an ideal choice for those seeking to leverage existing software.

• Streaming DataTraditional web stacks often treat http requests and responses as atomic events. However, the truth is that they are streams, and many cool node.js applications can be built to take advantage of this fact. One great example is parsing file uploads in real time, as well as building proxies between different data layers.

Page 10: Intro to node and mongodb   1

Node.js Good Use Cases• Soft Real Time ApplicationsAnother great aspect of node.js is the ease at which you can develop soft real time systems. By that I mean stuff like twitter, chat software, sport bets or interfaces to instant messaging networks.

Page 11: Intro to node and mongodb   1

Basic HTTP Servervar http = require('http'); var server = http.createServer(function (req, res) {  res.writeHead(200);  res.end('Hello World');}); server.listen(4000);

Page 12: Intro to node and mongodb   1

Some people use the core http module to

build their web apps, most use a framework

like Expressor Connect or Flatiron or Tako or Derby or Geddy or Mojito or …

Page 13: Intro to node and mongodb   1

Visithttp://expressjs.com/guide.html

for a detailed guide on using Express

Page 14: Intro to node and mongodb   1

What is Non-Blocking I/O?And why should I care?

Page 15: Intro to node and mongodb   1

Blocking I/

270ms = SUM(user, activities, leaderboard)

// Get User – 20ms$query = 'SELECT * FROM users WHERE id = ?';$users = query($query, array($id));print_r($users); // Get Activities – 100ms$query = 'SELECT * FROM activities ORDER BY timestamp LIMIT 50';$activities = query($query);print_r($activities); // Get Leader Board – 150ms$query = 'SELECT count(points) as total, user_id FROM activities LIMIT 50';$leader_board = query($query);

Page 16: Intro to node and mongodb   1

Non-Blocking I/

150ms = MAX(user, activities, leaderboard)

// Get User – 20msvar query = 'SELECT * FROM users WHERE id = ?';db.query(query, [userId], function (err, results) {  console.log(results);}); // Get Activities – 100msvar query = 'SELECT * FROM activities ORDER BY timestamp LIMIT 50';db.query(query, function (err, results) {  console.log(results);}); // Get Leader Board – 150msvar query = 'SELECT count(points) as total, user_id FROM activities LIMIT 50';db.query(query, function (err, results) {  console.log(results);});

Page 17: Intro to node and mongodb   1

The most jarring thing about Server Side JavaScript

is thinking in callbacks

Page 18: Intro to node and mongodb   1

The Node Callback PatternawesomeFunction(arg, function (err, data) {   if (err) {      // Handle Error   }      // Do something awesome with results.});

• Error first then success… ALWAYS!• Because this is the de-facto standard 99.99999% of the time

you will be able to guess how a Node library will work.

Page 19: Intro to node and mongodb   1

Callbacks are the Devil’s Work!Don’t go down this rabbit hole…

One of the biggest mistakes is to get yourself in to callback hell by nesting callbacks inside of

callbacks inside of more callbacks.

var userQuery = 'SELECT * FROM users WHERE id = ?';var activityQuery = 'SELECT * FROM activities ORDER BY timestamp LIMIT 50';var leaderBoardQuery = 'SELECT count(points) as total, user_id FROM activities LIMIT 50'; db.query(userQuery, [id], function (userErr, userResults) {   db.query(activityQuery, function (activityErr, activityResults) {      db.query(leaderBoardQuery, function (leaderBoardErr, leaderBoardResults) {          // Do something here       });   });});

Page 20: Intro to node and mongodb   1

Avoiding Callback Hell• Keep your code shallow• Break up your code into small chunks• Use a sequential library like async• Visit http://callbackhell.com

Page 21: Intro to node and mongodb   1

Async to the rescue!var async = require('async');var db = require(’db'); function getUser (callback) {  var query = 'SELECT * FROM users WHERE id = ?';  db.query(query, [userId], callback);} function getActivities (callback) {  var query = 'SELECT * FROM activities ORDER BY timestamp LIMIT 50';  db.query(query, callback);} function getLeaderBoard (callback) {  var query = 'SELECT count(points) as total, user_id FROM activities LIMIT 50';  db.query(query, callback);} var tasks = [getUser, getActivities, getLeaderBoard];async.parallel(tasks, function (err, results) {  var user = results[0];  var activities = results[1];  var leaderBoard = results[2];});

Page 22: Intro to node and mongodb   1

Visithttps://github.com/caolan/async

for a detailed guide on using the async module.

Async provides several useful patterns for asynchronous control flow

including: parallel, series, waterfall, auto and queue.

Page 23: Intro to node and mongodb   1

The Node Package Managerotherwise know as… NPM

It’s how you harness the awesomeness of the Node.js community!

Page 24: Intro to node and mongodb   1

Using NPMIt’s standard practice to install modules locally for your current project. Modules are installed in the ./node_modules in the current directory.

To Install a new module

npm install <module>

To find a module in the NPM repository

npm search <search string>

To list the modules (and their dependencies) in the current project

npm list

To see module details

npm info <module>

Page 25: Intro to node and mongodb   1

DON’T INSTALL MODULES GLOBALLY!

Unless they are tools like node-dev, jake, express, minify-js OR linked development modules but more on that later.

Page 26: Intro to node and mongodb   1

NPM is awesome sauce!

Visithttps://npmjs.org

for more details about NPM and to browse the current NPM Repository

Page 27: Intro to node and mongodb   1

Node.js Modules• async • connect • express • mongodb-

native-driver• request

• apn • ql.io-engine • pem • winston • winston-

mongodb

Page 28: Intro to node and mongodb   1

Node.js Modules• node-sql • nodemailer • connect-http-

signature• http-signature• underscore

• file-utils • validator • mongoskin • passport.Js • yql

Page 29: Intro to node and mongodb   1

Node.js Modules• node-gcm • forever • mongodb_s3_b

ackup• nconf

• node-sqlserver• Socket-io• generic-pool• And many

others

Page 30: Intro to node and mongodb   1

Deployment Platforms• Amazon EC2 • Windows Azure• Heroku• Joynet• Nodejistu

Page 31: Intro to node and mongodb   1

Questions?

Page 32: Intro to node and mongodb   1

Introduction to

Page 33: Intro to node and mongodb   1

NoSql• Non-Relational• Horizontally Scalable• Distributed• Schema-Free• Open-Source• Replication Support • Simple API

Page 34: Intro to node and mongodb   1

NoSql Flavours• Key-value Store.• Graph• Big Table• Document Store

Page 35: Intro to node and mongodb   1

mongoDB Overview• Document Database

– Documents (objects) map nicely to programming language data types. – Embedded documents and arrays reduce need for joins. – Dynamic schema

• High Performance– Embedding makes reads and writes fast.– Indexes can include keys from embedded documents and arrays.– Optional streaming writes (no acknowledgments).

• High Availability– Replicated servers with automatic master failover.

Page 36: Intro to node and mongodb   1

mongoDB Overview• Easy Scalability

– Automatic sharding distributes collection data across machines.

Page 37: Intro to node and mongodb   1

mongoDB Data Model• MongoDB instance hosts a number of databases.• A database holds a set of collections.• A collection holds a set of documents.• A document is a set of key-value pairs. • Documents have dynamic schema.

Page 38: Intro to node and mongodb   1

Document Structure• Data is stored in the form of JSON data.(Which

internally stored as BSON){ "_id" : ObjectId("4ccbfc75bd163019417c27f8"), "title": “Hello World! ", "author": { "firstname": "Joe", "lastname": "Bloggs" }, "tags": ["test", "foo", "bar"] }

Page 39: Intro to node and mongodb   1

Key mongoDB Features• Flexibility• Power• Speed/Scaling• Ease of use

Page 40: Intro to node and mongodb   1

Questions?