webinar- couchbase mobile deep dive

31

Upload: couchbase

Post on 20-Aug-2015

638 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Webinar- Couchbase Mobile Deep Dive
Page 2: Webinar- Couchbase Mobile Deep Dive

Couchbase  Mobile    Deep  Dive

Jens  Al4e  

[email protected]

V1.0 - 6/17/14

Page 3: Webinar- Couchbase Mobile Deep Dive

©2009  Helder  da  Rocha

Page 4: Webinar- Couchbase Mobile Deep Dive

Architecture  of  Couchbase  Lite  and  &  Sync  Gateway

• Document  data  store  Mul?-­‐Version  Concurrency  Control  (MVCC)  

• Replica=on  protocol  Conflict  handling  

• Querying/filtering  Map/reduce  views  

• Client  APIs  REST  

Na?ve

Page 5: Webinar- Couchbase Mobile Deep Dive

Document  Store

Page 6: Webinar- Couchbase Mobile Deep Dive

Document  Database

• Persistent  map  from  IDs  →  documents  

• where  documents  are  JSON  objects  

• …plus  aLachments  Extra  set  of  proper?es  mapping  name  →  {binary  data,  MIME  type}  

• …with  mul=ple  revisions

Page 7: Webinar- Couchbase Mobile Deep Dive

Mul=-­‐Version  Concurrency  Control

• Each  document  has  a  revision  history  

• Each  revision’s  ID  has  a  genera=on  number  and  a  digest  e.g.  12-­‐c8c993737bf3e800b0a3d2e864c2039d  

• Dele=ons  are  just  special  revisions  “_deleted”:  true  

• Every  doc  update  must  specify  its  parent  revision  ID

Page 8: Webinar- Couchbase Mobile Deep Dive

Sequence  Index

• Every  revision  added  gets  a  sequence  number  

• Sequence  numbers  are  assigned  consecu=vely  

• Local  to  a  single  database  

• Documents  are  indexed  by  sequence  number  Abstract  ?meline  of  the  database’s  history  

Allows  asking  “what  changed  since  sequence  ____?”

Page 9: Webinar- Couchbase Mobile Deep Dive

Replica=on

Page 10: Webinar- Couchbase Mobile Deep Dive

What  Replica=on  Does

• Transfers  changed  documents  from  a  source  to  a  target  database  

• Incremental  

• Asynchronous  

• Peer-­‐to-­‐peer  

• Semi-­‐op=mized  Doesn’t  send  any  revision  that  the  target  already  has  

Doesn’t  send  any  adachment  version  that  the  target  already  has  

But  does  send  en?re  JSON  docs  &  adachments  (no  delta  compression)

Page 11: Webinar- Couchbase Mobile Deep Dive

Replica=on  Conflicts

• Conflicts  are  inevitable,  per  the  CAP  Theorem  

• Replica=ng  conflic=ng  revision  histories  produces  a  tree  

• The  document  model  allows  for  conflicts  A  conflict  is  a  valid  state  of  a  document  

• Conflict  resolu=on  is  the  app’s  job  Only  the  app  needs  to  care  about  resolving  conflicts  

App  can  use  any  logic  it  wants  to  pick  winners  or  merge  changes  

Resolu?on  creates  new  revisions  that  are  replicated

Jens — This is too

confusing. We really

need a diagram

here! Thx.

Page 12: Webinar- Couchbase Mobile Deep Dive

Replica=on  Conflicts  Cause  Branches

1-­‐abcd 2-­‐c124

1-­‐abcd 2-­‐c124 3-­‐beef 1-­‐abcd 2-­‐c124 3-­‐fade

1-­‐abcd 2-­‐c124

3-­‐beef

3-­‐fade

Page 13: Webinar- Couchbase Mobile Deep Dive

4-­‐dead

4-­‐feed

Resolving  a  Conflict

1-­‐abcd 2-­‐c124

3-­‐beef

3-­‐fade

Page 14: Webinar- Couchbase Mobile Deep Dive

Replica=on  Conflicts

• Conflicts  are  inevitable,  per  the  CAP  Theorem  

• Replica=ng  conflic=ng  revision  histories  produces  a  tree  

• The  document  model  allows  for  conflicts  A  conflict  is  a  valid  state  of  a  document  

• Conflict  resolu=on  is  the  app’s  job  Only  the  app  needs  to  care  about  resolving  conflicts  

App  can  use  any  logic  it  wants  to  pick  winners  or  merge  changes  

Resolu?on  creates  new  revisions  that  are  replicated

Page 15: Webinar- Couchbase Mobile Deep Dive

Replica=on  and  Peer-­‐to-­‐Peer

• It’s  more  than  just  push/pull  to  a  server  

• Any  two  databases  can  replicate  if  they  can  connect  Couchbase  Lite’s  “Listener”  component  allows  connec?ons  

• Arbitrary  topologies  (trees,  meshes)  are  supported

Page 16: Webinar- Couchbase Mobile Deep Dive

Replica=on  Protocol

• Extension  of  REST  API  

• One  ac=ve  partner  (client),  one  passive  (server)  

• Ac=ve  partner  can  push  or  pull  

• State  (checkpoint)  is  stored  redundantly  on  both  sides

Page 17: Webinar- Couchbase Mobile Deep Dive

Pull  Algorithm

• Verify  local  &  remote  checkpoints  

• Request  the  changes  since  checkpointed  remote  sequence  →  array  of  {sequence,  doc-­‐id,  rev-­‐id}  tuples  

• Determine  which  document  revisions  do  not  exist  locally  

• Download  leaf  revision(s)  of  each  such  document  Including  revision  history  

Skipping  unmodified  adachments  

• Update  the  checkpoints  

• In  con1nuous  mode,  keep  monitoring  remote  changes

Page 19: Webinar- Couchbase Mobile Deep Dive

Filtered  Replica=on

• Implemented  in  Couchbase  Lite,  CouchDB,  PouchDB,  etc.  

• App-­‐defined  filter  func=on  can  accept  or  skip  each  outgoing  revision  

• Always  runs  on  source  side  of  replica=on  Push:  on  client  side  

Pull:  on  server  side  

• Simple,  but  not  super  scaleable

Page 20: Webinar- Couchbase Mobile Deep Dive

Channel-­‐Based  Replica=on

• Implemented  by  Couchbase  Sync  Gateway  

• Documents  are  tagged  with  channel  IDs  

• User  accounts  have  access  to  sets  of  channels  

• Documents  are  indexed  by  channel  

• Pull  replica=ons  efficiently  look  up  docs  in  available  channels

Tell people to watch

the Sync Gateway

talk next week for

more details!

Page 21: Webinar- Couchbase Mobile Deep Dive

Map/Reduce  Views

Page 22: Webinar- Couchbase Mobile Deep Dive

Anatomy  of  a  View

• A  view  belongs  to  a  database,  has  a:  Name  

Map  func?on  

Reduce  func?on  (op?onal)  

Persistent  index  

Checkpoint  sequence  

• Views  exist  in:  Couchbase  Lite  

Couchbase  Server  

not  Sync  Gateway  

Also  CouchDB,  PouchDB,  etc.FYI: Last week’s iOS

webinar had better

slides explaining this

stuff.

Page 23: Webinar- Couchbase Mobile Deep Dive

Indexing  a  View

• For  each  document  updated  since  checkpoint  sequence:  1. Remove  all  index  rows  previously  generated  by  that  document  

2. Call  map  func?on  with  current  document  proper?es  as  parameter  

3. Each  ?me  emit()  is  called,  add  {key,  value,  docID}  tuple  to  the  index  

• Save  the  current  sequence  number  as  the  checkpoint  

• The  keys  in  the  index  are  sorted  as  JSON

Page 24: Webinar- Couchbase Mobile Deep Dive

Querying  a  View

• Is  its  checkpoint  sequence  the  database’s  latest  sequence?  If  not,  update  the  index  (as  per  previous  slide)  

This  can  be  suppressed  by  sejng  query’s  IndexUpdateMode  

• Fetch  a  range  of  {key,  value,  docID}  tuples  from  the  index  Honoring  startKey,  endKey,  skip,  limit,  reverse

Page 25: Webinar- Couchbase Mobile Deep Dive

Client  APIs

Page 26: Webinar- Couchbase Mobile Deep Dive

REST  API  —  The  Classic

• Originally  defined  by  Apache  CouchDB  

• Supported  by  Sync  Gateway  (with  slight  differences)  Server  side  of  replica?on  

API  for  app  servers  

• Supported  by  Couchbase  Lite  Listener  For  JavaScript  app  code  (e.g.  PhoneGap)  

Server  side  of  peer-­‐to-­‐peer  replica?on

Page 27: Webinar- Couchbase Mobile Deep Dive

REST  API

• Document  access  (CRUD):  PUT  /  GET  /  DELETE  

/dbname/docid  

• Query  a  view:  POST  /dbname/_design/name/_view/name  

• Replicate:  POST  /dbname/_replicate

Page 28: Webinar- Couchbase Mobile Deep Dive

Na=ve  APIs  by  Plalorm

• Objec=ve-­‐C:  iOS,  Mac  OS  X  

• Java:  Android,  Linux  

• C#:  Windows,  Windows  Mobile,  Mono          BETA  

• Common  API  design  across  languages  We  have  a  single  carefully  designed  language-­‐neutral  API  spec  

Matching  class  names,  method  names,  seman?cs  

Slight  differences,  to  remain  idioma?c

Point them to the

“Development with

Android / iOS”

webinars.

Page 29: Webinar- Couchbase Mobile Deep Dive

Na=ve  API:  Key  Classes

• Manager  

• Database  

• Document  

• View  

• Query  

• Replica=on

Page 30: Webinar- Couchbase Mobile Deep Dive

Oops, that’s all I wrote. Stall for time — maybe answer some questions?

Page 31: Webinar- Couchbase Mobile Deep Dive

THE  END

www.couchbase.com/webinars  

developer.couchbase.com/mobile/