Developing in NoSQL with Couchbase and Java
Raghavan N. Srinivas Couchbase 123
Objective
• A technical overview – To getting started on programming with Couchbase using Java – To learn about the basic operations, asynchronous and advanced operations – To learn about secondary indexing with views and queries (a Couchbase Server 2.0 feature) – To enhance applications with views and queries Speaker Introduction
• Architect and Evangelist working with developers • Speaker at JavaOne, RSA conferences, Sun Tech Days, JUGs and other developer conferences • Taught undergrad and grad courses • Technology Evangelist at Sun Microsystems for 10+ years • Still trying to understand and work more effectively on Java and distributed systems • Couchbase Developer Advocate working with Java and Ruby developers • Philosophy: “Better to have an unanswered question than a unquestioned answer” Agenda
> Introduction and Getting Started > Asynchronous and Advanced operations > Doc. Design and Beer Data > Introduction to Query and Views > Roadmap and conclusions
A bit of NOSQL
Intro. Advanced Doc. Design Views Summary opertations The NoSQL Boom
Cartoon Courtesy: geek and poke CAP Theorem
The CAP theorem states that it is impossible for a distributed computer system to simultaneously provide all three of the following guarantees:
• Consistency means that each node/client always has the same view of the data, • Availability means that all clients can always read and write, • Partition tolerance means that the system works well across physical network partitions.
Eight Fallacies of Distributed Computing
• The Network is reliable • Latency is zero • Bandwidth is infinite • The Network is secure • Topology doesn’t change • There is one administrator • Transport cost is zero • The Network is homogenous Web Application Architecture and Performance
Application Scales Out Just add more commodity web www.facebook.com/animalparty servers
Load Balancer Web Servers
Rela onal Database Scales Up Get a bigger, more complex Database server
- Expensive and disrup ve sharding - Doesn’t perform at Web Scale Couchbase data layer scales like application logic tier Data layer now scales with linear cost and constant performance.
Application Scales Out www.facebook.com/animalparty Just add more commodity web servers
Load Balancer Web Servers
Couchbase Servers Database Scales Out Just add more commodity data servers
Horizontally scalable, schema-less, auto- sharding, high-performance at Web Scale
Scaling out flattens the cost and performance curves. What is Couchbase?
Intro. Advanced Doc. Design Views Summary opertations What is Couchbase?
• Multitenant Document (JSON-based) Store • Distributed • Fault Tolerant – Persistent – Replicated • Schema-Free Qualities
• Simple – memcached API • Uniformly FAST – Sub-millisecond response times – Predictable scalability • Elastic – Easy scalable
Couchbase Server 1.8 Architecture
11211 11210 Memcapable 1.0 Memcapable 2.0
Moxi
Memcached
Couchbase EP Engine
Data Manager Cluster Manager Heartbeat
storage interface Process monitor Node health monitor Configura on manager Rebalance orchestrator Global singleton supervisor REST management API/Web UI vBucket state and replica on manager
Persistence Layer h p on each node one per cluster
Erlang/OTP
HTTP Erlang port mapper Distributed Erlang 8091 4369 21100 - 21199 Component Architecture
11211 11210 Memcapable 1.0 Memcapable 2.0
Moxi
Memcached
Couchbase EP Engine WebUI Heartbeat
storage interface Processmonitor Node health monitor Rebalance orchestrator REST ManagementREST / ConfigurationManager Global singleton supervisor vBucket state and replica on manager htt Persistence Layer p on each node one per cluster
Erlang/OTP
HTTP Erlang port mapper Distributed Erlang 8091 4369 21100 - 21199 Architecture
User action results in the need to change the VALUE of KEY 1
Application updates key’s VALUE, 2 performs SET operation
4 Couchbase driver hashes KEY, identifies SET request sent over 3 KEY’s master server network to master server
5 Couchbase replicates KEY-VALUE pair, caches it in memory and stores it to disk A Distributed Hash Table Document Store
Application
set(key, json) get(key) returns json Database Cluster
{ DData “id": ”brewery_Legacy”, “type” : “brewery”, "name" : "Legacy Brewing”, “address": "525 Canal Street Reading", "updated": "2010-07-22 20:00:20", } Getting Started
Intro. Advanced Doc. Design Views Summary opertations Installation of the Server for development
• Operating systems supported • RedHat – RPM • Ubuntu – dpkg • Windows • Mac OS • GUI Installer • Unattended installation • Quickstart once installed (invokes the Admin Console) • http://
Couchbase Client interface
Intro. Advanced Doc. Design Views Summary opertations Client Setup: Getting Cluster Configuration
Couchbase Client Cluster Configuration over REST { … "bucketCapabilities": [ "touch", http://myserver:8091/ "sync", "couchapi" ], "bucketCapabilitiesVer": "sync-1.0", "bucketType": ”couchbase", "name": "default", "nodeLocator": "vbucket", "nodes": [ ….
Server Server Server Server Node Node Node Node
2 1 Client at Runtime: Adding a node
Couchbase Client New node
Cluster coming online Topology Update
Server Server Server Server Server Node Node Node Node Node
2 2 Opening a Connection (Java)
Connect to the Cluster URI of any cluster node
List
uris.add(URI.create("http://127.0.0.1:8091/pools")); try { client = new CouchbaseClient(uris, "default", ""); } catch (Exception e) { System.err.println("Error connecting to Couchbase: " + e.getMessage()); System.exit(0); } Java Client Library Dependencies
• common-codecs – Base 64 encoding. Encoders and decoders • jettison – JSON Processing • netty – HTTP comet streaming • httpcore-nio – High performance NIO sockets (for views) • httpcore – Needed by httpcore-nio