From 0 to syncing

44
From 0 to syncing in 30 min Ma#hew Revell @ma#hewrevell Philipp Fehre @ischi

description

Before joining Couchbase Phil has been a consultant on many different node.js and NoSQL projects working with many different languages and databases. By helping clients solve problems regarding scalability as well building completely new APIs he gained a broad knowledge of the available platforms and their tradeoffs in the big and small. He's a Developer Evangelist for Couchbase where he works to educate developers on the different parts of using a NoSQL database from mobile to big iron servers.

Transcript of From 0 to syncing

Page 1: From 0 to syncing

From 0 to syncing in 30 min

Ma#hew  Revell  @ma#hewrevell  

Philipp  Fehre  @ischi

Page 2: From 0 to syncing

Introduction Couchbase

Page 3: From 0 to syncing

Couchbase 101

• Distributed  key-­‐value  store  

• Create  secondary  indexed  views  using  Map  Reduce  

• AutomaBc  key-­‐based  parBoning  

• Integrated  memcached  caching  layer  

• Strongly  consistent  within  a  cluster  

• Cross-­‐data  centre  replicaBon  

• Intelligent  SDKs

Page 4: From 0 to syncing

Couchbase  Organiza1on•Couchbase  operates  like  a  Key-­‐Value  Document  Store  

• Key  is  a  UTF-­‐8  string  up  to  256  Bytes  

• Values  can  be:    

-­‐ Simple  Datatypes:  strings,  numbers,  date1me,  boolean,  and  binary  data  can  be  stored  -­‐-­‐  they  are  stored  as  Base64  encoded  strings  

-­‐ Complex  Datatypes:  dic1onaries/hashes,  arrays/lists,  can  be  stored  in  JSON  format  (simple  lists  can  be  string  based  with  delimiter)  

-­‐ JSON  is  a  special  class  of  string  with  a  specific  format  for  encoding  simple  and  complex  data  structures  

• Schema  is  unenforced  and  implicit,  schema  changes  are  programma1c,  done  online,  and  can  vary  from  Document  to  Document

Page 5: From 0 to syncing

Couchbase Server 2.0 Architecture

New  Persistence  Layer

storage  interface

Couchbase  EP  Engine

11210  Memcapable    2.0

Moxi

11211  Memcapable    1.0

Object-­‐level  Cache

Disk  Persistence

8092  Query  API

Que

ry  Engine

HTTP  8091

Erlang  port  mapper  4369

Distributed  Erlang  21100  -­‐  21199

Heartbeat

Process  m

onito

r

Glob

al  singleton  supe

rviso

r

Confi

gura1o

n  manager

on  each  node

Rebalance  orchestrator

Nod

e  he

alth  m

onito

r

one  per  cluster

vBucket  state  and

 replica1

on  m

anager

h#p

REST  m

anagem

ent  A

PI/W

eb  UI

Erlang/OTP

Server/Cluster  Management  &  CommunicaBon  

(Erlang)

RAM  Cache,  Indexing  &  Persistence  Management  

(C)

Page 6: From 0 to syncing

Supported SDK'swww.couchbase.com/communiBes

• Each  supported  SDK  page  has  instruc1ons  for  setup  

• PHP,  Ruby,  and  Python  clients  are  wrappers  around  libcouchbase  C  library,  so  libcouchbase  must  be  installed  first  

• For  other  community  clients,  click  on  "All  Clients"  on  lea  nav,  scroll  down  the  page  and  you  can  see  clients  for  Go,  Erlang,  Clojure,  TCL,  Perl  and  others.

Page 7: From 0 to syncing

‹#›Couchbase, Inc. Confidential

Easy Scalability

XDCRSingle Node

Type

No Manual Sharding !Database manages data movement to scale out - Not the user

Database handles propagation of updates to scale across clusters and geos !Provides disaster recover / data locality

Hugely simplifies management of clusters !Easy to scale clusters by adding any # of nodes

FEATURES

Auto Sharding

Couchbase, Inc. Confidential

Page 8: From 0 to syncing

‹#›Couchbase, Inc. Confidential

Consistent, High Performance

Fine Grained Locking

Built-in Cache

Hash Partitioning

Allows high concurrency and in turn high throughput via highly granular latches

No need of separate cache layer !Database manages actively used data

Uniform data distribution !Uniform load distribution – NO hotspots

PERFORMANCE

Support a large number of users needed for interactive apps

Massive Concurrent Connections

FEATURES

Couchbase, Inc. Confidential

Page 9: From 0 to syncing

‹#›Couchbase, Inc. Confidential

Always on 24x7 Capability

All admin operations online !•Compaction • Indexing •Rebalance •Backup & Restore

•High availability using in-memory replication •Auto or manual failover •XDCR for disaster recovery

Online administrative

operations

HA via Replication DR via XDCR

FEATURES

Online DB upgrades and HW maintenance !Optimized swap operation to replace nodes

Online DB upgrades and maintenance

Couchbase, Inc. Confidential

Page 10: From 0 to syncing

‹#›Couchbase, Inc. Confidential

Flexible Data Model

Represent data as objects instead of shredding into rows and columns !Create indexes on any attribute of the document

Each document can have a different structure !Easy to change data without database changes and downtime

Maintains Native object

representation

Handles constantly

changing data

JSONJSONJSONJSONJSON

FEATURES

Data with mixed structure better managed via JSON in a document DB than an RDBMS

Schema-less for structured / un/semi- structured

data

Couchbase, Inc. Confidential

Page 11: From 0 to syncing

‹#›Couchbase, Inc. ConfidentialCouchbase, Inc. Confidential

Couchbase Server Cluster

Basic Operation

User Configured Replica Count = 1

Read/write/update

Active

SERVER 1

Active

SERVER 2

Active

SERVER 3

App Server 1

COUCHBASE Client Library !CLUSTER MAP

COUCHBASE Client Library !CLUSTER MAP

App Server 2

Doc 5

Doc 2

Doc 9

Doc

Doc

Doc

Doc 4

Doc 7

Doc 8

Doc

Doc

Doc

Doc 1

Doc 3

Doc 6

Doc

Doc

Doc

Replica Replica Replica

Doc 4

Doc 1

Doc 8

Doc

Doc

Doc

Doc 6

Doc 3

Doc 2

Doc

Doc

Doc

Doc 7

Doc 9

Doc 5

Doc

Doc

Doc

• 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 App never needs to know

• App reads, writes, updates docs • Multiple app servers can access

same document at same time

Page 12: From 0 to syncing

‹#›Couchbase, Inc. ConfidentialCouchbase, Inc. Confidential

Add Nodes to Cluster

SERVER 4 SERVER 5

Replica

Active

Replica

Active

Read/write/update

App Server 1

COUCHBASE Client Library !CLUSTER MAP

COUCHBASE Client Library !CLUSTER MAP

App Server 2

User Configured Replica Count = 1

Couchbase Server Cluster

Active

SERVER 1

Doc 5

Doc 2

Doc 9

Doc

Doc

Doc

Replica

Doc 4

Doc 1

Doc 8

Doc

Doc

Doc

Active

SERVER 2

Doc 4

Doc 7

Doc 8

Doc

Doc

Doc

Replica

Doc 6

Doc 3

Doc 2

Doc

Doc

Doc

Active

SERVER 3

Doc 1

Doc 3

Doc 6

Doc

Doc

Doc

Replica

Doc 7

Doc 9

Doc 5

Doc

Doc

Doc

Read/write/update

• Two servers added withone-click operation

• Docs automatically rebalance across cluster

Even distribution of docs Minimum doc movement

• Cluster map updated • App database

calls now distributed over larger number of servers

Page 13: From 0 to syncing

‹#›Couchbase, Inc. ConfidentialCouchbase, Inc. Confidential

Fail Over Node

User Configured Replica Count = 1

SERVER 4 SERVER 5

Replica

Active

Replica

Active

App Server 1

COUCHBASE Client Library !CLUSTER MAP

COUCHBASE Client Library !CLUSTER MAP

App Server 2

Couchbase Server Cluster

Active

SERVER 1

Doc 5

Doc 2

Doc 9Doc

Doc

Doc

Replica

Doc 4

Doc 1

Doc 8Doc

Doc

Doc

Active

SERVER 2

Doc 4

Doc 7 Doc 8

Doc

Doc Doc

Replica

Doc 6

Doc 3 Doc 2

Doc

Doc Doc

Active

SERVER 3

Doc 1

Doc 3

Doc 6Doc

Doc

Doc

Replica

Doc 7

Doc 9

Doc 5Doc

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

Doc 1 Doc 3

Doc

Page 14: From 0 to syncing

What is Couchbase Lite

Page 15: From 0 to syncing

JSON  Anywhere

On In Data In Data

Page 16: From 0 to syncing

TradiBonal  Mobile

ApplicaBon  ServerREST  Services

Complex

Complex Sync Code While Offline or Cannot Use App

While Offline

Construction/Deconstruction of

JSON

RDBMS Style Local Storage

Page 17: From 0 to syncing

Mobile  DBaaS

DBaaSREST  Services

RDBMS Style Local Storage

Complex Sync Code

Scaling

Simple

Page 18: From 0 to syncing

JSON  Anywhere  -­‐  Automated  Sync

Synchronization Automated when

Connectivity Present

Page 19: From 0 to syncing

Couchbase  LiteThe  only  NoSQL  Database  for  Mobile  Devices

Couchbase*Lite**iOS*

Couchbase*Lite**

Android*

Lite

Features  Ultra-­‐lightweight,  secure  JSON  database    Na1ve  support  for  iOS,  Android  and  

REST/HTML5  Powerful  conflict  resolu1on  Automated  Sync  

Page 20: From 0 to syncing

Couchbase  LiteThe  only  NoSQL  Database  for  Mobile  Devices

Couchbase*Lite**iOS*

Couchbase*Lite**

Android*

Lite

Features  Ultra-­‐lightweight,  secure  JSON  database    Na1ve  support  for  iOS,  Android  and  

REST/HTML5  Powerful  conflict  resolu1on  Automated  Sync  

Page 21: From 0 to syncing

Couchbase  Lite  Features

• Use  JSON  in  your  Mobile  App  Data  Tier  

• Always  Work  with  Local  Data  on  Device  

• Modify  schema  dynamically,  super  flexible  

• When  Connected:    Create  New  Data  and  Change  Documents,  they  Sync  AutomaBcally  

• When  Not  Connected:  Work  with  solid  local  NoSQL  database  

• Powerful  conflict  resoluBon  features

Page 22: From 0 to syncing

The Couch syncing history

Page 23: From 0 to syncing

Remember this?

Page 24: From 0 to syncing

CouchDB replication

• Uses  the  CouchDB  replicaBon  protocol  

• ReplicaBon  via  HTTP  

• Proven  to  work  nicely

Page 25: From 0 to syncing

Couchbase Sync Gateway

CouchDB  replica1on  with  Couchbase

Page 26: From 0 to syncing

So what can we do

• Handle  user  authenBcaBon  

• Handle  document  channels  

• Synchronise  mulBple  devices  /  applicaBons

Page 27: From 0 to syncing

channel()

Channels  organize  what  JSON  Documents  are  synchronized  down  to  which  Users

User  A

User  B

User  C

JSONdoc.channels  :  “food”

JSONdoc.channels  :  [  “food”,  “sports”  ]

JSONdoc.channels  :  [  “sports”,  “travel”  ]

channels:  food

channels:  sports

channels:  food,  travel

User  D  creates  Content  

with  Channel  associaBonThese  Users  Subscribed    

to  Content  Channels

Page 28: From 0 to syncing

User Authentication?

• Facebook  

• Mozilla  Persona  

• OAuth  

• Username  /  Password

Page 29: From 0 to syncing

Setting everything up

• Install  Sync  Gateway  

• Setup  a  Sync  Gateway  config  

• Setup  the  Couchbase  Bucket  to  use  

• Install  CouchbaseLite  (Cocoa  Pods  anybody?)

Page 30: From 0 to syncing

DEMO

Page 31: From 0 to syncing
Page 32: From 0 to syncing

Ok that was easy, right?

Page 33: From 0 to syncing

Let’s build a TodoApp

Page 34: From 0 to syncing

What do we need?

• Manage  lists  and  tasks  

• A  datastore  wired  to  a  view  

• User  login  

• Offline  funcBonality  

• Cross  device  usablilty

Page 35: From 0 to syncing

Demo

Page 36: From 0 to syncing

Models…

@interface Titled : CBLModel !+ (NSString*) docType; !- (instancetype) initInDatabase: (CBLDatabase*)database withTitle: (NSString*)title; !@property (copy) NSString* title; @property (readonly) NSDate* created_at; !@end

Page 37: From 0 to syncing

Listing lists…

// Returns a query for all the lists in a database. + (CBLQuery*) queryListsInDatabase: (CBLDatabase*)db { CBLView* view = [db viewNamed: @"lists"]; if (!view.mapBlock) { [view setMapBlock: MAPBLOCK({ if ([doc[@"type"] isEqualToString:kListDocType]) emit(doc[@"title"], nil); }) reduceBlock: nil version: @"1"]; } return [view createQuery]; }

Page 38: From 0 to syncing

Setting up the model

[[self.database modelFactory] registerClass:[List class] forDocumentType:@"list"];

Page 39: From 0 to syncing

Wire up the Datastore

• Implements  a  datasource  for  TableView  

• Monitors  any  changes

Page 40: From 0 to syncing

Wire up the Datastore#import <UIKit/UIKit.h> #import <Couchbaselite/CBLUITableSource.h> !@class DetailViewController; !@interface MasterViewController : UIViewController <CBLUITableDelegate> !@property (strong, nonatomic) DetailViewController *detailViewController; @property (nonatomic) IBOutlet CBLUITableSource* dataSource; @property (strong, nonatomic) IBOutlet UITableView *tableView; !@end

Page 41: From 0 to syncing

User Login

• Handled  via  Facebook  

• Na?ve  Social  Login  handling  in  iOS

Page 42: From 0 to syncing

Setting up Sync

- (void)defineSync { pull = [_database createPullReplication:_remoteURL]; pull.continuous = YES; push = [_database createPushReplication:_remoteURL]; push.continuous = YES; [self listenForReplicationEvents: push]; [self listenForReplicationEvents: pull]; [_authenticator registerCredentialsWithReplications: @[pull, push]]; }

Page 43: From 0 to syncing

Questions?

Page 44: From 0 to syncing

Links

• CouchbaseMobile  

• Code  for  TodoLite  

• Code  for  Sync  Hello  World  

• Introduc?on  to  Sync  Gateway  Video  

• Sync  Gateway  Homebrew  Formula