Share
Embed
Study about PouchDB
A brief introduction of PouchDB
(Free study in Golden Week holiday)
Kelvin Ko
May 11, 2021
Transcript
20 slides
-
About me • Kelvin @ko_kelvin • LINE Client Team (LFK
Shop part) • Android Developer -
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 -
Current Tech Stack • Frontend: • React - Redux -
Typescript • Backend: • Firebase • Google Auth • Realtime Database For real time sync across devices -
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) -
Looking for alternative To replace Firebase Realtime Database • Sync
data across clients in real time • Support Offline access • Open Source -
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 -
PouchDB What it is? • In-browser database • NoSQL Database
• Document Database to store JSON object (similar to Firebase DB) • CouchDB compatiable • designed for Sync -
How it work? Sync • Init local DB • Init
Remote DB • Sync Data between databases • How it can be so simple !?😮 How it handle conflict ? 🤔 -
How it work? Storing a document • document can be
any JSON object with field “_id” -
How it work? Retrieving a document • get with “id”
• a new field “_rev” (revision) is added to your document -
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 -
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 -
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? -
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 -
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 -
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 -
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 -
That’s all for my study in GW Thank you for
listening (If you know other Firebase database alternative, do let me know 😃 )