CouchDBonRailsAnIntroductionJonathanWeiss03.07.2010WhoamI?WorkingforPeritorinBerlin,GermanyWritten,maintain,orinvolvedin Webistrano Capistrano SimplyStored Happening ThegreatfireofLondon@jweiss2ScalariumEC2ClusterManagement Auto-config Self-Healing Auto-Scaling One-click-deployment–documentDB67”CouchDBisbuiltoftheWeb“JacobKaplan-MossWebTechnologiesHTTPtheaccessprotocolJavaScriptthequerylanguageJSONthestorageformat8JSONDocument{_id:BCCD12CBB,_rev:1-AB764C,type:person,name:DarthVader,age:63,headware:[Helmet,Sombrero],dark_side:true,weapons:{right_arm:light_saber,left_arm:null}}9JSONDocument{_id:BCCD12CBB,_rev:1-AB764C,type:person,name:DarthVader,age:63,headware:[Helmet,Sombrero],dark_side:true,weapons:{right_arm:light_saber,left_arm:null}}10JSONDocument{_id:BCCD12CBB,_rev:1-AB764C,type:person,name:DarthVader,age:63,headware:[Helmet,Sombrero],dark_side:true,weapons:{right_arm:light_saber,left_arm:null}}11NoTablesorNamespaces12NoTablesorNamespaces13ManualNamespacing{_id:BCCD12CBB,_rev:1-AB764C,type:person,name:DarthVader,age:63,headware:[Helmet,Sombrero],dark_side:true,weapons:{right_arm:light_saber,left_arm:null}}14InteractingwithCouchDB15PUT/dbname/IDJSONHTTPClientInteractingwithCouchDB16JSONGET/dbname/IDHTTPClientInteractingwithCouchDB17DELETE/dbname/IDHTTPClient18ViewsDesignDocument{id:_design/hats”,_rev:431212AB4”,language:javascript”,views:{all:{map:function(doc){....}”,reduce:function(doc){....}”},by_manufacturer:{map:function(doc){....}”,reduce:function(doc){....}”}}}19DesignDocument{id:_design/hats”,_rev:431212AB4”,language:javascript”,views:{all:{map:function(doc){....}”,reduce:function(doc){....}”},by_manufacturer:{map:function(doc){....}”,reduce:function(doc){....}”}}}20DocumentID–prefixedby“_design/”DesignDocument{id:_design/hats”,_rev:431212AB4”,language:javascript”,views:{all:{map:function(doc){....}”,reduce:function(doc){....}”},by_manufacturer:{map:function(doc){....}”,reduce:function(doc){....}”}}}21HashofViewsDesignDocument{id:_design/hats”,_rev:431212AB4”,language:javascript”,views:{all:{map:function(doc){....}”,reduce:function(doc){....}”},by_manufacturer:{map:function(doc){....}”,reduce:function(doc){....}”}}}22HashofViewsEveryviewhasmap&reducefunctionMapfunction(doc){if(doc.headware){for(varhatindoc.headware){emit(hat,1);}}}23Mapfunction(doc){if(doc.headware){for(varhatindoc.headware){emit(hat,1);}}}24PassedeverydocumentintheDBMapfunction(doc){if(doc.headware){for(varhatindoc.headware){emit(hat,1);}}}25Inspects&DecidesMapfunction(doc){if(doc.headware){for(varhatindoc.headware){emit(hat,1);}}}26EmitsresultforindexReducefunction(keys,values,rereduce){returnsum(values);}27Reducefunction(keys,values,rereduce){returnsum(values);}28Passedmapresult(orpartialreduceresult)Reducefunction(keys,values,rereduce){returnsum(values);}29Aggregates(count,sum,average,…)ExampleMapResultMapfunctionsaresimilartoSQLindices30IDKEYVALUE51ABFA211Cap1ABC123456Cappy1BCCD12CBBHelmet1BCCD12CBBSombrero1SortedbythekeyKeycanalsobeanarrayValuecanbecomplexJSONQueryaview31GET/dbname/_design/hats/_view/allHTTPClient{total_rows:348,offset:0,rows”:[{id:A,key:A,value:1},{id:B,key:B,value:1},]}Queryaview32GET/dbname/_design/hats/_view/all?include_docs=trueHTTPClientViewQueryFilterby key=ABC123 startkey=123&endkey=9 limit=100 descending=true group=true reduce=true Include_docs=true33SQLvs.JavaScript34Vs.SQLvs.JavaScript35Vs.ActiveRecordSimplyStoredSimplyStoredConvenienceLayerforCouchDB Models&Associations Validations Callbacks Dynamicfinder S3attachments Paranoiddelete ActiveModelcompliant36BSD-licensedonfigureSetup383940RockingChairIn-memoryCouchDB JustabigHash UnderstandsallSimplyStoredgeneratedviews Speedsuptests Testscanruninparallel Nicefordebugging41BSD-licensedon(MVCC)CrashresistantHotbackupsCompaction44Replication45CouchDBReplication46POST/_replicatePOST/_replicateEventuallyconsistent&conflictresolutionLoadBalancing47HTTPClientHTTPLoadBalancerReplicationCaching48HTTPClientHTTPCacheVarnishApache…Multi-Master49Sharding/PartitioningwithCouchDBLounge50HTTPClientCouchDBLoungeShardingwithCouchDBLounge51HTTPClientCouchDBLoungeVariousCouchAppsValidationsFilteredreplicationChangesfeedFutonGeoFulltext-SearchwithembeddedLuceneExperimentalRuby-View-Server52©PeritorGmbH-AlleRechtevorbehalten PeritorGmbHBlücherstr.22,HofIIIAufgang610961BerlinTel.:+49(0)30692009840Fax:+49(0)30692009849Internet:@peritor.comQ&A