Getting Started with MongoDB and Node.js


Published on

Slides from my talk at Mongo Seattle 2010.

Published in: Technology
No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

  • How many people have used Node.js? In production?

    Start with V8:
    Chrome’s javascript engine (Fast, aggressively developed)

    Add Evented I/O, system calls, HTTP libraries

    First created by Ryan Dahl @ Joyent

    “Scalable network applications”: tens of thousands of simultaneous connections on a single box
  • High concurrency apps are likely to encounter the C10k problem:

    Overhead per connection: An order of magnitude smaller than most libraries’ thread stack allocations.

    Builds on the approaches used by libasync, twisted, evenmachine, etc.

    Use kernel level event notification (kqueue, epoll, etc.) to notify when files, network connections etc. are ready to be used. App can do other work rather than blocking.

  • Node shares more with Twisted than Rails

  • nginx is amazing at handling static content.

    Buffers are the suggested way for handling binary data, large data, etc.

    The Buffer API is detailed here:

  • Mongo’s shell is javascript, can execute javascript in process

    You can actually write your entire application in javascript - front end and back!
  • In fact, there are several blog engines already. Try Wheat.

  • Of course, building your UI on top of Node.js isn’t a bad idea if the rest of your app fits as well - no need to run a Rails app next to your Node app just for a GUI. (cf. Hummingbird).

  • The current js driver supports master and slave connections, atomic operations, map/reduce...

  • Chained execution of callbacks is how most every Node program will work.
  • Recent builds of Node support udp sockets.
    Mongo’s great at storing semi-structured data quickly
  • As much fun as quadruply-nested callbacks are.
  • As much fun as quadruply-nested callbacks are.
  • Hydrating documents loaded from the db converts them into instances of our models.

    Looks almost like Rails...

  • Getting Started with MongoDB and Node.js

    1. 1. Getting Started With Node.js and mongoDB Grant Goodale (@ggoodale) 7/26/2010
    2. 2. What is ? “Evented I/O for V8 Javascript” – Application engine for “scalable network applications” Really freaking fast: One process, zero* threads (Almost) no blocking * actually, there is a thread pool. We’ll get to that.
    3. 3. What is ? High concurrency with low overhead No thread swapping or context switching Applications must be designed from the ground up to not block on I/O, CPU-intensive work, etc.
    4. 4. What Node.js Isn’t A full-stack web framework (a la Ruby on Rails) But! you can get there: ExpressJS: ( (Web framework with MongoDB support built in - similar to Sinatra) (Rack-like middleware for Node and Express) haml.js: (Pure js implementation of HAML)
    5. 5. What Node.js Isn’t (cont.) Great at sending large static files concurrently. (use nginx.) Buffer implementation in recent versions improves this dramatically: Completely non-blocking. CPU-intensive stuff will still block you (do it in a worker process). Some system calls (executed in a thread pool)
    6. 6. &
    7. 7. & Great together! Both: speak Javascript understand JSON documents are freaking fast. So, what should you build?
    8. 8. & You can build: Standard n-tier web applications Great way to learn mongoDB and Node Go build a blog engine! (Everyone does.) Lots of libraries to help you get started: ExpressJS, Connect, etc.
    9. 9. & You should build: Applications requiring high throughput and concurrency Applications with real-time needs Applications with long-polling connections Real-time multiuser chat Web or log analytics (e.g. Hummingbird)
    10. 10. Getting Started Grab the following: mongoDB: Node.js: npm, a package manager for Node: curl | sh
    11. 11. Getting Started Use npm to install a mongoDB driver for Node: npm install mongodb Tons of other fun stuff as well - full package list at
    12. 12. Evented I/O: a Primer Before:
    13. 13. Evented I/O: a Primer After:
    14. 14. Evented I/O: a Primer After:
    15. 15. Evented I/O: a Primer After:
    16. 16. A simple example: Syslog
    17. 17. Mongoose Cleans up a bit of the nested callback “fun”
    18. 18. Mongoose Cleans up a bit of the nested callback “fun”
    19. 19. Mongoose Adds ORM capabilities (but doesn’t mandate them)
    20. 20. Other Fun Stuff: (Need extensions? We got 'em.) Routers, file servers, frameworks, crypto... Cross-platform WebSockets Supports HTML5 websockets Falls back to Flash / AJAX in less capable browsers
    21. 21. Questions? twitter: ggoodale email: