SlideShare une entreprise Scribd logo
1  sur  53
Télécharger pour lire hors ligne
Using NoSQL with Yo’ SQL
                         Supplementing your app with a slice of MongoDB



                                                             Rich Thornett
                                                             Dribbble




Thursday, June 9, 2011
Dribbble
                           What are you working on?
                         Show and tell for creatives via screenshots




Thursday, June 9, 2011
Your Father's Webapp
                         Dribbble is a typical web application:

                         Ruby on Rails + Relational Database


                                We <3 PostgreSQL
                                  But for certain tasks ...




Thursday, June 9, 2011
Alternative Values
                         log | scale | optimize | aggregate | cache

                              More flexible data structures

                                 Easier horizontal scaling




Thursday, June 9, 2011
NoSQL
                                 No == Not Only
                                 (but sounds a bit stronger, no?)


                           • No: Fixed table schemas
                           • No: Joins
                           • Yes: Scale horizontally
                                      Examples
                  Memcached, Redis, CouchDB, Cassandra, MongoDB ...




Thursday, June 9, 2011
Exploring MongoDB
            • Persistent data store
            • Powerful query language (closest to RDBMs)
            • Broad feature set
            • Great community and documentation
                            Utility belt that fits us?




Thursday, June 9, 2011
What is MongoDB?
                         A document-oriented NoSQL database

                             Collections & Documents
                                           v.
                                    Tables & Rows




Thursday, June 9, 2011
What's a document?
                                   Our old friend JavaScript
                    {
                         _id: ObjectId("4ddfe31db6bc16ab615e573d"),
                         description: "This is a BSON document",
                         embedded_doc: {
                           description: "I belong to my parent document"
                         },
                         tags: ['can', 'haz', 'arrays']
                    }


                          Documents are BSON (binary encoded JSON)



Thursday, June 9, 2011
Embedded Documents
                         Avoid joins for "belongs to" associations

                    {
                      _id: ObjectId("4ddfe31db6bc16ab615e573d"),
                      description: "This is a BSON document",
                      embedded_doc: {
                        description: "I belong to my parent document"
                      },
                      tags: ['can', 'haz', 'arrays']
                    })




Thursday, June 9, 2011
Arrays
                             Avoid joins for "tiny relations"
                    {
                      _id: ObjectId("4ddfe31db6bc16ab615e573d"),
                      description: "This is a BSON document",
                      embedded_doc: {
                        description: "I belong to my parent document"
                      },
                      tags: ['can', 'haz', 'arrays']
                    })

                                     Relational Cruft

                          thing         thing_taggings        tags

Thursday, June 9, 2011
Googley
            “With MongoDB we can ... grow our data set horizontally
             on a cluster of commodity hardware and do distributed
           (read parallel execution of) queries/updates/inserts/deletes.”

                                        --Markus Gattol
                         http://www.markus-gattol.name/ws/mongodb.html




Thursday, June 9, 2011
Replica Sets
                 Automate the storing of multiple copies of data

                    • Read Scaling
                    • Data Redundancy
                    • Automated Failover
                    • Maintenance
                    • Disaster Recovery


Thursday, June 9, 2011
Dude, who sharded?
                                       Relax, not you.


                                   Auto-sharding
                         You
                         Specify a shard key for a collection

                         Mongo
                         Partitions the collection across machines

                         Application
                         Blissfully unaware (mostly :)

Thursday, June 9, 2011
CoSQL
                                     MongoDB




                                                    Lo
                            g
                           alin
                                     WEBAPP




                                                    ggi
                         Sc




                                                        ng
                                  MIND THE APP

                                      RDBMS
                         An




                                                     ing
                           aly




                                                   ch
                              tic




                                                   Ca
                                 s




                                     Flexibility
Thursday, June 9, 2011
Ads
          • Orthogonal to primary app
          • Few joins
          • Integrity not critical

                              Let's Mongo!



Thursday, June 9, 2011
From the Console
                         But there are drivers for all major languages
                                       Create a text ad
                     db.ads.insert({
                       advertiser_id: 1,
                       type: 'text',
                       url: 'http://dribbbler-on-the-roof.com',
                       copy: 'Watch me!',
                       runs: [{
                         start: new Date(2011, 4, 7),
                         end: new Date(2011, 4, 14)
                       }],
                       created_at: new Date()
                     })

Thursday, June 9, 2011
Querying
                                   Query by match
                   db.ads.find({advertiser_id: 1})


                                  Paging active ads
                  // Page 2 of text ads running this month
                  db.ads.find({
                   type: 'text',
                    runs: {
                      $elemMatch: {
                        start: {$lte: new Date(2011, 4, 10)},
                        end: {$gte: new Date(2011, 4, 10)}
                      }
                    }
                  }).sort({created_at: -1}).skip(15).limit(15)
Thursday, June 9, 2011
Advanced Queries
                         http://www.mongodb.org/display/DOCS/Advanced+Queries

                             $gt                  $mod              $size
                             $lt                  $ne               $type
                             $gte                 $in               $elemMatch
                             $lte                 $nin              $not
                             $all                 $nor              $where
                             $exists              $or

                                      count | distinct | group
                         Group does not work across shards, use map/reduce instead.



Thursday, June 9, 2011
Polymorphism
               Easy inheritance. Document has whatever fields it needs.
                                // Banner ad has additional fields
                                db.ads.insert({
                                   advertiser_id: 1,
                                   type: 'banner',
                                   url: 'http://dribbble-me-this.com',
                                   copy: 'Buy me!',
                                   runs: [],
                                   image_file_name: 'ad.png',
                                   image_content_type: 'image/png',
                                   image_file_size: '33333'
                                })

                                      Single | Multiple | Joined
                              table inheritance all present difficulties

                         No DB changes to create new subclasses in Mongo
Thursday, June 9, 2011
Logging
                         • Scale and query horizontally
                         • Add fields on the fly
                         • Writes: Fast, asynchronous, atomic




Thursday, June 9, 2011
Volume Logging
                              • Ad impressions
                              • Screenshot views
                              • Profile views

                    Fast, asynchronous writes and sharding FTW!




Thursday, June 9, 2011
Real-time Analytics
                     What people and locations are trending this hour?
          db.trends.update(
            {date: "2011-04-10 13:00"},        // search criteria
            {
               $inc: {                         // increment
                 'user.simplebits.likes_received': 1,
                 'country.us.likes_received': 1,
                 'city.boston.likes_received': 1
               }
            },
            true                               // upsert
          )

            upsert: Update document (if present) or insert it
            $inc: Increment field by amount (if present) or set to amount

Thursday, June 9, 2011
Flex Benefits
     • Add/nest new fields to measure with ease
     • Atomic upsert with $inc
     Replaces two-step, transactional find-and-update/create

     • Live, cached aggregation



Thursday, June 9, 2011
Scouting




Thursday, June 9, 2011
Design a Designer
            db.users.insert(
              { name: 'Dan Cederholm',
                available: true,
                skills: ['html', 'css', 'illustration', 'icon design'] }
            )




Thursday, June 9, 2011
Geospatial Indexing
            db.users.ensureIndex({location: '2d'})
            db.users.insert(
              { name: 'Dan Cederholm',
                // Salem longitude/latitude
                location: [-70.8972222, 42.5194444],
                available: true,
                skills: ['html', 'css', 'illustration', 'icon design'] }
            )




Thursday, June 9, 2011
Search by Location
               boston = [-71.0602778, 42.3583333] // long/lat

                                  Within area
    // $maxDistance: Find users in Boston area (w/in 50 miles)
    db.users.find({location: {$near: boston, $maxDistance: 0.7234842}})



                          Within area, matching criteria
       // Find users in the Boston area who:
       //    are available for work
       //    have expertise in HTML and icon design
       db.users.find({
          location: {$near: boston, $maxDistance: .7234842},
          available: true,
          skills: {$all: ['html', 'icon design']}
       })
Thursday, June 9, 2011
Search Power
                         Flexible Documents
                                   +
                         Rich Query Language
                                   +
                          Geospatial Indexing




Thursday, June 9, 2011
Stats




Thursday, June 9, 2011
Unique Views
                                a.k.a visitors per day




                         unique = remote_ip address / DAY




Thursday, June 9, 2011
Map/Reduce
                          http://www.mongodb.org/display/DOCS/MapReduce


                         Aggregate by key => GROUP BY in SQL


                          Collections
                          Input and output

                          Map
                          Returns 0..N key/value pairs per document

                          Reduce
                          Aggregates values per key

Thursday, June 9, 2011
Strategy
                         Two-pass map/reduce to calculate unique visitors

                              Pass 1
                              GROUP BY: profile, visitor
                              COUNT: visits per visitor per profile

                              Pass 2
                              GROUP BY: profile
                              COUNT: visitors




Thursday, June 9, 2011
Profile View Data
                                   Visits on a given day

                         // Profile 1
                         {profile_id: 1, remote_ip: '127.0.0.1'}
                         {profile_id: 1, remote_ip: '127.0.0.1'}
                         {profile_id: 1, remote_ip: '127.0.0.2'}

                         // Profile 2
                         {profile_id: 2, remote_ip: '127.0.0.4'}
                         {profile_id: 2, remote_ip: '127.0.0.4'}




Thursday, June 9, 2011
Pass 1: Map Function
                           Count visits per remote_ip per profile
                                KEY = profile, remote_ip

                           map = function() {
                              var key = {
                               profile_id: this.profile_id,
                               remote_ip: this.remote_ip
                             };

                               emit(key, {count: 1});
                           }



Thursday, June 9, 2011
Reduce Function
                                        Counts
                                   (occurrences of key)

                         reduce = function(key, values) {
                           var count = 0;

                             values.forEach(function(v) {
                               count += v.count;
                             });

                             return {count: count};
                         }

Thursday, June 9, 2011
Pass 1: Run Map/Reduce
                         Count visits per remote_ip per profile

   db.profile_views.mapReduce(map, reduce,
     {out: 'profile_views_by_visitor'}
   )

   // Results: Unique visitors per profile
   db.profile_views_by_visitor.find()
   { "_id": { "profile_id": 1, "remote_ip": "127.0.0.1" }, "value": { "count": 2 } }
   { "_id": { "profile_id": 1, "remote_ip": "127.0.0.2" }, "value": { "count": 1 } }
   { "_id": { "profile_id": 2, "remote_ip": "127.0.0.4" }, "value": { "count": 1 } }




Thursday, June 9, 2011
Pass 2: Map/Reduce
                                Count visitors per profile
                                   KEY = profile_id

                         map = function() {
                           emit(this._id.profile_id, {count: 1});
                         }




Thursday, June 9, 2011
Pass 2: Results
                          Count visitors per profile
        // Same reduce function as before
        db.profile_views_by_visitor.mapReduce(map, reduce,
          {out: 'profile_views_unique'}
        )

        // Results
        db.profile_views_unique.find()
        { "_id" : 1, "value" : { "count" : 2 } }
        { "_id" : 2, "value" : { "count" : 1 } }




Thursday, June 9, 2011
Map/Deduce
                 Can be clunkier than GROUP BY in SQL. But ...

                           Large data sets, you get:
                            • Horizontal scaling
                            • Parallel processing across cluster
                         JavaScript functions offers flexibility/power




Thursday, June 9, 2011
Activity
                            SELECT * FROM everything;




                         Too many tables to JOIN or UNION
Thursday, June 9, 2011
Relational solution
                         Denormalized events table as activity log.
                         Column         |            Type             |
                ------------------------+-----------------------------+
                 id                     | integer                     |
                 event_type             | character varying(255)      |
                 subject_type           | character varying(255)      |
                 actor_type             | character varying(255)      |
                 secondary_subject_type | character varying(255)      |
                 subject_id             | integer                     |
                 actor_id               | integer                     |
                 secondary_subject_id   | integer                     |
                 recipient_id           | integer                     |
                 secondary_recipient_id | integer                     |
                 created_at             | timestamp without time zone |

                          We use James Golick’s timeline_fu gem for Rails:
                            https://github.com/jamesgolick/timeline_fu
Thursday, June 9, 2011
Direction
                   Incoming Activity   Generated Activity
                      (recipients)         (actors)




Thursday, June 9, 2011
Complications
              Multiple recipients
              • Subscribe to comments for a shot
              • Twitter-style @ mentions in comments

              Confusing names
              • Generic names make queries and view logic hard to follow

              N+1
              • Each event may require several lookups to get actor, subject, etc




Thursday, June 9, 2011
Events in Mongo
                         Comment on a Screenshot containing an @ mention
                         Screenshot owner and @user should be recipients.




                         Mongo version of our timeline_events table
                           {
                               event_type: "created",
                               subject_type: "Comment",
                               actor_type: "User",
                               subject_id: 999,
                               actor_id: 1,
                               recipients: [], // Multiple recipients
                               secondary_recipient_id: 3,
                               created_at: "Wed May 05 2010 15:37:58 GMT-0400 (EDT)"
                           }

Thursday, June 9, 2011
Mongo Event v.2

                                     Why is a user a recipient?
                         {
                             event_type: "created",
                             subject_type: "Comment",
                             actor_type: "User",
                             subject_id: 999,
                             actor_id: 1,
                             recipients: [1, 2],
                             recipients: [
                                {user_id: 2, reason: 'screenshot owner'},
                                {user_id: 3, reason: 'mention'}
                             ],
                             created_at: "Wed May 05 2010 15:37:58 GMT-0400 (EDT)"
                         }


Thursday, June 9, 2011
Mongo Event v.3

                                           Meaningful names
                         {
                             event_type: "created",
                             subject_type: "Comment",
                             actor_type: "User",
                             subject_id: 999,
                             actor_id: 1
                             user_id: 1,
                             comment_id 999,
                             screenshot_id: 555,
                             recipients: [
                                {user_id: 2, reason: 'screenshot owner'},
                                {user_id: 3, reason: 'mention'}
                             ],
                             created_at: "Wed May 05 2010 15:37:58 GMT-0400 (EDT)"
                         }
Thursday, June 9, 2011
Mongo Event v.4

                             Denormalize to eliminate N+1s in view
                         {
                             event_type: "created",
                             subject_type: "Comment",
                             user_id: 1,
                             comment_id: 999,
                             screenshot_id: 999,
                             user: {id: 1, login: "simplebits", avatar: "dancederholm-peek.png"},
                             comment: {id: 999, text: "Great shot!”},
                             screenshot: {id: 555, title: "Shot heard around the world"},
                             recipients: [
                                {user_id: 2, reason: 'screenshot owner'},
                                {user_id: 3, reason: 'mention'}
                             ],
                             created_at: "Wed May 05 2010 15:37:58 GMT-0400 (EDT)"
                         }
Thursday, June 9, 2011
Denormalizing?
                    You're giving up RDBMs benefits to optimize.

                     Optimize your optimizations.
                                Document flexibility:
                         Data structures can mirror the view




Thursday, June 9, 2011
Caching
                            http://www.mongodb.org/display/DOCS/Caching

                         MongoDB uses memory-mapped files

            • Grabs free memory as needed; no configured cache size
            • Relies on OS to reclaim memory (LRU)




Thursday, June 9, 2011
Replace Redis/Memcached?
              FREQUENTLY accessed items LIKELY in memory

                         Good enough for you?
                          One less moving part.




Thursday, June 9, 2011
Cache Namespaces
                         'ad_1'
                                         Memcached keys are flat
                         'ad_2'
                         'ad_3'          No simple way to expire all

                                     Collection
                         // Clear collection to expire
                         db.ads_cache.remove()

                         can serve as an expirable namespace



Thursday, June 9, 2011
Time to Mongo?
                                        Versatility?

               Data structure flexibility worth more than joins?

                                Easier horizontal scaling?


                         log | scale | optimize | aggregate | cache
                                http://www.mongodb.org

Thursday, June 9, 2011
Cheers!

                         Rich Thornett
                               Dribbble
                         http://dribbble.com

                           @frogandcode


Thursday, June 9, 2011

Contenu connexe

En vedette

Why work experience can change your life
Why work experience can change your lifeWhy work experience can change your life
Why work experience can change your lifeGraduates Yorkshire
 
GH and Toyota
GH and ToyotaGH and Toyota
GH and ToyotaGolin
 
Communities in schools & the elementary campus
Communities in schools & the elementary campusCommunities in schools & the elementary campus
Communities in schools & the elementary campusJan Seiter
 
KEDWIBAHASAAN/BILINGUALISM/Pemilihan Bahasa Indonesia sebagai Bahasa Ibu (B1)...
KEDWIBAHASAAN/BILINGUALISM/Pemilihan Bahasa Indonesia sebagai Bahasa Ibu (B1)...KEDWIBAHASAAN/BILINGUALISM/Pemilihan Bahasa Indonesia sebagai Bahasa Ibu (B1)...
KEDWIBAHASAAN/BILINGUALISM/Pemilihan Bahasa Indonesia sebagai Bahasa Ibu (B1)...Baren Barnabas
 
Audiência por Day Parts | Crianças e Adolescentes
Audiência por Day Parts | Crianças e AdolescentesAudiência por Day Parts | Crianças e Adolescentes
Audiência por Day Parts | Crianças e AdolescentesMichelle Sesterrem
 
Making Informed Choices That Move Us Beyond Our Default Future
Making Informed Choices That Move Us Beyond Our Default Future Making Informed Choices That Move Us Beyond Our Default Future
Making Informed Choices That Move Us Beyond Our Default Future Formicio
 
The digitalgenerationishere v5__4pager
The digitalgenerationishere v5__4pagerThe digitalgenerationishere v5__4pager
The digitalgenerationishere v5__4pagerMustafa Kuğu
 
進化ゲーム理論の枠組みを用いたソーシャルゲームにおけるユーザの利他的行動の分析
進化ゲーム理論の枠組みを用いたソーシャルゲームにおけるユーザの利他的行動の分析進化ゲーム理論の枠組みを用いたソーシャルゲームにおけるユーザの利他的行動の分析
進化ゲーム理論の枠組みを用いたソーシャルゲームにおけるユーザの利他的行動の分析Masanori Takano
 
Summit Struggles In Light Of 21 Ordinance
Summit Struggles In Light Of 21 OrdinanceSummit Struggles In Light Of 21 Ordinance
Summit Struggles In Light Of 21 Ordinancedustinmcaninch
 
Mastering Disasters - Velocity Ignite 2013 New York
Mastering Disasters - Velocity Ignite 2013 New YorkMastering Disasters - Velocity Ignite 2013 New York
Mastering Disasters - Velocity Ignite 2013 New YorkCatchpoint Systems
 
Flex Open Platform - Polycom
Flex Open Platform - PolycomFlex Open Platform - Polycom
Flex Open Platform - PolycomDevis Balsemin
 

En vedette (18)

Why work experience can change your life
Why work experience can change your lifeWhy work experience can change your life
Why work experience can change your life
 
GH and Toyota
GH and ToyotaGH and Toyota
GH and Toyota
 
Communities in schools & the elementary campus
Communities in schools & the elementary campusCommunities in schools & the elementary campus
Communities in schools & the elementary campus
 
Marketing without Money
Marketing without MoneyMarketing without Money
Marketing without Money
 
EBS_Digest_Issue4
EBS_Digest_Issue4EBS_Digest_Issue4
EBS_Digest_Issue4
 
KEDWIBAHASAAN/BILINGUALISM/Pemilihan Bahasa Indonesia sebagai Bahasa Ibu (B1)...
KEDWIBAHASAAN/BILINGUALISM/Pemilihan Bahasa Indonesia sebagai Bahasa Ibu (B1)...KEDWIBAHASAAN/BILINGUALISM/Pemilihan Bahasa Indonesia sebagai Bahasa Ibu (B1)...
KEDWIBAHASAAN/BILINGUALISM/Pemilihan Bahasa Indonesia sebagai Bahasa Ibu (B1)...
 
Ad hoc Networks
Ad hoc NetworksAd hoc Networks
Ad hoc Networks
 
Romania
RomaniaRomania
Romania
 
Audiência por Day Parts | Crianças e Adolescentes
Audiência por Day Parts | Crianças e AdolescentesAudiência por Day Parts | Crianças e Adolescentes
Audiência por Day Parts | Crianças e Adolescentes
 
Social students
Social studentsSocial students
Social students
 
Making Informed Choices That Move Us Beyond Our Default Future
Making Informed Choices That Move Us Beyond Our Default Future Making Informed Choices That Move Us Beyond Our Default Future
Making Informed Choices That Move Us Beyond Our Default Future
 
The digitalgenerationishere v5__4pager
The digitalgenerationishere v5__4pagerThe digitalgenerationishere v5__4pager
The digitalgenerationishere v5__4pager
 
進化ゲーム理論の枠組みを用いたソーシャルゲームにおけるユーザの利他的行動の分析
進化ゲーム理論の枠組みを用いたソーシャルゲームにおけるユーザの利他的行動の分析進化ゲーム理論の枠組みを用いたソーシャルゲームにおけるユーザの利他的行動の分析
進化ゲーム理論の枠組みを用いたソーシャルゲームにおけるユーザの利他的行動の分析
 
Summit Struggles In Light Of 21 Ordinance
Summit Struggles In Light Of 21 OrdinanceSummit Struggles In Light Of 21 Ordinance
Summit Struggles In Light Of 21 Ordinance
 
SugarCRM - What's New in Version 7.7
SugarCRM - What's New in Version 7.7SugarCRM - What's New in Version 7.7
SugarCRM - What's New in Version 7.7
 
Mastering Disasters - Velocity Ignite 2013 New York
Mastering Disasters - Velocity Ignite 2013 New YorkMastering Disasters - Velocity Ignite 2013 New York
Mastering Disasters - Velocity Ignite 2013 New York
 
Folhas anatomia
Folhas   anatomiaFolhas   anatomia
Folhas anatomia
 
Flex Open Platform - Polycom
Flex Open Platform - PolycomFlex Open Platform - Polycom
Flex Open Platform - Polycom
 

Similaire à Using NoSQL with Yo' SQL

Flowdock's full-text search with MongoDB
Flowdock's full-text search with MongoDBFlowdock's full-text search with MongoDB
Flowdock's full-text search with MongoDBFlowdock
 
Django and Neo4j - Domain modeling that kicks ass
Django and Neo4j - Domain modeling that kicks assDjango and Neo4j - Domain modeling that kicks ass
Django and Neo4j - Domain modeling that kicks assTobias Lindaaker
 
Mongo db with spring data document
Mongo db with spring data documentMongo db with spring data document
Mongo db with spring data documentSean Lee
 
CloudFoundry and MongoDb, a marriage made in heaven
CloudFoundry and MongoDb, a marriage made in heavenCloudFoundry and MongoDb, a marriage made in heaven
CloudFoundry and MongoDb, a marriage made in heavenPatrick Chanezon
 
Jumpstart: Building Your First MongoDB App
Jumpstart: Building Your First MongoDB AppJumpstart: Building Your First MongoDB App
Jumpstart: Building Your First MongoDB AppMongoDB
 
MongoDB presentation
MongoDB presentationMongoDB presentation
MongoDB presentationHyphen Call
 
MongoDB at Sailthru: Scaling and Schema Design
MongoDB at Sailthru: Scaling and Schema DesignMongoDB at Sailthru: Scaling and Schema Design
MongoDB at Sailthru: Scaling and Schema DesignDATAVERSITY
 
Connecting to a REST API in iOS
Connecting to a REST API in iOSConnecting to a REST API in iOS
Connecting to a REST API in iOSgillygize
 
Polyglot Database - Linuxcon North America 2016
Polyglot Database - Linuxcon North America 2016Polyglot Database - Linuxcon North America 2016
Polyglot Database - Linuxcon North America 2016Dave Stokes
 
No SQL - MongoDB
No SQL - MongoDBNo SQL - MongoDB
No SQL - MongoDBMirza Asif
 
nosql [Autosaved].pptx
nosql [Autosaved].pptxnosql [Autosaved].pptx
nosql [Autosaved].pptxIndrani Sen
 
Introducing Mongo DB and setting up Adobe AEM6 with mongo
Introducing Mongo DB and setting up Adobe AEM6 with mongoIntroducing Mongo DB and setting up Adobe AEM6 with mongo
Introducing Mongo DB and setting up Adobe AEM6 with mongoYash Mody
 
MongoDB Lab Manual (1).pdf used in data science
MongoDB Lab Manual (1).pdf used in data scienceMongoDB Lab Manual (1).pdf used in data science
MongoDB Lab Manual (1).pdf used in data sciencebitragowthamkumar1
 
Big Data Israel Meetup : Couchbase and Big Data
Big Data Israel Meetup : Couchbase and Big DataBig Data Israel Meetup : Couchbase and Big Data
Big Data Israel Meetup : Couchbase and Big DataTugdual Grall
 
MongoDB Introduction and Data Modelling
MongoDB Introduction and Data Modelling MongoDB Introduction and Data Modelling
MongoDB Introduction and Data Modelling Sachin Bhosale
 

Similaire à Using NoSQL with Yo' SQL (20)

Flowdock's full-text search with MongoDB
Flowdock's full-text search with MongoDBFlowdock's full-text search with MongoDB
Flowdock's full-text search with MongoDB
 
Django and Neo4j - Domain modeling that kicks ass
Django and Neo4j - Domain modeling that kicks assDjango and Neo4j - Domain modeling that kicks ass
Django and Neo4j - Domain modeling that kicks ass
 
Mongo db with spring data document
Mongo db with spring data documentMongo db with spring data document
Mongo db with spring data document
 
CloudFoundry and MongoDb, a marriage made in heaven
CloudFoundry and MongoDb, a marriage made in heavenCloudFoundry and MongoDb, a marriage made in heaven
CloudFoundry and MongoDb, a marriage made in heaven
 
Lokijs
LokijsLokijs
Lokijs
 
On no sql.partiii
On no sql.partiiiOn no sql.partiii
On no sql.partiii
 
Jumpstart: Building Your First MongoDB App
Jumpstart: Building Your First MongoDB AppJumpstart: Building Your First MongoDB App
Jumpstart: Building Your First MongoDB App
 
MongoDB presentation
MongoDB presentationMongoDB presentation
MongoDB presentation
 
MongoDB at Sailthru: Scaling and Schema Design
MongoDB at Sailthru: Scaling and Schema DesignMongoDB at Sailthru: Scaling and Schema Design
MongoDB at Sailthru: Scaling and Schema Design
 
Connecting to a REST API in iOS
Connecting to a REST API in iOSConnecting to a REST API in iOS
Connecting to a REST API in iOS
 
Polyglot Database - Linuxcon North America 2016
Polyglot Database - Linuxcon North America 2016Polyglot Database - Linuxcon North America 2016
Polyglot Database - Linuxcon North America 2016
 
Node at artsy
Node at artsyNode at artsy
Node at artsy
 
No SQL - MongoDB
No SQL - MongoDBNo SQL - MongoDB
No SQL - MongoDB
 
Everyday - mongodb
Everyday - mongodbEveryday - mongodb
Everyday - mongodb
 
nosql [Autosaved].pptx
nosql [Autosaved].pptxnosql [Autosaved].pptx
nosql [Autosaved].pptx
 
Introducing Mongo DB and setting up Adobe AEM6 with mongo
Introducing Mongo DB and setting up Adobe AEM6 with mongoIntroducing Mongo DB and setting up Adobe AEM6 with mongo
Introducing Mongo DB and setting up Adobe AEM6 with mongo
 
MongoDB Lab Manual (1).pdf used in data science
MongoDB Lab Manual (1).pdf used in data scienceMongoDB Lab Manual (1).pdf used in data science
MongoDB Lab Manual (1).pdf used in data science
 
Big Data Israel Meetup : Couchbase and Big Data
Big Data Israel Meetup : Couchbase and Big DataBig Data Israel Meetup : Couchbase and Big Data
Big Data Israel Meetup : Couchbase and Big Data
 
Mongodb
MongodbMongodb
Mongodb
 
MongoDB Introduction and Data Modelling
MongoDB Introduction and Data Modelling MongoDB Introduction and Data Modelling
MongoDB Introduction and Data Modelling
 

Dernier

Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfhans926745
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessPixlogix Infotech
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 

Dernier (20)

Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 

Using NoSQL with Yo' SQL

  • 1. Using NoSQL with Yo’ SQL Supplementing your app with a slice of MongoDB Rich Thornett Dribbble Thursday, June 9, 2011
  • 2. Dribbble What are you working on? Show and tell for creatives via screenshots Thursday, June 9, 2011
  • 3. Your Father's Webapp Dribbble is a typical web application: Ruby on Rails + Relational Database We <3 PostgreSQL But for certain tasks ... Thursday, June 9, 2011
  • 4. Alternative Values log | scale | optimize | aggregate | cache More flexible data structures Easier horizontal scaling Thursday, June 9, 2011
  • 5. NoSQL No == Not Only (but sounds a bit stronger, no?) • No: Fixed table schemas • No: Joins • Yes: Scale horizontally Examples Memcached, Redis, CouchDB, Cassandra, MongoDB ... Thursday, June 9, 2011
  • 6. Exploring MongoDB • Persistent data store • Powerful query language (closest to RDBMs) • Broad feature set • Great community and documentation Utility belt that fits us? Thursday, June 9, 2011
  • 7. What is MongoDB? A document-oriented NoSQL database Collections & Documents v. Tables & Rows Thursday, June 9, 2011
  • 8. What's a document? Our old friend JavaScript { _id: ObjectId("4ddfe31db6bc16ab615e573d"), description: "This is a BSON document", embedded_doc: { description: "I belong to my parent document" }, tags: ['can', 'haz', 'arrays'] } Documents are BSON (binary encoded JSON) Thursday, June 9, 2011
  • 9. Embedded Documents Avoid joins for "belongs to" associations { _id: ObjectId("4ddfe31db6bc16ab615e573d"), description: "This is a BSON document", embedded_doc: { description: "I belong to my parent document" }, tags: ['can', 'haz', 'arrays'] }) Thursday, June 9, 2011
  • 10. Arrays Avoid joins for "tiny relations" { _id: ObjectId("4ddfe31db6bc16ab615e573d"), description: "This is a BSON document", embedded_doc: { description: "I belong to my parent document" }, tags: ['can', 'haz', 'arrays'] }) Relational Cruft thing thing_taggings tags Thursday, June 9, 2011
  • 11. Googley “With MongoDB we can ... grow our data set horizontally on a cluster of commodity hardware and do distributed (read parallel execution of) queries/updates/inserts/deletes.” --Markus Gattol http://www.markus-gattol.name/ws/mongodb.html Thursday, June 9, 2011
  • 12. Replica Sets Automate the storing of multiple copies of data • Read Scaling • Data Redundancy • Automated Failover • Maintenance • Disaster Recovery Thursday, June 9, 2011
  • 13. Dude, who sharded? Relax, not you. Auto-sharding You Specify a shard key for a collection Mongo Partitions the collection across machines Application Blissfully unaware (mostly :) Thursday, June 9, 2011
  • 14. CoSQL MongoDB Lo g alin WEBAPP ggi Sc ng MIND THE APP RDBMS An ing aly ch tic Ca s Flexibility Thursday, June 9, 2011
  • 15. Ads • Orthogonal to primary app • Few joins • Integrity not critical Let's Mongo! Thursday, June 9, 2011
  • 16. From the Console But there are drivers for all major languages Create a text ad db.ads.insert({ advertiser_id: 1, type: 'text', url: 'http://dribbbler-on-the-roof.com', copy: 'Watch me!', runs: [{ start: new Date(2011, 4, 7), end: new Date(2011, 4, 14) }], created_at: new Date() }) Thursday, June 9, 2011
  • 17. Querying Query by match db.ads.find({advertiser_id: 1}) Paging active ads // Page 2 of text ads running this month db.ads.find({ type: 'text', runs: { $elemMatch: { start: {$lte: new Date(2011, 4, 10)}, end: {$gte: new Date(2011, 4, 10)} } } }).sort({created_at: -1}).skip(15).limit(15) Thursday, June 9, 2011
  • 18. Advanced Queries http://www.mongodb.org/display/DOCS/Advanced+Queries $gt $mod $size $lt $ne $type $gte $in $elemMatch $lte $nin $not $all $nor $where $exists $or count | distinct | group Group does not work across shards, use map/reduce instead. Thursday, June 9, 2011
  • 19. Polymorphism Easy inheritance. Document has whatever fields it needs. // Banner ad has additional fields db.ads.insert({ advertiser_id: 1, type: 'banner', url: 'http://dribbble-me-this.com', copy: 'Buy me!', runs: [], image_file_name: 'ad.png', image_content_type: 'image/png', image_file_size: '33333' }) Single | Multiple | Joined table inheritance all present difficulties No DB changes to create new subclasses in Mongo Thursday, June 9, 2011
  • 20. Logging • Scale and query horizontally • Add fields on the fly • Writes: Fast, asynchronous, atomic Thursday, June 9, 2011
  • 21. Volume Logging • Ad impressions • Screenshot views • Profile views Fast, asynchronous writes and sharding FTW! Thursday, June 9, 2011
  • 22. Real-time Analytics What people and locations are trending this hour? db.trends.update( {date: "2011-04-10 13:00"}, // search criteria { $inc: { // increment 'user.simplebits.likes_received': 1, 'country.us.likes_received': 1, 'city.boston.likes_received': 1 } }, true // upsert ) upsert: Update document (if present) or insert it $inc: Increment field by amount (if present) or set to amount Thursday, June 9, 2011
  • 23. Flex Benefits • Add/nest new fields to measure with ease • Atomic upsert with $inc Replaces two-step, transactional find-and-update/create • Live, cached aggregation Thursday, June 9, 2011
  • 25. Design a Designer db.users.insert( { name: 'Dan Cederholm', available: true, skills: ['html', 'css', 'illustration', 'icon design'] } ) Thursday, June 9, 2011
  • 26. Geospatial Indexing db.users.ensureIndex({location: '2d'}) db.users.insert( { name: 'Dan Cederholm', // Salem longitude/latitude location: [-70.8972222, 42.5194444], available: true, skills: ['html', 'css', 'illustration', 'icon design'] } ) Thursday, June 9, 2011
  • 27. Search by Location boston = [-71.0602778, 42.3583333] // long/lat Within area // $maxDistance: Find users in Boston area (w/in 50 miles) db.users.find({location: {$near: boston, $maxDistance: 0.7234842}}) Within area, matching criteria // Find users in the Boston area who: // are available for work // have expertise in HTML and icon design db.users.find({ location: {$near: boston, $maxDistance: .7234842}, available: true, skills: {$all: ['html', 'icon design']} }) Thursday, June 9, 2011
  • 28. Search Power Flexible Documents + Rich Query Language + Geospatial Indexing Thursday, June 9, 2011
  • 30. Unique Views a.k.a visitors per day unique = remote_ip address / DAY Thursday, June 9, 2011
  • 31. Map/Reduce http://www.mongodb.org/display/DOCS/MapReduce Aggregate by key => GROUP BY in SQL Collections Input and output Map Returns 0..N key/value pairs per document Reduce Aggregates values per key Thursday, June 9, 2011
  • 32. Strategy Two-pass map/reduce to calculate unique visitors Pass 1 GROUP BY: profile, visitor COUNT: visits per visitor per profile Pass 2 GROUP BY: profile COUNT: visitors Thursday, June 9, 2011
  • 33. Profile View Data Visits on a given day // Profile 1 {profile_id: 1, remote_ip: '127.0.0.1'} {profile_id: 1, remote_ip: '127.0.0.1'} {profile_id: 1, remote_ip: '127.0.0.2'} // Profile 2 {profile_id: 2, remote_ip: '127.0.0.4'} {profile_id: 2, remote_ip: '127.0.0.4'} Thursday, June 9, 2011
  • 34. Pass 1: Map Function Count visits per remote_ip per profile KEY = profile, remote_ip map = function() { var key = { profile_id: this.profile_id, remote_ip: this.remote_ip }; emit(key, {count: 1}); } Thursday, June 9, 2011
  • 35. Reduce Function Counts (occurrences of key) reduce = function(key, values) { var count = 0; values.forEach(function(v) { count += v.count; }); return {count: count}; } Thursday, June 9, 2011
  • 36. Pass 1: Run Map/Reduce Count visits per remote_ip per profile db.profile_views.mapReduce(map, reduce, {out: 'profile_views_by_visitor'} ) // Results: Unique visitors per profile db.profile_views_by_visitor.find() { "_id": { "profile_id": 1, "remote_ip": "127.0.0.1" }, "value": { "count": 2 } } { "_id": { "profile_id": 1, "remote_ip": "127.0.0.2" }, "value": { "count": 1 } } { "_id": { "profile_id": 2, "remote_ip": "127.0.0.4" }, "value": { "count": 1 } } Thursday, June 9, 2011
  • 37. Pass 2: Map/Reduce Count visitors per profile KEY = profile_id map = function() { emit(this._id.profile_id, {count: 1}); } Thursday, June 9, 2011
  • 38. Pass 2: Results Count visitors per profile // Same reduce function as before db.profile_views_by_visitor.mapReduce(map, reduce, {out: 'profile_views_unique'} ) // Results db.profile_views_unique.find() { "_id" : 1, "value" : { "count" : 2 } } { "_id" : 2, "value" : { "count" : 1 } } Thursday, June 9, 2011
  • 39. Map/Deduce Can be clunkier than GROUP BY in SQL. But ... Large data sets, you get: • Horizontal scaling • Parallel processing across cluster JavaScript functions offers flexibility/power Thursday, June 9, 2011
  • 40. Activity SELECT * FROM everything; Too many tables to JOIN or UNION Thursday, June 9, 2011
  • 41. Relational solution Denormalized events table as activity log. Column | Type | ------------------------+-----------------------------+ id | integer | event_type | character varying(255) | subject_type | character varying(255) | actor_type | character varying(255) | secondary_subject_type | character varying(255) | subject_id | integer | actor_id | integer | secondary_subject_id | integer | recipient_id | integer | secondary_recipient_id | integer | created_at | timestamp without time zone | We use James Golick’s timeline_fu gem for Rails: https://github.com/jamesgolick/timeline_fu Thursday, June 9, 2011
  • 42. Direction Incoming Activity Generated Activity (recipients) (actors) Thursday, June 9, 2011
  • 43. Complications Multiple recipients • Subscribe to comments for a shot • Twitter-style @ mentions in comments Confusing names • Generic names make queries and view logic hard to follow N+1 • Each event may require several lookups to get actor, subject, etc Thursday, June 9, 2011
  • 44. Events in Mongo Comment on a Screenshot containing an @ mention Screenshot owner and @user should be recipients. Mongo version of our timeline_events table { event_type: "created", subject_type: "Comment", actor_type: "User", subject_id: 999, actor_id: 1, recipients: [], // Multiple recipients secondary_recipient_id: 3, created_at: "Wed May 05 2010 15:37:58 GMT-0400 (EDT)" } Thursday, June 9, 2011
  • 45. Mongo Event v.2 Why is a user a recipient? { event_type: "created", subject_type: "Comment", actor_type: "User", subject_id: 999, actor_id: 1, recipients: [1, 2], recipients: [ {user_id: 2, reason: 'screenshot owner'}, {user_id: 3, reason: 'mention'} ], created_at: "Wed May 05 2010 15:37:58 GMT-0400 (EDT)" } Thursday, June 9, 2011
  • 46. Mongo Event v.3 Meaningful names { event_type: "created", subject_type: "Comment", actor_type: "User", subject_id: 999, actor_id: 1 user_id: 1, comment_id 999, screenshot_id: 555, recipients: [ {user_id: 2, reason: 'screenshot owner'}, {user_id: 3, reason: 'mention'} ], created_at: "Wed May 05 2010 15:37:58 GMT-0400 (EDT)" } Thursday, June 9, 2011
  • 47. Mongo Event v.4 Denormalize to eliminate N+1s in view { event_type: "created", subject_type: "Comment", user_id: 1, comment_id: 999, screenshot_id: 999, user: {id: 1, login: "simplebits", avatar: "dancederholm-peek.png"}, comment: {id: 999, text: "Great shot!”}, screenshot: {id: 555, title: "Shot heard around the world"}, recipients: [ {user_id: 2, reason: 'screenshot owner'}, {user_id: 3, reason: 'mention'} ], created_at: "Wed May 05 2010 15:37:58 GMT-0400 (EDT)" } Thursday, June 9, 2011
  • 48. Denormalizing? You're giving up RDBMs benefits to optimize. Optimize your optimizations. Document flexibility: Data structures can mirror the view Thursday, June 9, 2011
  • 49. Caching http://www.mongodb.org/display/DOCS/Caching MongoDB uses memory-mapped files • Grabs free memory as needed; no configured cache size • Relies on OS to reclaim memory (LRU) Thursday, June 9, 2011
  • 50. Replace Redis/Memcached? FREQUENTLY accessed items LIKELY in memory Good enough for you? One less moving part. Thursday, June 9, 2011
  • 51. Cache Namespaces 'ad_1' Memcached keys are flat 'ad_2' 'ad_3' No simple way to expire all Collection // Clear collection to expire db.ads_cache.remove() can serve as an expirable namespace Thursday, June 9, 2011
  • 52. Time to Mongo? Versatility? Data structure flexibility worth more than joins? Easier horizontal scaling? log | scale | optimize | aggregate | cache http://www.mongodb.org Thursday, June 9, 2011
  • 53. Cheers! Rich Thornett Dribbble http://dribbble.com @frogandcode Thursday, June 9, 2011