20
John Culviner GitHub: github.com/johnculviner Blog: johnculviner.com Twitter: @johnculviner Email: [email protected] +

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

Embed Size (px)

Citation preview

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

John CulvinerGitHub: github.com/johnculvinerBlog: johnculviner.comTwitter: @johnculvinerEmail: [email protected]

+

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

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

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

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!

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

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

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

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

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

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?

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

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

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

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

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

SUPER efficient with pipes

Actual entire node application

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

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

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

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

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

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’

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

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)

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

Actual* email from MongoDB….*minus the bropart

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

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

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

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 ¯\_(ツ )_/¯

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

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

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

Node REPLV8 JavaScript engine

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

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

Okay seriously code now…

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

Questions/Comments?

John CulvinerGitHub: github.com/johnculvinerBlog: johnculviner.comTwitter: @johnculvinerEmail: [email protected]