Slide 1

Slide 1 text

Kelvin Ko Study about PouchDB 2021 GWࣗ༝ݚڀൃදձ

Slide 2

Slide 2 text

About me • Kelvin @ko_kelvin • LINE Client Team (LFK Shop part) • Android Developer

Slide 3

Slide 3 text

Why study about PouchDB? Motivation • Working on a free project in our ΋͘΋͘ձ #z_lfk_mokumoku • Building a Developer friendly Note-taking Web App https://www.stashany.com

Slide 4

Slide 4 text

Current Tech Stack • Frontend: • React - Redux - Typescript • Backend: • Firebase • Google Auth • Realtime Database For real time sync across devices

Slide 5

Slide 5 text

But… Firebase real time database limitation • No offline support in Web (actually it support offline usage, but limited functions) • No search function (Require third party indexing service) • No self-host solution (some users concern privacy)

Slide 6

Slide 6 text

Looking for alternative To replace Firebase Realtime Database • Sync data across clients in real time • Support Offline access • Open Source

Slide 7

Slide 7 text

What kind of database match the requirement? Characteristics • AP Database (Availability, Partition-Tolerance) • In Note App use case: • Always can write and read the notes any time even it is offline • Notes across clients (e.g. Desktop, mobile) will be in-sync (eventually consistent) when get back online CAP Theorem

Slide 8

Slide 8 text

PouchDB What it is? • In-browser database • NoSQL Database • Document Database to store JSON object (similar to Firebase DB) • CouchDB compatiable • designed for Sync

Slide 9

Slide 9 text

How it work? Sync • Init local DB • Init Remote DB • Sync Data between databases • How it can be so simple !?😮 How it handle conflict ? 🤔

Slide 10

Slide 10 text

How it work? Storing a document • document can be any JSON object with field “_id”

Slide 11

Slide 11 text

How it work? Retrieving a document • get with “id” • a new field “_rev” (revision) is added to your document

Slide 12

Slide 12 text

How it work? Updating document • Required to include “_rev” (revision) in the document to update • the “_rev” in update document should match the one in database, otherwise will raise conflict • For success update, new “_rev” will be assigned Fail: if provide _rev different from current db one Success: a new _rev will be assigned to document

Slide 13

Slide 13 text

Why need revision “_rev”? Make Sync work! • Git-like Database • Every update of a “document” is like a commit • “_rev” is like the commit hash

Slide 14

Slide 14 text

What if conflict occur? In case both clients make change offline • client 1 updated document, rev become “rev 3-a” • client 2 updated document, rev become “rev 3-b” • When both clients get back online and “sync” Offline client 1 Offline client 2 What will happen?

Slide 15

Slide 15 text

What if conflict occur? • CouchDB will choose an arbitrary winner based on a deterministic algorithm* • Both clients will see the same winner once they're back online 1. The revision with the longest revision history list becomes the winning revision. 2. If they are the same, the _rev values are compared in ASCII sort order, * Usually, we don’t need to care about the algorithm, just for your interest about the implementation

Slide 16

Slide 16 text

Handling conflict Example • Get with option: {conflicts: true} to detect any conflict in this document • Get the conflict version with a specific “_rev”: Return Winner Loss version

Slide 17

Slide 17 text

Handling conflict • In application layer • You can decide just use the winner version • Or handle the conflict: in note app case, • Display both version of notes to user and let user decide which to keep Note1-rev-A.md Note1-rev-B.md

Slide 18

Slide 18 text

Some interesting possibilities (If still have time)

Slide 19

Slide 19 text

Some interesting features Plugins • Pouchdb-adapter-memory: In-memory version of PouchDB • Replicates with a local PouchDB, acting as a cache • Peer Pouch: Allow PouchDB instances transfer data over WebRTC • Databases in different clients can sync with each other without Server • PouchDB Quick Search: Full text search engine for PouchDB

Slide 20

Slide 20 text

That’s all for my study in GW Thank you for listening (If you know other Firebase database alternative, do let me know 😃 )