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

An introduction to CouchDB

An introduction to CouchDB

8c9b2e646f684c03394c5522717c371b?s=128

anderspree

July 10, 2019
Tweet

Transcript

  1. !"

  2. ! I'M JULIA

  3. 2015

  4. WEB DEVELOPER @ NEIGHBOURHOODIE SOFTWARE

  5. !"

  6. ON-CALL HORROR STORIES (OF MY FRIENDS) > Database Server going

    offline (panic!) > Failed attempt in scaling up causes corrupted & lost data I realised that CouchDB has some distinct features that I just took for granted
  7. Apache CouchDB has started. Time to relax.

  8. !" RELAX

  9. RELAX? > ease of use JSON & HTTP ! works

    with all* Languages
  10. RELAX? > fault-tolerant > build with anticipating failure > Couch

    = cluster of unreliable commodity hardware Build with Erlang
  11. ERLANG robust & concurrent > developed for real-time telecom applications

    > extreme emphasis on reliability and availability
  12. DOCUMENT DATABASE

  13. DOCUMENT DATABASE ! > JSON > unique IDs > schema-free

    > attachments
  14. COUCHDB DOCUMENT { "_id": "00001", "_rev": "1-A", "name": "Bulbasaur", "types":

    ["grass", "poison"], "abilities": ["Overgrow","Chlorophyll"], "catch_rate": 45 }
  15. QUERYING

  16. QUERYING > Map/Reduce > Mango > Primary Index > changes

  17. MAP/REDUCE > runs func on every doc in the db

    (once!) this creates a view index (b-tree) > next time of query -> only runs on docs that have changed/are new
  18. MAP > Creates index function myMapFunction(doc) { emit(doc.name); } >

    Query The index
  19. REDUCE Already Build in: '_sum', '_count', '_stats' { map: function

    (doc) { emit(doc.name.charAt(0)); }, reduce: '_count' }
  20. MANGO a DSL inspired by MongoDB > faster-performing & easier

    to use > ! since CouchDB 2.0.0
  21. MANGO { selector: { name: { $eq: 'Pikachu' } }

    }
  22. PRIMARY INDEX curl -X GET 127.0.0.1:5984/pokemon/00025 > Index by ID

    > Already there > super fast ! create your own IDs in a clever format
  23. MAKE USE OF THE PRIMARY INDEX Example: Timeseries Data (Logs)

    > system has many devices > device sends log data ID Format: system:device:ISODate
  24. MAKE USE OF THE PRIMARY INDEX Example: Timeseries Data (Logs)

    // get all logs of Device 1 of System 23 from January 2017, latest first await db.allDocs({ include_docs: true, startkey: `system23:device1:2017-01-`, endkey: `system23:device1:2017-01-\uffff`, descending: true })
  25. REAL-TIME CHANGE NOTIFICATIONS > index by sequence /db/_changes > What

    changes happened in die database? > What change occurred to this document? > What changed since X?
  26. REPLICATION CouchDB was designed with sync in mind, and this

    is exactly what it excels at.
  27. REPLICATION { "_id": "00001", "_rev": "1-A", "name": "Bulbasaur", "types": ["grass",

    "poison"], "abilities": ["Overgrow","Chlorophyll"], "catch_rate": 45 }
  28. REPLICATION { "_id": "00001", "_rev": "1-A", "name": "Bulbasaur", "types": ["grass",

    "poison"], "abilities": ["Overgrow","Chlorophyll"], "catch_rate": 45 } CouchDB requires you to manage revisions (_rev) (tedious but pays off in the end)
  29. REPLICATION > MVCC (Multiversion concurrency control) > sequence index &

    revisions
  30. REPLICATION > bi-directional replication (synchronization) > multi-primary architecture

  31. REPLICATION ! ⬅ ☁☁☁ ↖ ☁☁☁ ↘ ☁☁ *

  32. REPLICATION ! ⬅ ☁☁☁ ↖ ☁☁☁ ↘ ☁☁ * ↕

    !
  33. REPLICATION ! ⬅ ☁☁☁ ↖ ☁☁☁ ↘ ☁☁ * !

    !
  34. REPLICATION CONFLICTS > an arbitrary winner Revision is chosen by

    CouchDB > conflicting Revisions are also stored > no Data is lost > up to the application developer to decide what to do
  35. COUCHDB 2.0 ( ! ! ) > (Amazon) Dynamo Paper

    > same API as Single Instance > ready for big data workloads
  36. NEIGHBOURHOODIE SOFTWARE > CouchDB Consulting / Support / Workshops >

    ! Opservatory - CouchDB Monitoring > Greenkeeper - Javascript Dependency updates > (Offline-First) App Development Consulting
  37. !

  38. LINKS > CouchDB Docs > Offline-First Web App with Couch

    & Pouch Tutorial > Learn Mango Queries