Introduction to Couchbase Mobile: Couchbase Connect 2014

42
Introduction to Couchbase Lite Traun Leyden | Couchbase Lite Android Developer, Couchbase

description

Video available at: http://youtu.be/EaSqU3oQ0q0 Speaker: Traun Leyden, Couchbase This talk will present an overview of Couchbase Lite on the iOS and Android platforms. * A demonstration of an equivalent iOS and Android app that is able to sync data between two devices. * An overview of the major components of Couchbase Lite: - Sync - Documents - Views and Queries * A description of how document conflict resolution works. * A discussion of the features of the differences between iOS and Android and the roadmap to API parity. * An overview of the resources and sample projects available. By the end of the talk you should have a high enough level of understanding to design and build your own app on top of Couchbase Lite.

Transcript of Introduction to Couchbase Mobile: Couchbase Connect 2014

Page 1: Introduction to Couchbase Mobile: Couchbase Connect 2014

Introduction to Couchbase LiteTraun Leyden | Couchbase Lite Android Developer,

Couchbase

Page 2: Introduction to Couchbase Mobile: Couchbase Connect 2014

©2014 Couchbase, Inc.

Here’s what I’m going to cover:

Overview of the problem

A demo of Couchbase Lite

An overview of Couchbase Lite

A deep dive into Couchbase Lite core concepts

Resources

Q&A

Overview

2

Page 3: Introduction to Couchbase Mobile: Couchbase Connect 2014

Where should you store your mobile app data?

Local Only Remote Only Local and Remote

Page 4: Introduction to Couchbase Mobile: Couchbase Connect 2014

©2014 Couchbase, Inc.

Implications of Data Location choice

4

Data

Location

Network

Requirement

Application

Availability

Application

Responsiveness

Enabling

Technology

Remote

Only

Always

Connected

Sometimes

AvailableVariable

REST Services

(JSON)

Local

Only

Never

Connected

Always

Available

Always

FastSQLite

Local

& Remote

Sometimes

Connected

Always

Available

Always

Fast?

1 2 3 4 5

Page 5: Introduction to Couchbase Mobile: Couchbase Connect 2014

©2014 Couchbase, Inc.

Narrowing down Data Location options

5

Data

Location

Network

RequirementApplication Availability

Application

Responsiveness

Remote

Only

Local

Only

(maybe in 90s)

Local

& Remote

Page 6: Introduction to Couchbase Mobile: Couchbase Connect 2014

©2014 Couchbase, Inc.

Narrowing down Data Location options

6

Data

Location

Network

RequirementApplication Availability

Application

Responsiveness

Remote

Only

Always

Connected

(unrealistic)

Sometimes

Available

(bad)

Variable

(bad)

Local

Only

(maybe in 90s)

Local

& Remote

Sometimes

Connected

Always

Available

Always

Fast

Page 7: Introduction to Couchbase Mobile: Couchbase Connect 2014

©2014 Couchbase, Inc.

Requirements:

Always fast — because the network is taken out of the critical path

Always available — for the same reason

Data location constraint:

Local & Remote.

Local so that the application is always available and fast

Remote so that it can be shared with other users

Requirements recap

7

Page 8: Introduction to Couchbase Mobile: Couchbase Connect 2014

©2014 Couchbase, Inc.

Requirement simplification: treat the local data as “read-only” cached copy.

For example, a Twitter app

Remote data is pulled and stored locally in SQLite

The app shows the data from SQLite

No network — no problem

Writes (new Tweets) require an internet connection

Once the write is pushed to the server, it is stored locally

Simple enough to build from scratch

Building an app that meets the requirements

8

Page 9: Introduction to Couchbase Mobile: Couchbase Connect 2014

©2014 Couchbase, Inc.

But what if you need to build an Evernote clone rather than a Twitter app?

New requirements:

The user can add / edit / delete notes while completely offline

If online, but network is slow, modifying notes must still be fast

The user can even edit the same note on two different offline devices

and ...

Adding a requirement: user writes to local data store

9

Page 10: Introduction to Couchbase Mobile: Couchbase Connect 2014

©2014 Couchbase, Inc.

Adding a requirement: user writes to local data store

10

Your boss wants it done by the end of this sprint!

Page 11: Introduction to Couchbase Mobile: Couchbase Connect 2014

©2014 Couchbase, Inc.

Why is this hard?

The “local read only” storage no longer works

You no longer have a single master — it’s now a multi-master system

Deletes are tricky

Conflicts are tricky

You’ll need to build a full-fledged “sync engine”

Adding a requirement: user writes to local data store

11

You probably know what’s coming next …

Page 12: Introduction to Couchbase Mobile: Couchbase Connect 2014

We’ve already built this for you

Demo

Page 13: Introduction to Couchbase Mobile: Couchbase Connect 2014

Couchbase Lite Overview

Page 14: Introduction to Couchbase Mobile: Couchbase Connect 2014

©2014 Couchbase, Inc.

Couchbase Lite Architecture

14

Sync Gateway

Couchbase

Lite

Couchbase

Lite

App

Lite

App

Lite

HTTP

Sync

Page 15: Introduction to Couchbase Mobile: Couchbase Connect 2014

©2014 Couchbase, Inc.

Couchbase Lite Architecture

15

Peer to Peer

HTTP

Sync

Couchbase

Lite

Couchbase

Lite

AppLite

AppLite

Page 16: Introduction to Couchbase Mobile: Couchbase Connect 2014

©2014 Couchbase, Inc.

Supported Couchbase Lite Platforms

Native iOS / OSX

Native Android

Native Java (Linux, OSX, Windows)

Native C# -> Native iOS / Android / Windows Phone via Xamarin

Phonegap

Titanium

Couchbase Lite Architecture

16

(100% Open Source — Apache 2)

Page 17: Introduction to Couchbase Mobile: Couchbase Connect 2014

©2014 Couchbase, Inc.

Couchbase Lite core functionality

Document CRUD

Attachments

Views / Queries (MapReduce)

Replication

Pluggable “change listener” to monitor DB events

REST API (PhoneGap)

Couchbase Lite Overview

17

Page 18: Introduction to Couchbase Mobile: Couchbase Connect 2014

©2014 Couchbase, Inc.

Couchbase Lite extended functionality - Part I

REST API Listener (iOS, Android)

Javascript Based Views (iOS, Android)

Couchbase Lite Overview

18

Page 19: Introduction to Couchbase Mobile: Couchbase Connect 2014

©2014 Couchbase, Inc.

Couchbase Lite extended functionality, Part II

Model layer (iOS)

Full Text Indexing (iOS)

Geoqueries (iOS)

Database-level encryption via SQLCipher (iOS)

Couchbase Lite Overview

19

Noteworthy on the horizon ...

Experimental branch w/ ForestDB backend (iOS)

Page 20: Introduction to Couchbase Mobile: Couchbase Connect 2014

Couchbase Lite Deep Dive

CRUD

Page 21: Introduction to Couchbase Mobile: Couchbase Connect 2014

©2014 Couchbase, Inc.

Modeling data in JSON is different than SQL

You can have arbitrarily nested structure in a document

Contact addresses could be included as an array

You can gain some “atomicity” between fields by putting them in the same document

There are no joins — if a doc contains an ID of another doc, a separate lookup would be needed

Couchbase Lite Deep Dive

21

Page 22: Introduction to Couchbase Mobile: Couchbase Connect 2014

©2014 Couchbase, Inc.

JSON Document

22

Sample JSON document

{

"_id":"[email protected]",

"_rev":"1-238",

"type":"User",

"addresses":[

{

"address_type":"home",

"street":"Easy St"

},

{

"address_type":"work",

"street":"Mission St"

}

]

}

_id can be specified or auto-assigned a UUID. Must be unique in database.

_rev is special metadata used by the system to track revisions + conflicts

“type” is a common convention to specify “object type”

Page 23: Introduction to Couchbase Mobile: Couchbase Connect 2014

©2014 Couchbase, Inc.

CRUD - Creating a new document

You pass it an NSDictionary (iOS) or a Map (Java) that represents your JSON Document

The Map can contain Lists and other Maps

Everything must be serializable into primitive types

Couchbase Lite Deep Dive

23

Page 24: Introduction to Couchbase Mobile: Couchbase Connect 2014

©2014 Couchbase, Inc.

CRUD - Understanding Attachments

A document can have any number of attachments

Typical example: Profile document with image of user attached

Efficient sync behavior: attachment data only transferred when the attachment has changed

Common question: sync priority? Ans: not yet

Couchbase Lite Deep Dive

24

Page 25: Introduction to Couchbase Mobile: Couchbase Connect 2014

©2014 Couchbase, Inc.

CRUD - Editing Documents

Your edit attempt may fail!

Pass the entire new Map object, with the previous _rev ID field

It does a “Compare and Set”, and if the previous _rev ID field is different than what you passed, the operation will fail

How can this happen? New revision of doc is pulled while user is editing a UI form.

Couchbase Lite Deep Dive

25

Page 26: Introduction to Couchbase Mobile: Couchbase Connect 2014

©2014 Couchbase, Inc.

CRUD - Deletions are new special Revision

A deletion adds a new “Tombstone” revision

This revision is sync’d like any other revision

If a doc has a tombstone revision, Couchbase Lite pretends its not there.

If needed, you can actually retain any information you like in the Tombstone revision

Clean up happens by a process called Compaction

Couchbase Lite Deep Dive

26

Page 27: Introduction to Couchbase Mobile: Couchbase Connect 2014

©2014 Couchbase, Inc.

CRUD - A few “fringe” features to be aware of

Document purging allows you to delete all traces of a document locally, without affecting any other clients

Local Documents allow you to create a document that is never sync’d

Couchbase Lite Deep Dive

27

Page 28: Introduction to Couchbase Mobile: Couchbase Connect 2014

©2014 Couchbase, Inc.

Revisions and Conflicts

28

Imagine a document editable by Alice and Bob

Alice creates Rev 3-21ab of document while offline

Bob creates Rev 3-42cc of document while offline

Alice and Bob both go online and devices sync

On both devices, Couchbase Lite deterministically chooses Rev 3-21ab as the winner and deletes Rev 3-42cc

As of Rev 4-45cb, the conflict is resolved

Rev

1-34ad

Rev

2-63fc

Rev

4-10ac

Rev

4-45cb

Rev

5-42bb

Tombstone

Rev

3-21ab

Rev

3-42ccConflict

Page 29: Introduction to Couchbase Mobile: Couchbase Connect 2014

©2014 Couchbase, Inc.

CRUD - A few more notes about conflicts

The system will “auto-resolve” conflicts in a deterministic manner

Non-deletions always chosen over deletions

Branches with more revisions win over others

All else equal, lexicographic compare of revision Id digest (the non-numeric part of the revision)

It’s not possible for your app to “intercept” this logic

However, your app can retroactively choose a different conflict resolution, possibly doing application specific merge

Couchbase Lite Deep Dive

29

Page 30: Introduction to Couchbase Mobile: Couchbase Connect 2014

Couchbase Lite Deep Dive

Views + Queries

Page 31: Introduction to Couchbase Mobile: Couchbase Connect 2014

©2014 Couchbase, Inc.

Views

Purpose: The primary way of “getting data out” of DB, aside from fetching single doc by ID

Harder to grasp than SQL

As the developer, you are responsible for building low level indexes

No joins — Some queries can be awkward

Typically, a TableView / ListAdapter will correspond to a single Couchbase Lite View

A map function is required for every view

An optional reduce function can be specified — a typical use case would be for “stats”

Typically written in native code (Java / ObjC), but there is an option to write in Javascript

Couchbase Lite Deep Dive

31

Page 32: Introduction to Couchbase Mobile: Couchbase Connect 2014

©2014 Couchbase, Inc.

Couchbase Lite Deep Dive

32

Suppose you have the following docs:

{ "id": "Bob", "age": 22 }

{ "id": "Mary", "age": 52 }

{ "id": "Joe", "age": 28 }

{ "id": "Bill", "age": 15 }

{ "id": "Alice", "age": 20 }

And want to show usernames of all

users between 20 and 30, sorted

ascending by age:

Name

Alice (20)

Bob (22)

Joe (28)

Page 33: Introduction to Couchbase Mobile: Couchbase Connect 2014

©2014 Couchbase, Inc.

Couchbase Lite Deep Dive

33

Map Function Generated View Index

map(doc) {

# eg, "Alice (20)"

value = "%s (%d)" % (doc.id, doc.age)

# we use the age for the key

key = doc.age

# emit key and value into view index

emit(key, value)

}

Key Value (DocID)

15 Bill (15) Bill

20 Alice (20) Alice

22 Bob (22) Bob

28 Joe (28) Joe

52 Mary (52) Mary

Page 34: Introduction to Couchbase Mobile: Couchbase Connect 2014

Query Generated View Index

©2014 Couchbase, Inc.

Couchbase Lite Deep Dive

34

query.startKey = 20

query.endKey = 30

Only these

three rows fall

in key range of

query

X

X

Key Value (DocID)

15 Bill (15) Bill

20 Alice (20) Alice

22 Bob (22) Bob

28 Joe (28) Joe

52 Mary (52) Mary

Page 35: Introduction to Couchbase Mobile: Couchbase Connect 2014

©2014 Couchbase, Inc.

Final notes on Views and Queries

Can emit more than one row into index for single document

Keys can be arrays rather than single value

Queries support “grouping”

A LiveQuery runs persistently and streams new results

A view index is updated lazily when queried, but running a LiveQuery on it will keep it constantly up to date

The map function must always produce same output given same input —cannot depend on current time for example

Couchbase Lite Deep Dive

35

Page 36: Introduction to Couchbase Mobile: Couchbase Connect 2014

Couchbase Lite Deep Dive

Replications

Page 37: Introduction to Couchbase Mobile: Couchbase Connect 2014

©2014 Couchbase, Inc.

Couchbase Lite Deep Dive

37

Push and Pull are independent

©2014 Couchbase, Inc.

Sync Gateway

Couchbase

Lite

(Device A)

Push

Couchbase

Lite

(Device B)

Pull Pull

Page 38: Introduction to Couchbase Mobile: Couchbase Connect 2014

©2014 Couchbase, Inc.

Couchbase Lite Deep Dive

38

One Shot vs. Continuous

©2014 Couchbase, Inc.

Started

Running

Stopped

One Shot

Started

Running Idle

Continuous

Page 39: Introduction to Couchbase Mobile: Couchbase Connect 2014

©2014 Couchbase, Inc.

Couchbase Lite Deep Dive

39

Callback API for app to be notified of change events

Continuous replications use HTTP longpoll (Java/.NET) or Websockets to open socket to Sync Gateway Changes Feed

A “slice” of the database can be replicated by using Channels

On Android, a background replication will continue to run as long as not in Deep Sleep

On iOS, it’s possible to use push notifications to trigger replication, or have scheduled background trigger to run periodically

Page 40: Introduction to Couchbase Mobile: Couchbase Connect 2014

Couchbase Lite Resources

Page 41: Introduction to Couchbase Mobile: Couchbase Connect 2014

©2014 Couchbase, Inc.

Couchbase Lite Resources

41

Downloads: http://mobile.couchbase.com

Documentation: http://developer.couchbase.com

Twitter: @CouchbaseMobile

Google Group (active!): http://groups.google.com/group/mobile-couchbase/

Github Repos:

https://github.com/couchbase/couchbase-lite-ios

https://github.com/couchbase/couchbase-lite-android

https://github.com/couchbase/couchbase-lite-net

https://github.com/couchbaselabs/ToDoLite-Android

https://github.com/couchbaselabs/ToDoLite-iOS

Page 42: Introduction to Couchbase Mobile: Couchbase Connect 2014

Thanks for watching! Questions?