$30 off During Our Annual Pro Sale. View Details »

Study about PouchDB

Study about PouchDB

A brief introduction of PouchDB
(Free study in Golden Week holiday)

Kelvin Ko

May 11, 2021

More Decks by Kelvin Ko

Other Decks in Programming


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

    View Slide

  2. About me
    • Kelvin @ko_kelvin

    • LINE Client Team (LFK Shop part)

    • Android Developer

    View Slide

  3. Why study about PouchDB?
    • Working on a free project in our ΋͘΋͘ձ #z_lfk_mokumoku

    • Building a Developer friendly Note-taking Web App

    View Slide

  4. Current Tech Stack
    • Frontend:

    • React - Redux - Typescript

    • Backend:

    • Firebase

    • Google Auth

    • Realtime Database
    For real time sync across devices

    View Slide

  5. 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)

    View Slide

  6. Looking for alternative
    To replace Firebase Realtime Database
    • Sync data across clients in real time

    • Support Offline access

    • Open Source

    View Slide

  7. What kind of database match the requirement?
    • 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

    View Slide

  8. PouchDB
    What it is?
    • In-browser database

    • NoSQL Database

    • Document Database to store JSON object (similar to Firebase DB)

    • CouchDB compatiable

    • designed for Sync

    View Slide

  9. How it work?
    • Init local DB

    • Init Remote DB

    • Sync Data between databases

    How it can be so simple !?😮
    How it handle conflict ? 🤔

    View Slide

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

    View Slide

  11. How it work?
    Retrieving a document
    • get with “id”

    • a new field
    “_rev” (revision) is added
    to your document

    View Slide

  12. 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

    View Slide

  13. 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

    View Slide

  14. 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
    Offline client 1 Offline client 2
    What will happen?

    View Slide

  15. What if conflict occur?
    • CouchDB will choose an arbitrary winner based on a deterministic
    • 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

    View Slide

  16. Handling conflict
    • Get with option: {conflicts: true} to detect any conflict in this document

    • Get the conflict version with a specific “_rev”:
    Loss version

    View Slide

  17. 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

    View Slide

  18. Some interesting possibilities
    (If still have time)

    View Slide

  19. Some interesting features
    • 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

    View Slide

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

    View Slide