Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Couchbase Lite Strange Loop

Chris Anderson
September 20, 2013

Couchbase Lite Strange Loop

Sync is the future of mobile data, and the best way to power sync is with an embedded mobile database. In this talk I describe Couchbase Lite and then I get geeky with the data-structures we use for conflict detection and management. If you want to write your own offline capable database, you'll end up using data structures like this.

Chris Anderson

September 20, 2013
Tweet

More Decks by Chris Anderson

Other Decks in Technology

Transcript

  1. JSON  Anywhere   Couchbase  Server • JSON  on  the  device

    ­ Developers   increasingly  prefer   NoSQL  database • JSON  on  the  wire ­ No  need  for  data   transforma9on • JSON  in  the  cloud ­ Flexible  data  model ­ High  performance ­ Easy  scalability JS N JS N JS N Friday, September 20, 13
  2. Architecture Server  3 Server  1 Server  2 Couchbase  Server Channel

    Sync  Gateway Channel Channel Sync  Gateway Channel Couchbase  Lite  for  iOS  and  Android On Premise In the cloud Friday, September 20, 13
  3. Couchbase  Lite The  only  NoSQL  Database  for  Mobile  Devices •

    Features ­ Ultra-­‐lightweight,  secure  JSON   database   ­ Na9ve  support  for  iOS,  Android   and  REST/HTML5 ­ Full  document,  index  and   querying  and  sync  capabili9es ­ Powerful  conflict  resolu9on Couchbase   Lite   Android Couchbase   Lite   iOS Friday, September 20, 13
  4. What  you  get Couchbase   Lite   Android Couchbase  

    Lite   iOS ­ Enable  new  class  of  rich  data   intensive  local  applica9ons   ­ Rapid  development  using   na9ve  JSON  data ­ Highly  responsive  interac9ve   applica9ons ­ Always  available  -­‐    online  or   offline Friday, September 20, 13
  5. Collaborate  using  Channels • For  each  document,  you  specify  a

     set  of   channels  it  belongs  to • For  each  user  or  device,  you  control  which   channels  they  can  access • Replicate  only  a  subset  of  documents  down  to   the  device ­ User-­‐defined  filter  func9ons   ­ Simply  lets  you  know  whether  a  document  should  be  replicated • And  you  can  authen@cate  users   Friday, September 20, 13
  6. How  do  I  get  started? Zero to sync in 5

    minutes !!! Friday, September 20, 13
  7. Look  at  the  code  JSON  /  REST  interface -­‐CRUD  opera9ons

     use  HTTP   verbs -­‐Works  with  libraries  like   jQuery  and  Backbone Live  Query  UI  Updates -­‐UI  updated  to  reflect   database  changes,  even  for   remote  changes -­‐HTTP  long  poll  API  to   support  quick  redraw Collaborate  via  Cloud -­‐social  network  login -­‐programmable  sync   func9on  for  update   valida9on  and  channel   rou9ng Friday, September 20, 13
  8. All  Todo  Lists • Rendered  with  HTML   and  JavaScript

    • View  query  to  show  all   the  lists  on  a  device. • Form  submit  event   creates  a  document   represen@ng  a  list. Friday, September 20, 13
  9. Task  List  -­‐  Items • Toggle  a  checkbox • Add

     a  photo • Live  update Friday, September 20, 13
  10. Toggle  a  Task  Checkbox by  upda@ng  the  JSON  Document Uses

     a  familiar  REST  paradigm.  GET  and  PUT  used  here. Friday, September 20, 13
  11. Live  Update  UI Local  and  remote  updates  both  trigger  a

     database  event,  used  for  redraw. Friday, September 20, 13
  12. Sign-­‐in  to  sync  and  collaborate • Example  uses  Facebook,  we

      support  a  wide  variety  of   authen@ca@on  op@on • Pick  from  the  list  of  signed  in   users  when  sharing Friday, September 20, 13
  13. Sync  Func@on • Applica@on  code  run   in  the  cloud

    • Access  control • Channel  rou@ng • Update  Valida@on Your  cloud  in  one  page  of  code. Friday, September 20, 13
  14. Task  documents  routed  to  channels • Must  have  a  list_id

    • Routed  to  a  channel  based  on  list_id Friday, September 20, 13
  15. List  documents  grant  channel  access • Must  have  an  owner

    • Owner  can’t  change • Owner  and  members   can  access  channel • List  is  on  it’s  own   channel Friday, September 20, 13
  16. Revision  Trees {counter,  content  hash} 4-­‐3a14f7d8 5-­‐d7ad89bd 4 4 4

    3 3 3 2 2 2 3 1 winner 1 conflic@ng   version Friday, September 20, 13
  17. Revision  Trees 1 2 3 1 2 3 4 4

    Friday, September 20, 13
  18. Revision  Trees 1 2 3 1 2 3 4 4

    4 Friday, September 20, 13
  19. Revision  Trees 1 2 3 1 2 3 4 4

    4 Friday, September 20, 13
  20. Revision  Trees 1 2 3 4 1 2 3 4

    4 4 Friday, September 20, 13
  21. Revision  Trees 1 2 3 4 4 1 2 3

    4 4 Friday, September 20, 13
  22. Revision  Trees 1 2 3 1 2 3 4 4

    4 4 1 2 3 4 3 4 Friday, September 20, 13
  23. Revision  Trees 1 2 3 1 2 1 2 3

    4 4 4 4 4 3 3 4 Friday, September 20, 13
  24. Revision  Trees 1 2 3 1 2 1 2 3

    4 4 4 4 4 3 3 4 5 4 Friday, September 20, 13
  25. Revision  Trees 1 2 3 1 2 1 2 3

    4 4 4 3 3 4 5 5 4 4 Friday, September 20, 13
  26. Revision  Trees 1 2 3 1 2 1 2 4

    4 3 3 4 5 5 3 3 4 4 5 4 4 Friday, September 20, 13