Your SlideShare is downloading. ×

Couchbase_TLV_2014_Couchbase_at_ViberDev track 01 - getting started part 1

3,250

Published on

Published in: Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
3,250
On Slideshare
0
From Embeds
0
Number of Embeds
10
Actions
Shares
0
Downloads
60
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide
  • Bulletize the text. Make sure the builds work.
  • Bulletize the text. Make sure build work properly.
  • Bulletize the text. Make sure build work properly.
  • 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.
  • Again, through Streaming API, our Client is made aware of the new Topology
    Under ideal conditions no pauses – usually realtime.
  • So all the SDKs are responsible of accessing data but also keep the cluster topology up to date.
  • 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.
  • Download product and install the sdk:
    - install in the session
  • Access the value in a fast and consistent way!
  • 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.
  • Access the value in a fast and consistent way!
  • 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.
  • You can use counter for example for versionning
  • You can use counter for example for versionning
  • You can use counter for example for versionning
  • We’re looking up by the email address which is the key.
    If user found, show details, if not, ‘user not registered’
  • We’re adding a field into our model here. With zero downtime. True agile development.
    Finding the user, adding the fields, saving.
  • 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.
  • Transcript

    • 1. Getting Started ugdual Grall echnical Evangelist
    • 2. Dev Track Agenda 10:30 - 11:10 am Getting Started : Installation and Core operations 11:20 - 12:00pm Getting Started : Advanced Operations and Patterns 01:00 - 01:40 pm N1QL: An Early Peek at Couchbase's document database query language 01:50 - 02:30 pm Document Your World 02:40 - 03:20 pm Indexing and Querying 03:40 - 04:20 pm Power Techniques With Indexing 04:30 - 05:10 pm Exploring Common Models and Integrations
    • 3. Getting Started
    • 4. Cluster-wide Basic Operation App Server 1 App Server 2 COUCHBASE Client Library COUCHBASE Client Library COUCHBASE Client Library COUCHBASE Client Library Cluster Map Cluster Map READ/WRITE/UPDATE Server 1 Server 2 Server 3 Active Active Active Doc 5 Doc Doc 4 Doc Doc 1 Doc Doc 2 Doc Doc 7 Doc Doc 2 Doc Doc 9 Doc Doc 8 Doc Doc 6 Doc REPLICA REPLICA REPLICA • Docs distributed evenly across servers • Each server stores both active and replica docs Only one server active at a time • Client library provides app with simple interface to database • Cluster map provides map to which server doc is on Doc 4 Doc Doc 6 Doc Doc 7 Doc Doc 1 Doc Doc 3 Doc Doc 9 Doc • App reads, writes, updates docs Doc 8 Doc Doc 2 Doc Doc 5 Doc • Multiple app servers can access same document at same time Couchbase Server Cluster User Configured Replica Count = 1 App never needs to know
    • 5. Add Nodes to Cluster App Server 1 App Server 2 COUCHBASE Client Library COUCHBASE Client Library COUCHBASE Client Library COUCHBASE Client Library Cluster Map Cluster Map READ/WRITE/UPDATE READ/WRITE/UPDATE Server 1 Server 2 Server 3 Server 4 Server 5 Active Active Active Active Active Doc 5 Doc Doc 4 Doc Doc 1 Doc Doc 7 Doc Doc 2 Doc Doc 9 Doc Doc 8 Doc Doc 6 • Docs automatically rebalanced across cluster Doc Doc 2 Doc REPLICA REPLICA REPLICA Doc 4 Doc Doc 6 Doc Doc 7 Doc Doc 1 Doc Doc 3 Doc Doc 9 Doc Doc 8 Doc Doc 2 Doc Doc 5 Doc Couchbase Server Cluster User Configured Replica Count = 1 • Two servers added One-click operation Even distribution of docs Minimum doc movement REPLICA REPLICA • Cluster map updated • App database calls now distributed over larger number of servers
    • 6. Fail Over Node App Server 1 App Server 2 COUCHBASE Client Library COUCHBASE Client Library COUCHBASE Client Library COUCHBASE Client Library Cluster Map Cluster Map Server 1 Server 2 Server 3 Server 4 Server 5 Active Active Active Active Active Doc 5 Doc Doc 4 Doc Doc 1 Doc Doc 9 Doc Doc 2 Doc Doc 7 Doc Doc 2 Doc Doc 8 Doc Doc 1 Doc 6 Doc Doc Doc 3 REPLICA REPLICA REPLICA REPLICA Doc 4 Doc Doc 6 Doc Doc 7 Doc Doc 5 Doc 1 Doc Doc 3 Doc Doc 9 Doc Doc 2 Couchbase Server Cluster User Configured Replica Count = 1 Doc REPLICA Doc 8 Doc Doc • App servers accessing docs • Requests to Server 3 fail • Cluster detects server failed Promotes replicas of docs to active Updates cluster map • Requests for docs now go to appropriate server • Typically rebalance would follow
    • 7. Couchbase SDK Official SDKs Ruby Python Community SDKs Go Clojure www.couchbase.com/develop
    • 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 real-time – 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 e mak Couchbase Client App Server r ct onne c i on og opol et eceiv y
    • 10. Bootstrap 1. GET /pools 2. Look for the "default" pools 3. GET /pools/default 4. Look for the "buckets" hash which contains the bucket list 5. GET /pools/default/buckets 6. Parse the list of buckets and extract the one provided by the application 7. GET /pools/default/buckets/
    • 11. Client Setup: Getting Cluster Configuration Couchbase Client New Node Added to Cluster and Coming Online Couchbase Couchbase Topology Topology Update Update
    • 12. SDK & libcouchbase Dependency • Java, .Net, C, Go are native • The SDK does not have dependency on other language • Ruby, PHP, Python, Node have dependency to libcouchbase • They are “wrapper” on the top of the C library • Scala, Clojure, JRuby are using Couchbase Java SDK
    • 13. Hands On
    • 14. Quick Start : Couchbase Server Download from http://www.couchbase.com/download Install via .rpm, .deb, .exe, or .app EASY!
    • 15. Quick Start : Client 1. Go to the developer page http://www.couchbase.com/develop 2. Select your language 3. Follow the “Getting Started” page • Java • C • .Net • Python • Ruby • Node • PHP • …
    • 16. Demonstration Installing Couchbase Server & Client
    • 17. Basics: Retrieve et (key) ­ Retrieve a document ets(key) ­ Retrieve a document and the CAS value associated with the object (more on this in a bit)
    • 18. Basics: Create, Update, Delete et (key, value) ­ Store a document, overwrites if exists dd (key, value) ­ Store a document, error/exception if it already exists eplace (key, value) ­ Store a document, error/exception if doesn’t exist elete(key) ­ Delete the document from the system
    • 19. Fundamentals • Couchbase is structured as a Key-Value store: every Document has a Key and a Value. • Key can be up to 250 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.
    • 20. Dealing with keys • You are responsible of the key • Keys can be build from: • UUID • Atomic Counter • Date/TimeStamp • Contains Separator: • User:001 • Product_XYZ
    • 21. What about the value ? • Couchbase stores the data the way you send them • For example: • cb.set(“mykey”, “This is my Value”); • cb.set(“mykey”, 100.2); • cb.set(“mykey”, javaObjectSerialized); • What about JSON? • cb.set(“mykey”, "{"msg" : "This is the value"}"); • cb.set(“mykey”, json.toJson( myJavaObject) ); • Your application deals with the JSON Document
    • 22. Q&A Next Session : Advanced Operations and Patterns
    • 23. Getting Started – Part 2
    • 24. Basics: Retrieve et (key) ­ Retrieve a document ets(key) ­ Retrieve a document and the CAS value associated with the object (more on this in a bit)
    • 25. Basics: Create, Update, Delete et (key, value) ­ Store a document, overwrites if exists dd (key, value) ­ Store a document, error/exception if it already exists eplace (key, value) ­ Store a document, error/exception if doesn’t exist elete(key) ­ Delete the document from the system
    • 26. Other Options urability Requirements ­ ­ ­ PersistTo.ONE ReplicateTo.ONE Simply add this to your method call when needed • ops = cb.set(“mykey”, myObject, PersistTo.ONE); ime to Live (TTL) ­ Used to delete the value after the specified time • cb.set(“mykey”, 30 x 24 x 60 x 60, myObject); // keep the value in Couchbase for 30 days.
    • 27. Atomic Integers Atomic Counters are a special structure in Couchbase, they are executed 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
    • 28. Couchbase Patterns • Atomic Counter for keys • Find your ID by numbers, loop on the values, … • Lookup • Lookup a document/values using multiple keys • Lists • Lookup pattern, using list of values • Indices and Queries
    • 29. Use a Counter cb = new CouchbaseClient(uris, "default", ""); Gson json = new Gson() // create a new User User user = new User("John Doe", "john@demo.com", "7621387216"); long userCounter = cb.incr("user_counter", 1, 1); cb.set( "user:"+ userCounter, json.toJson(user) ); // create a new user; user = new User("Jane Smith Doe", "jane@demo.com", "355662216"); userCounter = cb.incr("user_counter", 1); cb.set( "user:"+ userCounter, json.toJson(user) );
    • 30. Use a Counter
    • 31. Lookup cb = new CouchbaseClient(uris, "default", ""); Gson json = new Gson() // create a new User User user = new User("John Doe", "john@demo.com", "7621387216"); long userCounter = cb.incr("user_counter", 1); String key = "user:"+ userCounter; cb.set(key, json.toJson(user) ); // create another key for lookup cb.set(“email:john@demo.com”, key); // Find by email String keyToUser = cb.get(“email:john@demo.com”); Object user = cb.get(keyToUser);
    • 32. Agile Model Development
    • 33. Simple Example in Ruby # example.rb require “./user.rb” u1 = User.new({ :email => robin@couchbase.com, :name => “Robin Johnson”, :title => “Developer Advocate”, :twitter => “@rbin” }) u1.save # user.rb require “rubygems” require “couchbase” class User attr_accessor :name, :email, :title, :twitter 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
    • 34. Add Lookup Class Method # example.rb require “./user.rb” u1 = User.new({ :email => robin@couchbase.com, :name => “Robin Johnson”, :title => “Developer Advocate”, :twitter => “@rbin” }) u1.save u1 = User.find_by_email(“robin@couchbase.com”) if u1 puts “User Found!” puts u1.inspect else puts “User Not Registered!” end # user.rb require “rubygems” require “couchbase” class User attr_accessor :name, :email, :title, :twitter def initialize(attr = {}) ... end def save c = Couchbase.bucket c.set(@email.downcase, self.to_json) end def self.find_by_email(email) c = Couchbase.bucket doc = c.get(email.downcase) return doc ? User.new(doc) : nil end end
    • 35. Agile Model Development # example.rb require “./user.rb” u1 = User.find_by_email(“robin@couchbase.com”) if u1 u1.fb_id = “682829292” u1.fb_token = “f02jdjd020d8373730djd02” u1.save else # create user end # user.rb require “rubygems” require “couchbase” class User attr_accessor :name, :email, :title, :twitter attr_accessor :fb_id, :fb_token def initialize(attr = {}) ... end def save ... end def self.find_by_email(email) c = Couchbase.bucket doc = c.get(email.downcase) return doc ? User.new(doc) : nil end end
    • 36. Concurrency & more
    • 37. Compare and Swap Optimistic Concurrency in a Distributed System # actors.rb c = Couchbase.bucket c.set(“mydoc”, { :myvalue => nil } doc1, flags, cas = c.get(“mydoc”, :extended => true) # c.set (“mydoc”, { “myvalue”: true }, :cas => cas) # will fail because cas has changed c.set (“mydoc”, { “myvalue”: true }, :cas => cas) Success CAS mismatch!
    • 38. Check Status before Saving OperationStatus ops = null; int numberOfAttemptsLeft = 5; do { CASValue listOfTagWithCas = cb.gets(KEY); if (listOfTagWithCas == null) { cb.set(KEY, tag).getStatus(); return; } else { ops = cb.append(listOfTagWithCas.getCas(), KEY, ","+ tag).getStatus(); if (ops.isSuccess()) { return; } } numberOfAttemptsLeft--; } while (numberOfAttemptsLeft > 0); if (ops != null && !ops.isSuccess()) { throw new Exception("Failed to update item '"+ KEY+"' too many times, giving up!"); }
    • 39. Replica Read ead the replica when you cannot reach the node responsible of the data try { value = (String)cb.get(key); System.out.println("Master node read"); } catch (Exception e ) { value = (String)cb.getFromReplica(key); System.out.println("Doing a Replica Read"); }
    • 40. Conclusion • Couchbase Server is installed • Couchbase Client SDK is installed • Core operations to save and get the data • What’s next? • How do you design your data? Document Design • How do you find your data? Indexing and Querying • Do even more… Common Models and Integration
    • 41. Q&A Next Session : Advanced Operation and Sample Applications

    ×