59
Building Modn Web Acaons

Modern Web Applications with MongoDB

Embed Size (px)

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

Page 1: Modern Web Applications with MongoDB

Building Modn Web Acaons

Page 3: Modern Web Applications with MongoDB

๏ History of web development

๏ Picking the right data model

๏ Modern DB interaction

๏ Modern Web Scale

Agda

Page 4: Modern Web Applications with MongoDB

What is a modn Web A?

Page 5: Modern Web Applications with MongoDB

What is a modn Web A?

Page 6: Modern Web Applications with MongoDB

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

you don't ow whe you are.

- James Burke

Page 7: Modern Web Applications with MongoDB

te 90’s๏Web is born

๏Web development mostly done in perl or C

๏Everyone is a webmaster

๏Relational databases

Page 8: Modern Web Applications with MongoDB

r ’s๏ Web growth

redefines scale

๏ Javascript avoided

๏ Dynamic languages come of age

๏ LAMP

๏ Everyone is a PHP programmer

๏ Relational databases

Page 9: Modern Web Applications with MongoDB

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*

Page 10: Modern Web Applications with MongoDB

is is whe all falls apa

Page 11: Modern Web Applications with MongoDB

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

Page 12: Modern Web Applications with MongoDB

Symptoms๏ Over abstraction

๏ Agile development unsustainable

๏ Needlessly complex architectures

๏ Memcache

Page 13: Modern Web Applications with MongoDB

2010 trds (Areßing r Ißues)

๏Horizontal scale

๏Variety of Choices (LAMP no more)

๏Specializing

Page 14: Modern Web Applications with MongoDB

Raonal designed for one ing, used for hing

Page 15: Modern Web Applications with MongoDB

What is a modn Web A?

Page 16: Modern Web Applications with MongoDB

What is a modn Web A?

Page 17: Modern Web Applications with MongoDB

What do we lk for in a database?

๏ Right structure to match my data

๏ Performance & Scale

๏ Features that enable me as a developer

Page 18: Modern Web Applications with MongoDB

K Queson:WHAT IS A RECORD?

Page 19: Modern Web Applications with MongoDB

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

Page 20: Modern Web Applications with MongoDB

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

Page 21: Modern Web Applications with MongoDB

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

Page 22: Modern Web Applications with MongoDB

Raonal design

Page 23: Modern Web Applications with MongoDB

Documt design

Page 24: Modern Web Applications with MongoDB

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!" } ]}

Page 25: Modern Web Applications with MongoDB

What is a modn Web A?

Page 26: Modern Web Applications with MongoDB

What is a modn Web A?

Page 27: Modern Web Applications with MongoDB

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

Page 28: Modern Web Applications with MongoDB

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)

Page 29: Modern Web Applications with MongoDB

Inserting the recordInitial Data Load

> db.places.insert(place1)

> db.places.insert(place1)

Page 30: Modern Web Applications with MongoDB

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" ]}

Page 31: Modern Web Applications with MongoDB

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

Page 32: Modern Web Applications with MongoDB

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!" } ]}

Page 33: Modern Web Applications with MongoDB

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" ],}

Page 34: Modern Web Applications with MongoDB

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);

Page 35: Modern Web Applications with MongoDB

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)

Page 36: Modern Web Applications with MongoDB

ch ftures๏ Rich query language

๏ GeoSpatial

๏ Text search

๏ Flexible schema

๏ Aggregation & MapReduce

๏ GridFS (distributed & replicated file storage)

๏ Integration with Hadoop, Storm, Solr & more

Page 37: Modern Web Applications with MongoDB

Database ndscape

Scala

bility

& Pe

rform

ance

Depth of Functionality

MongoDB

Key Value

RDBMS

Page 38: Modern Web Applications with MongoDB

NoSQL popu

Page 39: Modern Web Applications with MongoDB

NoSQL popu

Page 40: Modern Web Applications with MongoDB

NoSQL popu

Page 41: Modern Web Applications with MongoDB

What is a modn Web A?

Page 42: Modern Web Applications with MongoDB

What is a modn Web A?

Page 43: Modern Web Applications with MongoDB

Scabi Needs๏ Data is highly available

๏ Data is consistent

๏ Performant (caching unnecessary)

Page 44: Modern Web Applications with MongoDB

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

Page 45: Modern Web Applications with MongoDB

MongoDB : bui to scale๏ Intelligent replication

๏ Automatic partitioning of data(user configurable)

๏ Horizontal Scale

๏ Targeted Queries

๏ Parallel Processing

Page 46: Modern Web Applications with MongoDB

Igt Repcaon

Node 1Secondary

Node 2Secondary

Node 3Primary

Replication

Heartbeat

Replication

Page 47: Modern Web Applications with MongoDB

Scable Archecture

Node 1SecondaryConfigServer

Node 1SecondaryConfigServer

Node 1SecondaryConfigServer

Shard Shard Shard

Mongos

App Server

Mongos

App Server

Mongos

App Server

Page 48: Modern Web Applications with MongoDB

High Avaibi in Shards

Shard

Primary

Secondary

Secondary

Shard

orMongodx

Page 49: Modern Web Applications with MongoDB

Targed Requests

Shard Shard Shard

Mongos

1

2

3

4

Page 50: Modern Web Applications with MongoDB

Pall proceßing

Shard Shard Shard

Mongos

1

2 2 2

4 44

3 3 3

6

5

Page 51: Modern Web Applications with MongoDB

What is a modn Web A?

Page 52: Modern Web Applications with MongoDB

e g databaseto t your data

Page 53: Modern Web Applications with MongoDB

e g databasefor YOUR dopmt

Page 54: Modern Web Applications with MongoDB

e g Databasefor scale & Pfoance

Page 55: Modern Web Applications with MongoDB

Gng staed

Page 56: Modern Web Applications with MongoDB

sy deploymt๏ Heroku

๏ Rackspace

๏ Amazon

๏ Engine Yard

๏ App Fog

๏ ServerGrove

๏ Azure

๏ Nodejitsu

Page 57: Modern Web Applications with MongoDB

Indust acaon๏ Media &

Entertainment

๏ Retail

๏ Social

๏ Finance

๏ Gaming

๏ Insurance

๏ Healthcare

๏ Government

๏ Archiving

๏ Telecom

๏ Education

Page 58: Modern Web Applications with MongoDB

E IF YOU KED !Questions?

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

#DevCon5

Page 59: Modern Web Applications with MongoDB