From 0 to syncing
in 30 min
Ma#hew	
  Revell	
  @ma#hewrevell	
  
Philipp	
  Fehre	
  @ischi
Introduction Couchbase
Couchbase 101
• Distributed	
  key-­‐value	
  store	
  
• Create	
  secondary	
  indexed	
  views	
  using	
  Map	
  Reduc...
Couchbase	
  Organiza1on
• Couchbase	
  operates	
  like	
  a	
  Key-­‐Value	
  Document	
  Store	
  
• Key	
  is	
  a	
  ...
Couchbase Server 2.0 Architecture
New	
  Persistence	
  Layer
storage	
  interface
Couchbase	
  EP	
  Engine
11210	
  
Mem...
Supported SDK's
www.couchbase.com/communiBes
• Each	
  supported	
  SDK	
  page	
  has	
  instruc1ons	
  for	
  setup	
  
...
‹#›Couchbase, Inc. Confidential
Easy Scalability
XDCR
Single Node
Type
No Manual Sharding
!
Database manages
data movement...
‹#›Couchbase, Inc. Confidential
Consistent, High Performance
Fine
Grained
Locking
Built-in
Cache
Hash
Partitioning
Allows ...
‹#›Couchbase, Inc. Confidential
Always on 24x7 Capability

All admin operations
online
!
•Compaction
•Indexing
•Rebalance
...
‹#›Couchbase, Inc. Confidential
Flexible Data Model
Represent data as
objects instead of
shredding into rows
and columns
!...
‹#›Couchbase, Inc. ConfidentialCouchbase, Inc. Confidential
Couchbase Server Cluster
Basic Operation
User Configured Repli...
‹#›Couchbase, Inc. ConfidentialCouchbase, Inc. Confidential
Add Nodes to Cluster
SERVER 4 SERVER 5
Replica
Active
Replica
...
‹#›Couchbase, Inc. ConfidentialCouchbase, Inc. Confidential
Fail Over Node
User Configured Replica Count = 1
SERVER 4 SERV...
What is Couchbase Lite
JSON	
  Anywhere
On In DataIn Data
TradiBonal	
  Mobile
ApplicaBon	
  Server
REST	
  Services
Complex
Complex Sync
Code While Offline
or Cannot Use App
While ...
Mobile	
  DBaaS
DBaaS
REST	
  Services
RDBMS Style
Local Storage
Complex Sync Code
Scaling
Simple
JSON	
  Anywhere	
  -­‐	
  Automated	
  Sync
Synchronization
Automated when
Connectivity Present
Couchbase	
  Lite
The	
  only	
  NoSQL	
  Database	
  for	
  Mobile	
  Devices
Couchbase*
Lite**
iOS*
Couchbase*
Lite**
An...
Couchbase	
  Lite
The	
  only	
  NoSQL	
  Database	
  for	
  Mobile	
  Devices
Couchbase*
Lite**
iOS*
Couchbase*
Lite**
An...
Couchbase	
  Lite	
  Features
• Use	
  JSON	
  in	
  your	
  Mobile	
  App	
  Data	
  Tier	
  
• Always	
  Work	
  with	
 ...
The Couch syncing history
Remember this?
CouchDB replication
• Uses	
  the	
  CouchDB	
  replicaBon	
  protocol	
  
• ReplicaBon	
  via	
  HTTP	
  
• Proven	
  to	...
Couchbase Sync Gateway
CouchDB	
  replica1on	
  with	
  Couchbase
So what can we do
• Handle	
  user	
  authenBcaBon	
  
• Handle	
  document	
  channels	
  
• Synchronise	
  mulBple	
  de...
channel()
Channels	
  organize	
  what	
  JSON	
  Documents	
  are	
  synchronized	
  
down	
  to	
  which	
  Users
User	
...
User Authentication?
• Facebook	
  
• Mozilla	
  Persona	
  
• OAuth	
  
• Username	
  /	
  Password
Setting everything up
• Install	
  Sync	
  Gateway	
  
• Setup	
  a	
  Sync	
  Gateway	
  config	
  
• Setup	
  the	
  Couc...
DEMO
Ok that was easy, right?
Let’s build a TodoApp
What do we need?
• Manage	
  lists	
  and	
  tasks	
  
• A	
  datastore	
  wired	
  to	
  a	
  view	
  
• User	
  login	
 ...
Demo
Models…
@interface Titled : CBLModel
!
+ (NSString*) docType;
!
- (instancetype) initInDatabase: (CBLDatabase*)database
wi...
Listing lists…
// Returns a query for all the lists in a database.
+ (CBLQuery*) queryListsInDatabase: (CBLDatabase*)db {
...
Setting up the model
[[self.database modelFactory] registerClass:[List class]
forDocumentType:@"list"];
Wire up the Datastore
• Implements	
  a	
  datasource	
  for	
  TableView	
  
• Monitors	
  any	
  changes
Wire up the Datastore
#import <UIKit/UIKit.h>
#import <Couchbaselite/CBLUITableSource.h>
!
@class DetailViewController;
!
...
User Login
• Handled	
  via	
  Facebook	
  
• Na?ve	
  Social	
  Login	
  handling	
  in	
  iOS
Setting up Sync
- (void)defineSync {
pull = [_database createPullReplication:_remoteURL];
pull.continuous = YES;
push = [_...
Questions?
Links
• CouchbaseMobile	
  
• Code	
  for	
  TodoLite	
  
• Code	
  for	
  Sync	
  Hello	
  World	
  
• Introduc?on	
  to	...
From 0 to syncing
Upcoming SlideShare
Loading in …5
×

From 0 to syncing

525 views

Published on

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.

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

No Downloads
Views
Total views
525
On SlideShare
0
From Embeds
0
Number of Embeds
12
Actions
Shares
0
Downloads
9
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

From 0 to syncing

  1. 1. From 0 to syncing in 30 min Ma#hew  Revell  @ma#hewrevell   Philipp  Fehre  @ischi
  2. 2. Introduction Couchbase
  3. 3. 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
  4. 4. 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
  5. 5. 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 Query  Engine HTTP   8091 Erlang  port  mapper   4369 Distributed  Erlang   21100  -­‐  21199 Heartbeat Process  monitor Global  singleton  supervisor Configura1on  manager on  each  node Rebalance  orchestrator Node  health  monitor one  per  cluster vBucket  state  and  replica1on  manager h#p REST  management  API/Web  UI Erlang/OTP Server/Cluster   Management  &   CommunicaBon   (Erlang) RAM  Cache,  Indexing   &  Persistence   Management   (C)
  6. 6. Supported SDK's www.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.
  7. 7. ‹#›Couchbase, Inc. Confidential Easy Scalability XDCR Single 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
  8. 8. ‹#›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
  9. 9. ‹#›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
  10. 10. ‹#›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 JSON JSON JSON JSONJSON 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
  11. 11. ‹#›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
  12. 12. ‹#›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 with
 one-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

  13. 13. ‹#›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
  14. 14. What is Couchbase Lite
  15. 15. JSON  Anywhere On In DataIn Data
  16. 16. TradiBonal  Mobile ApplicaBon  Server REST  Services Complex Complex Sync Code While Offline or Cannot Use App While Offline Construction/ Deconstruction of JSON RDBMS Style Local Storage
  17. 17. Mobile  DBaaS DBaaS REST  Services RDBMS Style Local Storage Complex Sync Code Scaling Simple
  18. 18. JSON  Anywhere  -­‐  Automated  Sync Synchronization Automated when Connectivity Present
  19. 19. Couchbase  Lite The  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  
  20. 20. Couchbase  Lite The  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  
  21. 21. 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
  22. 22. The Couch syncing history
  23. 23. Remember this?
  24. 24. CouchDB replication • Uses  the  CouchDB  replicaBon  protocol   • ReplicaBon  via  HTTP   • Proven  to  work  nicely
  25. 25. Couchbase Sync Gateway CouchDB  replica1on  with  Couchbase
  26. 26. So what can we do • Handle  user  authenBcaBon   • Handle  document  channels   • Synchronise  mulBple  devices  /  applicaBons
  27. 27. channel() Channels  organize  what  JSON  Documents  are  synchronized   down  to  which  Users User  A User  B User  C JSON doc.channels  :  “food” JSON doc.channels  :  [  “food”,  “sports”  ] JSON doc.channels  :  [  “sports”,  “travel”  ] channels:  food channels:  sports channels:  food,  travel User  D  creates  Content   with  Channel  associaBon These  Users  Subscribed     to  Content  Channels
  28. 28. User Authentication? • Facebook   • Mozilla  Persona   • OAuth   • Username  /  Password
  29. 29. Setting everything up • Install  Sync  Gateway   • Setup  a  Sync  Gateway  config   • Setup  the  Couchbase  Bucket  to  use   • Install  CouchbaseLite  (Cocoa  Pods  anybody?)
  30. 30. DEMO
  31. 31. Ok that was easy, right?
  32. 32. Let’s build a TodoApp
  33. 33. What do we need? • Manage  lists  and  tasks   • A  datastore  wired  to  a  view   • User  login   • Offline  funcBonality   • Cross  device  usablilty
  34. 34. Demo
  35. 35. Models… @interface Titled : CBLModel ! + (NSString*) docType; ! - (instancetype) initInDatabase: (CBLDatabase*)database withTitle: (NSString*)title; ! @property (copy) NSString* title; @property (readonly) NSDate* created_at; ! @end
  36. 36. 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]; }
  37. 37. Setting up the model [[self.database modelFactory] registerClass:[List class] forDocumentType:@"list"];
  38. 38. Wire up the Datastore • Implements  a  datasource  for  TableView   • Monitors  any  changes
  39. 39. 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
  40. 40. User Login • Handled  via  Facebook   • Na?ve  Social  Login  handling  in  iOS
  41. 41. 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]]; }
  42. 42. Questions?
  43. 43. Links • CouchbaseMobile   • Code  for  TodoLite   • Code  for  Sync  Hello  World   • Introduc?on  to  Sync  Gateway  Video   • Sync  Gateway  Homebrew  Formula

×