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

An introduction to CouchDB

An introduction to CouchDB

anderspree

July 10, 2019
Tweet

More Decks by anderspree

Other Decks in Programming

Transcript

  1. !"

  2. !"

  3. 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
  4. RELAX? > fault-tolerant > build with anticipating failure > Couch

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

    > extreme emphasis on reliability and availability
  6. COUCHDB DOCUMENT { "_id": "00001", "_rev": "1-A", "name": "Bulbasaur", "types":

    ["grass", "poison"], "abilities": ["Overgrow","Chlorophyll"], "catch_rate": 45 }
  7. 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
  8. REDUCE Already Build in: '_sum', '_count', '_stats' { map: function

    (doc) { emit(doc.name.charAt(0)); }, reduce: '_count' }
  9. 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
  10. MAKE USE OF THE PRIMARY INDEX Example: Timeseries Data (Logs)

    > system has many devices > device sends log data ID Format: system:device:ISODate
  11. 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 })
  12. 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?
  13. REPLICATION { "_id": "00001", "_rev": "1-A", "name": "Bulbasaur", "types": ["grass",

    "poison"], "abilities": ["Overgrow","Chlorophyll"], "catch_rate": 45 }
  14. 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)
  15. 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
  16. COUCHDB 2.0 ( ! ! ) > (Amazon) Dynamo Paper

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

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

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

    & Pouch Tutorial > Learn Mango Queries