Node.js and MongoDB Compared to C#/Java/SQL

Preview:

Citation preview

John CulvinerGitHub: github.com/johnculvinerBlog: johnculviner.comTwitter: @johnculvinerEmail: john@johnculviner.com

+

About Me Independent JavaScript, C#,

and JVM Consultant Node.js NoSql (ElasticSearch, MongoDB,RavenDB) C# .NET Java/Groovy/Spring Aspiring DevOps (Ansible, haproxy, nginx on CentOS/Ubuntu) MS-SQL/MySQL

Heavy GUI JavaScript development Manual, jQuery, Knockout.js, Durandal.js, Ember.js, Angular.js, Aurelia, React.js SPA development

Open Source Street Cred AngularAgility jQuery File Download FluentKnockoutHelpers

OverviewWhat is Node.js & what's it good for?

vs .NET, Java etc.What is MongoDB & what's it good for?

vs YOUR_FAV_SQL_DB_HEREBuilding a CMS from scratch

Environment setupSimple API Endpoint

Objective: What you should and shouldn't use this stack for. To help you on your feet nice and easy!

What is ?

Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine. It's like Chrome but it has framework modules that

make it useful on a serverAssertion Testing

Buffer C/C++ Addons

Child Processes

Cluster Console

Crypto Debugger DNS Domain Errors Events

File System

Globals HTTP HTTPS Modules Net

OS Path Process Punycode Query Strings

Readline

REPL Stream String Decoder

Timers TLS/SSL TTY

UDP/Datagram

URL Utilities V8 VM ZLIB

Node.js' package ecosystem, npm (node package manager), is the largest ecosystem of open source libraries in the world.modulecounts.com

What is ?Node.js uses an event-driven, non-blocking I/O

model that makes it lightweight and efficient. Single threaded event loop just like the

JavaScript you know and ?love?

Event loop?EVERYTHING that is I/O bound blocking in

Node.js is asyncNode FORCES you to deal with I/O bound nature

of most of the apps we build* up front rather than adding parallelization later on….

*by we I mean me and likely 90% of you

CPU bound?Don't block the single thread or else your

app will grind to a haltThink of times a website has locked up

your browser’s UI Classic while true

CPU intensive operations

Stack overflow

SUPER efficient with pipes

Actual entire node application

Why do I care ¯\_(ツ )_/¯

Allegedly (when they aren’t using Go)

--------------------------Big places in town you could work for ------------------------------

Places with mycrappy Node codein production

Node.js - Good, BadGOOD

Web(like) applications that are limitedby I/O blocking (DB, backend service, file system, etc. calls)

Rapid application developmentMicro-services

BADCPU intensive apps (you'll block the event loop

and JS is slow(er) than .NET, JVM etc.)Large, monolithic ‘enterprisey’

applications (strong typing *can* be nice)You think JavaScript is for script-kiddies

What is A “NoSQL” BSON (binary JSON) document DB

Indexable, deep queryable, map-reduce aggregationsSharding – for data distribution “Scale out” on a shard key

Replication– for instance failure ‘safety’

Example

Shard Cluster (can just do one DB too)

Instance A

Instance B

{ _id: ‘UUID’, customerName: ‘Foo’ customerDomain ‘foo.com’….}, {/*cust.*/},{/*cust.*/}

CollectionsPages

Cutomers

{ _id: ‘UUID’, customerId: ‘UUID’ url: ‘/someurl’, widgets: [ {name: ‘WidgetA’ /*settings*/}, {name: ‘WidgetB’, /*settings*/} ] …..}, {/*page*/}, {/*page*/}…..

- Similar BSON documents that end up here due to a differing shard key from left

- BSON documents WITHIN A COLLECTION can have a varying schema! (but in practice that might be a bad idea)

Actual* email from MongoDB….*minus the bropart

Cutting through the BS…NoSQL works well for apps with few

“Aggregate Roots”“A group/cluster of objects that work together, are

treated as a unit that must remain consistent together”

Martin Fowler

Good use casePages have Sections have Widgets have Settings - settings might contain nested object graphs etc.

• Only operate on a page (read/write) as an atomic unit

• Duplication of data is small if not non existent

Bad use caseCustomer

sOrders

LineItemsProducts Customer

Categories

MerchantCategorie

sVendors

• “Spiderweb” of relationships that duplicate in a JOIN

• Care about querying, operating on various “entities” individually or within the bounds of an atomic transaction

Random DB observationsWhy not BOTH relational and Document?

Stores JSON natively and is querable/indexable! Free (unlike MSSQL $1.5K/yr ridiculous per virtual core

license)

queries get slow in the millions of objects in a collection (w/o index)

Server side joins just added in version 3.2 (latest)RavenDB

Auto indexing is cool Server side joins! Works well with .NET (Windows only, written in C#) “Cowboy coded” IMO so stability is ¯\_(ツ )_/¯

Before we code…Tooling we are using:

JetBrains IntelliJ/WebstormNodeJS 5.4MongoDB 3.x iTerm + oh-my-zsh (z-shell)RoboMongo (the Mongo GUI)

Install on *NIX[brew|apt-get|yum|etc..] install [mongodb|nodejs]

Install on WindowsDownload MSIs/EXEs from respective websites

Node REPLV8 JavaScript engine

in Node.js 4+ and Chrome 45+ supports string templating and LAMBDAS (finally)

Okay seriously code now…

Questions/Comments?

John CulvinerGitHub: github.com/johnculvinerBlog: johnculviner.comTwitter: @johnculvinerEmail: john@johnculviner.com

Recommended