Like this? Share it with your network

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads


Total Views
On Slideshare
From Embeds
Number of Embeds



Embeds 435 434 1

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

    No notes for slide
  • Getting started with Couchbase is intentionally very easy. Couchbase is Open Source under an Apache 2.0 license, but there are binaries built for just about any platform. Most of the time, getting started with Couchbase is just a matter of downloading the package for your OS or the setup. Setting up the client library is equally simple.
  • Couchbase client Basically, give people an overview of how the SDK’s get notified when there is a topology change via async REST.On the SDK side, we make sure the SDK’s as much as possible, get to the know the new cluster config – over streaming API.
  • We can have more durability PersistTo and ReplicateTo. Persist to master (1), two, three.Using PersistTo master is the vbucket on which the key is stored. If we use PersistTo, we trade off some performance for more data Durability.ReplicateTo is the fastest, as it’s just sending the doc to the other node.We only use these when we really need it.
  • If we have two different app servers trying to update the doc, the first one that gets there using the CAS value, will win. The second will fail.Every time any data is changed in a document, the CAS value changes. If we use the same CAS twice, it’s not going to work twice.
  • Demo the Secrets.imclojure app. Show how it interacts with data. Show the view of the docs, but insist on not going into too much view details, as Mike will cover views this afternoon.
  • Getting info from different tables via joins.We create an Aggregate.
  • Also touch on how we wouldn’t have to perform expensive ALTER TABLE statements when we update our data model.Easier to distribute as no need for joins.
  • A very typical way of structuring data in Documents. Id – string, Author – string etc etc.Comments on the blog are embedded in an Array, within the doc.Can be a good way of doing things. But what could the problem be with this structure - Particularly in this example?
  • These docs can be all separate and linked together through reference ID’s and multiple gets.In the comment object, we use the reference ID’s with a counter on the end. We can also see the positive integer at the bottom of the Blog post model, tracking how many comments there are. This is a good place to use Atomic Counters, to Inrement / Decrement comments if one were added, or deleted.Again, this makes it easier to distribute docs, allowing for less latency. It also prevents bloating in documents.
  • Differences / Advantages of Couchbase over traditional RDBMS.
  • We just interact with the Cluster, not a specific node.No need to think about where data is located. Couchbase Cluster map knows. We always know which shard data is on.
  • Mention in summary…Also, note there are other systems such as Hadoop, Elastic search that have a lot of merit to add, and there are system like relational databases that don’tadddresscontemporaryneeds


  • 1. Getting Started withCouchbase App Development Robin Johnson Developer Advocate
  • 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. Getting Started
  • 4. Where to Obtain CouchbaseDownload via .rpm, .deb, .exe, or .app EASY!
  • 5. Official SDKs Ruby Python SDKs Go Clojure
  • 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. 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. 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. Client Setup: Getting Cluster Configuration Couchbase Couchbase Client Topology App Server Received
  • 10. Client Setup: Getting Cluster Configuration Couchbase Client New Node Added to Cluster and Coming Online Couchbase Topology Update
  • 11. Developing with Couchbase
  • 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. Operations in Couchbase
  • 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. 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. 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. 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. 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. 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. 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. Demo
  • 22. Programming Model
  • 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. 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. 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. 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. 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. 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. Multiple Documents
  • 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. 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. 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. Differences of Note
  • 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. 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. 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. 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. 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. Q&A
  • 40. Thanks!@rbin on twitter!