couchconf tokyo 2013_getting started with couchbase app development
TRANSCRIPT
Getting Started with Couchbase App Development
Matt Ingenthron
Director, Developer Solutions
3
•Matt has been working with Couchbase since the beginning.
• Helped shape many of the features and architecture from experience of what was troublesome with previous large-scale web deployments.
• Has a passion for helping make app development more simple and straightforward.
• Currently leading the team in building out the right client interface into Couchbase and integration with higher level frameworks.
Who is Matt Ingenthron?
4
• 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.
Agenda
GETTING STARTED
5
6
Where to Obtain CouchbaseDownload from couchbase.com/download
Install via .rpm, .deb, .exe, or .app
Get a client fromcouchbase.com/develop
• Central place for clients, both officially supported and community driven.
• Java, .NET, PHP, Ruby and C• Python, golang, JRuby, Erlang, etc.• Install using common tools such
as Java’s Maven, Ruby’s gem, Ubuntu apt, etc.
7
Demo
PROGRAMMING MODEL
8
9
The Relational Approach to Storing Data
http://martinfowler.com/bliki/AggregateOrientedDatabase.html
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.
10
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
11
• get (key)– Retrieve a document
• gets(key)– Retrieve a document and the CAS value associated with the object (more
on this in a bit)
Basics: Retrieve
12
• 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 system
Also: new feature to allow applications to express durability requirements: ReplicateTo.TWO, PersistTo.TWO
Basics: Create, Update, Delete
13
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
Atomic Integers
14
Simple Example in Ruby# user.rbrequire “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) endend
# example.rbrequire “./user.rb”
u1 = User.new({ :email => “michael.nitschinger@c…”,:name => “Michael Nitschinger”,:title => “Developer Advocate”,:twitter => “@daschl”
})
u1.save
15
Extending Example with Accessor# user.rbrequire “rubygems”require “couchbase”
class User attr_accessor :name, :email, :title, :twitter
def initialize(attr = {}) ... end
def save client = Couchbase.bucket client.set(@email.downcase, self.to_json) end
def self.find_by_email(email) client = Couchbase.bucket doc = client.get(email.downcase) return doc ? User.new(doc) : nil endend
# example.rbrequire “./user.rb”
u1 = User.new({ :email => “michael.nitschinger@c…”,:name => “Michael Nitschinger”,:title => “Developer Advocate”,:twitter => “@daschl”
})
u1.save
u1 = User.find_by_email(“michael.nitschinger@c…”)
if u1 puts “User Found!” puts u1.inspectelse puts “User Not Registered!”end
16
Actor 1 Actor 2
Couchbase Server
CAS mismatch!Success
# actors.rb
c = Couchbase.bucketc.set(“mydoc”, { :myvalue => nil }
doc1, flags, cas = c.get(“mydoc”, :extended => true)
doc2, flags, cas = c.get(“mydoc”, :extended => true)
#c.set (“mydoc”, { “myvalue”: true }, :cas => cas)
# will fail because cas has changedc.set (“mydoc”, { “myvalue”: true }, :cas => cas)
Compare and SwapOptimistic Concurrency in a Distributed System
DIFFERENCES OF NOTE
17
18
Consistently Has High Performance
Couchbase Ensures…• Responses are always fast• Even if a response can’t be serviced
right away
Shift your Expectations:
• No longer a need to avoid round-trips
• No longer a need to avoid database requests for application speed
A Managed Cache
19
• As an appliction 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
Scales Easily, Transparently
20
Client Setup: Getting Cluster Configuration
http://myserver:8091/pools
{… "bucketCapabilities": [ "touch", "sync", "couchapi" ], "bucketCapabilitiesVer": "sync-1.0", "bucketType": ”couchbase", "name": "default", "nodeLocator": "vbucket", "nodes": [….
Couchbase ClientCluster Configuration
over REST
21
Client Setup: Getting Cluster Configuration
Couchbase Client
New Node Added to Cluster and Coming Online
Couchbase TopologyUpdate
22
Add Nodes to Cluster
• Two servers addedOne-click operation
• Docs automatically rebalanced across clusterEven distribution of docsMinimum doc movement
• Cluster map updated
• App database calls now distributed
over larger number of servers
REPLICA
ACTIVE
Doc 5
Doc 2
Doc
Doc
Doc 4
Doc 1
Doc
Doc
SERVER 1
REPLICA
ACTIVE
Doc 4
Doc 7
Doc
Doc
Doc 6
Doc 3
Doc
Doc
SERVER 2
REPLICA
ACTIVE
Doc 1
Doc 2
Doc
Doc
Doc 7
Doc 9
Doc
Doc
SERVER 3 SERVER 4 SERVER 5
REPLICA
ACTIVE
REPLICA
ACTIVE
Doc
Doc 8 Doc
Doc 9 Doc
Doc 2 Doc
Doc 8 Doc
Doc 5 Doc
Doc 6
READ/WRITE/UPDATE READ/WRITE/UPDATE
APP SERVER 1
COUCHBASE Client Library
CLUSTER MAP
COUCHBASE Client Library
CLUSTER MAP
APP SERVER 2
COUCHBASE SERVER CLUSTER
User Configured Replica Count = 1
23
Document Database, Simple ApproachWork with aggregates, usually JSON. Use simple operations to work with your data. 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 to easily scale up and make their applications perform.
In Summary
Easy to Get Started, Straightforward to UseDownload, install server. Binaries are available. Provisioning is fast through the browser and flexible with a REST interface. Install client using tools such as Java’s maven, Ruby’s gem, etc. Interact with simple verbs.
Q&A
24
THANKS!
25
26
Image Attribution• Broken Glass:
http://ajudgetocrush.deviantart.com/art/Broken-Glass-151350225
27