Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.



Published on

Published in: Technology, Business
  • Be the first to comment

  • Be the first to like this


  1. 1. Developing With Documents:Schemas and Relationships Chris Anderson Architect 11
  2. 2. What we’ll talk about• Working with JSON documents• Runtime-driven patterns for• linking between documents• embedding data• fetching multiple documents• Considerations with multi-datacenter deployments 2
  3. 3. JSON Documents 33
  4. 4. Couchbase Server is a Document Database 4
  5. 5. Document Database• Easy to distribute data• Makes sense to application programmers This synergy between the programming model and the distribution model is very valuable. It allows the database to use its knowledge of how the application programmer clusters the data to help performance across the cluster. 5
  6. 6. JSON Documents• Maps more closely to external API• CRUD Operations, lightweight schema { “fields” : [“with basic types”, 3.14159, true], “like” : “your favorite language” }• Stored under an identifier key client.set(“mydocumentid”, myDocument); mySavedDocument = client.get(“mydocumentid”); 6
  7. 7. Meta + Document Body{ Document "brewery": "New Belgium Brewing", user data, "name": "1554 Enlightened Black Ale", can be anything "abv": 5.5, "description": "Born of a flood...", "category": "Belgian and French Ale", "style": "Other Belgian-Style Ales", "updated": "2010-07-22 20:00:20"} unique ID “vintage” date format from an SQL dump{ >_< "id" : "beer_Enlightened_Black_Ale”, Metadata ... identifier,{ expiration, etc 7
  8. 8. No More ALTER table• No More alter table• More productive developers!• Emergent schema—the next session is about views• This session is about working directly with documents from interactive application code• Schema driven by code 8
  9. 9. Live Data 99
  10. 10. Realtime Interactive CRUD • Requirement: high-performance with high- concurrency and dynamic scale • Key/value API is the interactive low-latency path 10
  11. 11. Runtime Driven Schema• What’s in the database looks more like your code• Thinking about throughput, latency, update and read patterns is the new data modeling• Data flows get more attention than data at rest• When should I split a data-structure into multiple documents? • Generally the more useful your document is as a standalone entity, the better. • Documents that grow without bound are bad 11
  12. 12. Link Between Documents 12 12
  13. 13. Let’s Add Comments and Ratings to the Beer • Challenge linking items together • Whether to grow an existing item or store independent documents • No transactionality between documents! good w/ burgers{ "brewery": "New Belgium Brewing", I give that a 5! "name": "1554 Enlightened Black Ale", "abv": 5.5, "description": "Born of a flood...", tastes like "category": "Belgian and French Ale", college! "style": "Other Belgian-Style Ales", "updated": "2010-07-22 20:00:20" 13}
  14. 14. Let’s Add Comments and Ratings to the Beer • We’ll put comments in their own document • And add the ratings to the beer document itself. {{ "brewery": "New Belg "type": "comment", "name": "1554 Enligh "about_id": "abv": 5.5,"beer_Enlightened_Black_Ale", "description": "Born o "user_id": 525, "category": "Belgian "text": "tastes like college!", "style": "Other Belgia "updated": "2010-07-22 20:00:20" "updated": "2010-07-} “ratings” : { “525” : 5, I give that a 5! “30” : 4, “1044” : 2 14
  15. 15. Do it: save the comment document • Set at the id “f1e62” client.set(“f1e62”,{ create a new "type": "comment", document "about_id": "beer_Enlightened_Black_Ale", "user_id": 525, "text": "tastes like college!", { }); "updated": "2010-07-22 20:00:20" "id": "f1e62" } 15
  16. 16. { Link between comments and beers "brewery": "New Belgium "name": "1554 Enlightene "abv": 5.5, { "description": "Born of a f "type": "comment", "category": "Belgian and Flink to "about_id": "style": "Other Belgian-Sty beer "beer_Enlightened_Black_Ale", "updated": "2010-07-22 2 "user_id": 525, “ratings” : { "text": "tastes like college!", “525” : 5, "updated": "2010-07-22 20:00:20" “30” : 4, {} “1044” : 2 "id": "f1e62" }, } “comment_ids” : [ “f1e62”, link to comments “6ad8c” ] } 16
  17. 17. How to: look up comments from a beer • SERIALIZED LOOP beer = client.get(“beer:A_cold_one”); beer.comment_ids.each { |id| comments.push(client.get(id)); }• FAST MULTI-KEY LOOKUP beer = client.get(“beer:A_cold_one”); comments = client.multiGet(beer.comment_ids) • ASYNC VIEW QUERY comments = client.query(“myapp”,“by_comment_on”, {:key => “beer:A_cold_one”}); figure 17
  18. 18. How to: add a rating to a beer• Other users are ratings beers also, so we use a CAS update – we don’t want to accidentally overwrite another users rating that is being saved at the same time as ours• Best practice is to use a lambda so the client can retry cb.cas("mykey") do |doc| doc["ratings"][] = my_rating docend Actor 1 Actor 2 Success CAS mismatch & retry Couchbase Server 18
  19. 19. Object Graph With Shared Interactive Updates • Challenge: higher level data structures • Objects shared across multiple users • Mixed object sets (updating some private and some shared objects) figure 19
  20. 20. Get With Lock (GETL) • Often referred to as “GETL” • Pessimistic concurrency control • Locks have a short TTL • Locks released with CAS operations • Useful when working with object graphs 20
  21. 21. Multi-Datacenter Consistency? 21 21
  22. 22. You Want Datacenter Affinity• No ACID across documents, need resilient code• Locks and counters should be per-datacenter• Impacts operations like INCR DECR and CAS US DATA CENTER EUROPE DATA CENTER ASIA DATA CENTER Replication Replication Replication 22
  23. 23. Conclusion and Next Session Summary• JSON documents• Runtime-driven schemaNEXT UP: Views• See inside the data• Practical patterns 23
  24. 24. Questions? 24 2