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
  • Be the first to comment

  • Be the first to like this


  1. 1. Getting Started withCouchbase App Development Robin Johnson Developer Advocate
  2. 2. Agenda• Learn what it takes to get started and begin to build an application against Couchbase.• Lean how you work with a Document Database and what is different. Learn how you work with documents in Couchbase itself.• Gain an understanding of how fundamentally different approaches in Couchbase allow for your application to scale and consistently perform well.
  3. 3. Getting Started
  4. 4. Where to Obtain CouchbaseDownload via .rpm, .deb, .exe, or .app EASY!
  5. 5. Official SDKs Ruby Python SDKs Go Clojure
  6. 6. Quickstart Java• Current Release: 1.1.4 (supports both 1.8.1 and 2.0.x)• Download from the Website (JARs) or use Maven• Maven: add our Repository:• Getting Started Guide: started.html
  7. 7. Quickstart .NET• Current Release: 1.2.3 (supports both 1.8.1 and 2.0.x)• Package published to NuGet • Install-Package CouchbaseNetClient• Binaries published to• Getting Started Guide: started.html
  8. 8. Client Architecture Overview• Based on the information given, the Client tries to establish an initial connection.• Once that’s done, it connects to a streaming API (HTTP chunked).• Cluster updates are fetched from that connection.• Failover/Add/Remove scenarios all update the clients in near realtime – no application restarts required!• Key/Value access is done directly against the nodes.• For View access one of the nodes is picked out which aggregates the results.
  9. 9. Client Setup: Getting Cluster Configuration Couchbase Couchbase Client Topology App Server Received
  10. 10. Client Setup: Getting Cluster Configuration Couchbase Client New Node Added to Cluster and Coming Online Couchbase Topology Update
  11. 11. Developing with Couchbase
  12. 12. Fundamentals• Couchbase is structured as a Key-Value store: every Document has a Key and a Value.• Keys can be any UTF-8 string up to 255 characters long.• Keys are unique, within a database (bucket), there can only be one document with a associated key.• Keys are completely in the control of the application developer, there is no internal mechanism for key generation.• Values can be JSON, strings, numbers, binary blobs, or a special positive atomic counter (unsigned integer).• Values can be up to 20MB in size.
  13. 13. Operations in Couchbase
  14. 14. Basics: Retrieve• get (key) - Retrieve a document• gets(key) - Retrieve a document and the CAS value associated with the object (more on this in a bit)
  15. 15. Basics: Create, Update, Delete• set (key, value) - Store a document, overwrites if exists• add (key, value) - Store a document, error/exception if it already exists• replace (key, value) - Store a document, error/exception if doesn’t exist• delete(key) - Delete the document from the systemAlso: new feature to allow applications to express durabilityrequirements: ReplicateTo.TWO, PersistTo.TWO
  16. 16. Atomic IntegersAtomic Counters are a special structure in Couchbase, they areexecuted in order and are Positive Integer Values•set (key, value) - Use set to initialize the counter • cb.set(“my_counter”, 1)•incr (key) - Increase an atomic counter value, default by 1 • cb.incr(“my_counter”) # now it’s 2•decr (key) - Decrease an atomic counter value, default by 1 • cb.decr(“my_counter”) # now it’s 1
  17. 17. Simple Example in Ruby# example.rb # user.rbrequire “./user.rb” require “rubygems” require “couchbase”u1 ={ :email => “”, class User :name => “Robin Johnson”, attr_accessor :name, :email, :title, :twitter :title => “Developer Advocate”, :twitter => “@rbin” def initialize(attr = {})}) attr.each do |name, value| setter = "#{name}=” next unless respond_to?(setter) send(setter, value) end end def save client = Couchbase.bucket client.set(@email.downcase, self.to_json) end end
  18. 18. Add Lookup Class Method# example.rb # user.rbrequire “./user.rb” require “rubygems” require “couchbase”u1 ={ :email => “”, class User :name => “Robin Johnson”, attr_accessor :name, :email, :title, :twitter :title => “Developer Advocate”, :twitter => “@rbin”}) def initialize(attr = {}) ... def save c = Couchbase.bucketu1 = User.find_by_email(“”) c.set(@email.downcase, self.to_json) endif u1puts “User Found!”puts u1.inspect def self.find_by_email(email)else c = Couchbase.bucketputs “User Not Registered!” doc = c.get(email.downcase)end return doc ? : nil end end
  19. 19. Agile Model Development# example.rb # user.rbrequire “./user.rb” require “rubygems” require “couchbase”u1 = User.find_by_email(“”) class Userif u1 attr_accessor :name, :email, :title, :twitteru1.fb_id = “682829292”u1.fb_token = “f02jdjd020d8373730djd02” attr_accessor :fb_id, :fb_tokenu1.saveelse def initialize(attr = {}) ... end# create userend def save ... end def self.find_by_email(email) c = Couchbase.bucket doc = c.get(email.downcase) return doc ? : nil end end
  20. 20. Compare and Swap Optimistic Concurrency in a Distributed System# actors.rb Actor 1 Actor 2c = Couchbase.bucketc.set(“mydoc”, { :myvalue => nil } Success CAS mismatch!doc1, flags, cas = c.get(“mydoc”, :extended => true)# Couchbase Serverc.set (“mydoc”, { “myvalue”: true }, :cas => cas)# will fail because cas has changedc.set (“mydoc”, { “myvalue”: true }, :cas => cas)
  21. 21. Demo
  22. 22. Programming Model
  23. 23. The Relational Approach to Storing Data Relational databases were not designed with clusters in mind, which is why people have cast around for an alternative. Storing aggregates as fundamental units makes a lot of sense for running on a cluster.
  24. 24. What is an aggregate?When you need to retrieve data from RDBMS, you are"aggregating" or "denormalizing" the data for yourapplication through queries with joins, where clausesand order by clauses.In Document Databases, instead of breaking data intotables and foreign keys, you store the aggregate datatogether in JSON document(s).
  25. 25. Document Database by Comparison o::1001 { uid: “ji22jd”, customer: “Ann”, line_items: [ { sku: 0321293533, quan: 3, unit_price: 48.0 }, { sku: 0321601912, quan: 1, unit_price: 39.0 }, { sku: 0131495054, quan: 1, unit_price: 51.0 } ], payment: { type: “Amex”, expiry: “04/2001”, last5: 12345 } • Easy to distribute data • Makes sense to application programmers
  26. 26. Relational vs Document Model C1 C2 C3 C4 { JSON JSON } JSON Relational data model Document data model Highly-structured table organization Collection of complex documents with with rigidly-defined data formats and arbitrary, nested data formats and record structure. varying “record” format. 27
  27. 27. SQL Normalized Tables foreign key Users Addresses KEY First Last ZIP_ID ZIP_ID CITY STATE ZIP 1 Jasdeep Jaitla 2 1 DEN CO 30303 2 Joe Smith 2 2 MV CA 94040 3 Ali Dodson 2 3 CHI IL 60609 4 John Doe 3 4 NY NY 10010 To get information about specific user, you perform a join across two tablesSELECT * FROM Users u INNER JOIN Addresses a ON u.zip_id = a.zip_id WHERE key=1 28
  28. 28. Documents are Aggregates Users Addresses KEY First Last ZIP_ID ZIP_ID CITY STATE ZIP 1 1 Jasdeep Jaitla Jaitla 2 1 DEN CO 30303 { “ID”: 1, “First”: 2 3 Joe Ali Smith Dodson 2 2 + 2 3 SF CHI CA IL 94103 60609 = “Jasdeep”, “Last”: “Jaitla”, “ZIP”: “94103”, “CITY”: “SF”, “STATE”: “CA” JSON } 4 John Doe 3 4 NY NY 10010 Document Data is an Aggregate All data in a single documentcouchbase.get(“user::1”) 29
  29. 29. Multiple Documents
  30. 30. Sample Model• User profile• Blog Post Object • Contains the Content (ID, Title, Body Text, Author)• Blog Comment • Comments from other users • Option 1 - Keep it with Blog Document • Option 2 - Separate it Out
  31. 31. Single Document {Blog Post Object “id”: “scalabl3_Hello_World”,string id “author”: “scalabl3”,string author “type”: “post”string type “title”: “Hello World”, “format”: “markdown”,string title “body”: “Hello fromstring format [Couchbase](”,string body “html”: “<p>Hello from <a href=“http: …,string html “comments”: [array comments {“format”: “markdown”, “body”:”Awesome post!”}, {“format”: “markdown”, “body”:”Like it.” } ] }
  32. 32. Multiple Documents {Blog Post Object “id”: “scalabl3_Hello_World”,string id “author”: “scalabl3”,string author “type”: “post” “title”: “Hello World”,string type “format”: “markdown”,string title “body”: “Hello fromstring format [Couchbase](”,string body “html”: “<p>Hello from <a href=“http: …, “num_comments”: 2string html }int comments { “id”: “scalabl3_Hello_World::1”,Comment Object “format”: “markdown”, “body”: “Awesome post!”string format }string body {int id “id”: “scalabl3_Hello_World::2”, “format”: “markdown”, “body”: “Love It!” }
  33. 33. Differences of Note
  34. 34. Mental Adjustments• In SQL we tend to want to avoid hitting the database as much as possible • Even with caching and indexing tricks, and massive improvements over the years, SQL still gets bogged down by complex joins and huge indexes, so we avoid making database calls• In Couchbase, get’s and set’s are so fast they are trivial, not bottlenecks, this is hard for many people to accept at first; Multiple get statements are commonplace, don’t avoid it!
  35. 35. Consistently Has High Performance Couchbase Ensures… • Responses are always fast • Even if a response can’t be serviced right away A Managed Cache Shift your Expectations: • No longer a need to avoid round-trips • No longer a need to avoid database requests for application speed 36
  36. 36. Scales Easily, Transparently• As an application developer, you think about interacting with the cluster - No longer a need to think about shards - No longer have to deploy and manage internal or external proxies to understand where the data is placed
  37. 37. Add Nodes to Cluster APP SERVER 1 APP SERVER 2 COUCHBASE Client Library COUCHBASE Client Library CLUSTER MAP CLUSTER MAP READ/WRITE/UPDATE READ/WRITE/UPDATE • Two servers added One-click operation SERVER 1 SERVER 2 SERVER 3 SERVER 4 SERVER 5 ACTIVE ACTIVE ACTIVE ACTIVE ACTIVE • Docs automatically rebalanced across Doc 5 Doc Doc 4 Doc Doc 1 Doc cluster Even distribution of docs Doc 2 Doc Doc 7 Doc Doc 2 Doc Minimum doc movement Doc 9 Doc Doc 8 Doc Doc 6 Doc • Cluster map updated REPLICA REPLICA REPLICA REPLICA REPLICA • App database Doc 4 Doc Doc 6 Doc Doc 7 Doc calls now Doc 1 Doc Doc 3 Doc Doc 9 Doc distributed over larger Doc 8 Doc Doc 2 Doc Doc 5 Doc number of servers COUCHBASE SERVER CLUSTERUser Configured Replica Count = 1
  38. 38. Couchbase in SummaryEasy to Get Started, Straightforward to UseDownload, install server. Binaries are available. Provisioning is fast through thebrowser and flexible with a REST interface. Install client using tools such as Java’smaven, Ruby’s gem, etc. Interact with simple verbs.Document Database, Simple ApproachWork with aggregates, usually JSON. Use simple operations to work with yourdata. Couchbase understands and is designed for distributed systems.Deployment of Applications Allow for Scale,PerformanceNew approaches to storing and retrieving data in Couchbase give developers the ability toeasily scale up and make their applications perform.
  39. 39. Q&A
  40. 40. Thanks!@rbin on twitter!