Modern Web Applications with MongoDB

Preview:

DESCRIPTION

Presentation given to a room full of web designers and developers at DevCon5 in NYC. The presentation introduces concepts critical to understanding how databases work and why it's important to understand even when one typically works much higher in the stack. Topics covered: * History of web development * Picking the right data model * Modern database interaction * How modern databases scale An emphasis is placed on MongoDB in this presentation, but other technologies such as relational (MySQL, Oracle, MSSQL) and Key Value (Cassandra, Riak, DynamoDB) are also covered.

Citation preview

Building Modn Web Acaons

๏ History of web development

๏ Picking the right data model

๏ Modern DB interaction

๏ Modern Web Scale

Agda

What is a modn Web A?

What is a modn Web A?

If you don't ow whe you've come from,

you don't ow whe you are.

- James Burke

te 90’s๏Web is born

๏Web development mostly done in perl or C

๏Everyone is a webmaster

๏Relational databases

r ’s๏ Web growth

redefines scale

๏ Javascript avoided

๏ Dynamic languages come of age

๏ LAMP

๏ Everyone is a PHP programmer

๏ Relational databases

Mid ’s๏ Social re-

redefines scale

๏ Multimedia rules

๏ Heavy caching (memcache) required LAM(m)P

๏ Frameworks (Ruby on Rails) with heavy database abstractions en vogue

๏ Everyone is a OO programmer

๏ Relational databases*

is is whe all falls apa

Condons๏ Web users exponentially increasing

๏ Excessive layering causes applications to be slower

๏ Social (dynamic data) limits use of caching crutch

๏ Cost per byte decreasing rapidly

๏ Data growing in size & complexity

Symptoms๏ Over abstraction

๏ Agile development unsustainable

๏ Needlessly complex architectures

๏ Memcache

2010 trds (Areßing r Ißues)

๏Horizontal scale

๏Variety of Choices (LAMP no more)

๏Specializing

Raonal designed for one ing, used for hing

What is a modn Web A?

What is a modn Web A?

What do we lk for in a database?

๏ Right structure to match my data

๏ Performance & Scale

๏ Features that enable me as a developer

K Queson:WHAT IS A RECORD?

K Value๏ One-dimensional storage

๏ Single value is a blob

๏ Query on key only

๏ Some support secondary indexes

๏ No schema

๏ Value cannot be updated, only replaced

Key Blob

Cassandra, Redis, MemcacheD, Riak, DynamoDB

Raonal๏ Query on any field

๏ In-place updates

๏ Two-dimensional storage

๏ Each field contains a single value

๏ Very structured schema (table)

๏ Normalization process requires many tables, joins, indexes, and poor data locality

Primary Key

Oracle, MSSQL, MySQL, PostgreSQL, DB2

Documt๏ N-dimensional storage ๏ Each field can contain 0, 1,

many, or embedded values๏ Query on any field & level

๏ Flexible schema๏ Inline updates๏ Embedding related data has optimal data locality,

requires fewer indexes, has better performance

_id

MongoDB, CouchDB, RethinkDB

Raonal design

Documt design

Example Blog Post doc

{ _id : ObjectId("4c4ba5c0672c685e5e8aabf3"), author : "steve", date : "Sat Apr 24 2013 19:47:11", text : "About MongoDB...", tags : [ "tech", "databases" ], comments : [ { author : "Fred", date : "Sat Apr 25 2013 20:51:03 GMT-0700", text : "Best Post Ever!" } ]}

What is a modn Web A?

What is a modn Web A?

MongoDB spks your ngauage๏ Drivers in 14+ languages

๏ Interface is natural and idiomatic for each language

๏ Document natively maps to map/hash/object array/dict/struct

place1 = { name : "10gen HQ", address : "229 W 43rd St. 5th Floor", city : "New York", zip : "10036", tags : [ "business", "awesome" ]}

Start with an object (or array, hash, dict, etc)

Inserting the recordInitial Data Load

> db.places.insert(place1)

> db.places.insert(place1)

Querying> db.places.findOne({ zip: "10036", tags: "awesome" })

> db.places.find({tags: [ "rad", "awesome" ]})

{ name : "10gen HQ", address : "229 W 43rd St. 5th Floor", city : "New York", zip : "10036", tags : [ "business", "awesome" ]}

Updating> db.places.update( {name : "10gen HQ"}, { $push : { comments : { author : "steve", date : 6/26/2013, text : "Office hours are great!" } } })

Nested documents// Index nested documents> db.places.ensureIndex({ "comments.author":1 }) // optional> db.places.find({'comments.author':'Fred'})

{ _id : ObjectId("4c4ba5c0672c685e5e8aabf3"), name : "10gen HQ", address : "229 W 43rd St. 5th Floor", city : "New York", zip : "10036", comments : [ { author : "Fred", date : "Sat Apr 25 2013 20:51:03", text : "Best Place Ever!" } ]}

Multiple values// Index on tags (multi-key index)> db.places.ensureIndex({ tags: 1}) // optional> db.places.find( { tags: 'tech' } )

{ _id : ObjectId("4c4ba5c0672c685e5e8aabf3"), name : "10gen HQ", address : "229 W 43rd St. 5th Floor", city : "New York", zip : "10036", tags : [ "business", "awesome", "tech" ],}

Paginating Places in JSper_page = 10; page_num = 3;

places = db.places .find({ "city" : "new york" }) .sort({ "ts" : -1 }) .skip((page_num - 1) * per_page) .limit(per_page);

Paginating Places in Ruby @per_page = 10 @page_num = 3

@places = @db.places.find({ :city => "new york" }).sort({ :ts => -1 }).skip(( @page_num - 1 ) * @per_page).limit(@per_page)

ch ftures๏ Rich query language

๏ GeoSpatial

๏ Text search

๏ Flexible schema

๏ Aggregation & MapReduce

๏ GridFS (distributed & replicated file storage)

๏ Integration with Hadoop, Storm, Solr & more

Database ndscape

Scala

bility

& Pe

rform

ance

Depth of Functionality

MongoDB

Key Value

RDBMS

NoSQL popu

NoSQL popu

NoSQL popu

What is a modn Web A?

What is a modn Web A?

Scabi Needs๏ Data is highly available

๏ Data is consistent

๏ Performant (caching unnecessary)

Difft Aroaches

๏ MultiMaster๏ Peer to peer๏ Has Conflicts๏ Ring based

approach combines high availability and distribution

๏ Complex application logic

๏ Single Master๏ Consistent๏ Slaves have

delayed writes๏ High availability๏ No scalable

solution

๏ Single Master๏ Consistent๏ Secondaries have

delayed writes๏ High availability๏ Range based

distribution

MongoDB : bui to scale๏ Intelligent replication

๏ Automatic partitioning of data(user configurable)

๏ Horizontal Scale

๏ Targeted Queries

๏ Parallel Processing

Igt Repcaon

Node 1Secondary

Node 2Secondary

Node 3Primary

Replication

Heartbeat

Replication

Scable Archecture

Node 1SecondaryConfigServer

Node 1SecondaryConfigServer

Node 1SecondaryConfigServer

Shard Shard Shard

Mongos

App Server

Mongos

App Server

Mongos

App Server

High Avaibi in Shards

Shard

Primary

Secondary

Secondary

Shard

orMongodx

Targed Requests

Shard Shard Shard

Mongos

1

2

3

4

Pall proceßing

Shard Shard Shard

Mongos

1

2 2 2

4 44

3 3 3

6

5

What is a modn Web A?

e g databaseto t your data

e g databasefor YOUR dopmt

e g Databasefor scale & Pfoance

Gng staed

sy deploymt๏ Heroku

๏ Rackspace

๏ Amazon

๏ Engine Yard

๏ App Fog

๏ ServerGrove

๏ Azure

๏ Nodejitsu

Indust acaon๏ Media &

Entertainment

๏ Retail

๏ Social

๏ Finance

๏ Gaming

๏ Insurance

๏ Healthcare

๏ Government

๏ Archiving

๏ Telecom

๏ Education

E IF YOU KED !Questions?

http://spf13.comhttp://github.com/spf13@spf13

#DevCon5

Recommended