Modern Web Applications with MongoDB

43,641
-1

Published on

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.

Published in: Technology
1 Comment
33 Likes
Statistics
Notes
No Downloads
Views
Total Views
43,641
On Slideshare
0
From Embeds
0
Number of Embeds
27
Actions
Shares
0
Downloads
344
Comments
1
Likes
33
Embeds 0
No embeds

No notes for slide

Modern Web Applications with MongoDB

  1. 1. Building Modn Web Acaons
  2. 2. @spf13Steve Francia AKA Chief Evangelist @ responsible for Developer Experience: drivers, integrations, web & technical writing
  3. 3. ๏ History of web development ๏ Picking the right data model ๏ Modern DB interaction ๏ Modern Web Scale Agda
  4. 4. What is a modn Web A?
  5. 5. What is a modn Web A?
  6. 6. If you don't ow whe you've come from, you don't ow whe you are. - James Burke
  7. 7. te 90’s ๏Web is born ๏Web development mostly done in perl or C ๏Everyone is a webmaster ๏Relational databases
  8. 8. r ’s ๏ Web growth redefines scale ๏ Javascript avoided ๏ Dynamic languages come of age ๏ LAMP ๏ Everyone is a PHP programmer ๏ Relational databases
  9. 9. 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*
  10. 10. is is whe  all falls apa
  11. 11. Condons ๏ 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
  12. 12. Symptoms ๏ Over abstraction ๏ Agile development unsustainable ๏ Needlessly complex architectures ๏ Memcache
  13. 13. 2010 trds (Areßing r Ißues) ๏Horizontal scale ๏Variety of Choices (LAMP no more) ๏Specializing
  14. 14. Raonal designed for one ing, used for hing
  15. 15. What is a modn Web A?
  16. 16. What is a modn Web A?
  17. 17. What do we lk for in a database? ๏ Right structure to match my data ๏ Performance & Scale ๏ Features that enable me as a developer
  18. 18. K Queson: WHAT IS A RECORD?
  19. 19. 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
  20. 20. Raonal๏ 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
  21. 21. Documt๏ 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
  22. 22. Raonal design
  23. 23. Documt design
  24. 24. 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!" } ] }
  25. 25. What is a modn Web A?
  26. 26. What is a modn Web A?
  27. 27. MongoDB spks your ngauage ๏ Drivers in 14+ languages ๏ Interface is natural and idiomatic for each language ๏ Document natively maps to map/hash/object array/dict/struct
  28. 28. 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)
  29. 29. Inserting the record Initial Data Load > db.places.insert(place1) > db.places.insert(place1)
  30. 30. 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" ] }
  31. 31. Updating > db.places.update( {name : "10gen HQ"}, { $push : { comments : { author : "steve", date : 6/26/2013, text : "Office hours are great!" } } } )
  32. 32. 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!" } ] }
  33. 33. 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" ], }
  34. 34. Paginating Places in JS 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);
  35. 35. 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)
  36. 36. ch ftures๏ Rich query language ๏ GeoSpatial ๏ Text search ๏ Flexible schema ๏ Aggregation & MapReduce ๏ GridFS (distributed & replicated file storage) ๏ Integration with Hadoop, Storm, Solr & more
  37. 37. Database ndscape Scalability&Performance Depth of Functionality MongoDB Key Value RDBMS
  38. 38. NoSQL popu
  39. 39. NoSQL popu
  40. 40. NoSQL popu
  41. 41. What is a modn Web A?
  42. 42. What is a modn Web A?
  43. 43. Scabi Needs ๏ Data is highly available ๏ Data is consistent ๏ Performant (caching unnecessary)
  44. 44. Difft Aroaches ๏ 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
  45. 45. MongoDB : bui to scale ๏ Intelligent replication ๏ Automatic partitioning of data (user configurable) ๏ Horizontal Scale ๏ Targeted Queries ๏ Parallel Processing
  46. 46. Igt Repcaon Node 1 Secondary Node 2 Secondary Node 3 Primary Replication Heartbeat Replication
  47. 47. Scable Archecture Node 1 Secondary Config Server Node 1 Secondary Config Server Node 1 Secondary Config Server Shard Shard Shard Mongos App Server Mongos App Server Mongos App Server
  48. 48. High Avaibi in Shards Shard Primary Secondary Secondary Shard orMongod x
  49. 49. Targed Requests Shard Shard Shard Mongos 1 2 3 4
  50. 50. Pall proceßing Shard Shard Shard Mongos 1 2 2 2 4 44 3 3 3 6 5
  51. 51. What is a modn Web A?
  52. 52. e g database to t your data
  53. 53. e g database for YOUR dopmt
  54. 54. e g Database for scale & Pfoance
  55. 55. Gng staed
  56. 56. sy deploymt ๏ Heroku ๏ Rackspace ๏ Amazon ๏ Engine Yard ๏ App Fog ๏ ServerGrove ๏ Azure ๏ Nodejitsu
  57. 57. Indust acaon ๏ Media & Entertainment ๏ Retail ๏ Social ๏ Finance ๏ Gaming ๏ Insurance ๏ Healthcare ๏ Government ๏ Archiving ๏ Telecom ๏ Education
  58. 58. E IF YOU KED ! Questions? http://spf13.com http://github.com/spf13 @spf13 #DevCon5

×