SlideShare a Scribd company logo
Michael Parker
February 24, 2013
What is CouchDB?
● One of those hipster NoSQL databases
● Document-oriented, not relational
○ No joins, prefer denormalization

● RESTful API for all operations
● Views add structure via secondary indexes
● Other perks:
○
○
○
○

Multiversion concurrency control (lock-free)
MapReduce framework
Great web admin interface
Multi-master replication
Document-oriented
● Like a key-value store, flat namespace
○ In CouchDB, key is called a document identifier, or
doc id

● Documents stored in JSON format
○ "Schemaless," but the schema resides in your app
○ JSON also format for HTTP body

● Similar: MongoDB (no relation), Redis,
Cassandra
JSON Document in
CouchDB
{
"_id": "emp_001",
"_rev": "1-4c6114c65e295552ab1019e2b046b10e",
"name": {
"first": "Dante",
"last": "Hicks",
},
"phone": "310-555-1212",
"interests": ["philosophy", "Star Wars"]
}
RESTful URLs
● All resources in database identified by URL
● Basic URL structure:
○ /db_id: A database
○ /db_id/doc_id: A document in a database

● URL components and JSON fields starting
with an underscore are special, e.g.:
○ /_config: CouchDB configuration parameters
○ /db_id/_all_docs: A cursor across all
documents
○ /db_id/_design/design_doc: A design
document for a database
RESTful Methods
● HTTP methods for CRUD actions
○ GET: Read data, typically a document
○ HEAD: Like GET without a body, typically used to
check if a document exists
○ PUT: Creates new databases, documents, and other
resources
○ POST: Updates these resources
○ DELETE: Deletes these resources
RESTful Status Codes
● HTTP status codes for server responses
○ 200 OK: Request completed successfully (e.g.
retrieving, updating, deleting documents)
○ 201 Created: Resource created (used with PUT)
○ 202 Accepted: Request completed and operation
pending (e.g. for background operations)
○ 401 Unauthorized: Bad username or password
○ 404 Not Found: Resource missing
○ 409 Conflict: MVCC failure, or concurrent
modification to a document
○ 500 Internal Server Error: Everybody panic
Creating a Database
● Request (abbr.):
PUT /new_db/ HTTP/1.1
● Response (abbr.):
HTTP/1.1 201 Created
{"ok": true}
Creating a Database
● Request (abbr.):
GET /_all_dbs HTTP/1.1
● Response (abbr.):
HTTP/1.1 200 OK
["new_db"]
Creating a Document
● Request (abbr.):
PUT /my_db/emp_001 HTTP/1.1
{
"name": {
"first": "Dante", "last": "Hicks",
},
"phone": "310-555-1212",
"interests": ["philosophy", "Star Wars"]
}
Creating a Document
● Response (abbr.):
HTTP/1.1 201 Created
{
"ok": true,
"id": "emp_001",
"rev": "1-4c6114c65e295552ab1019e2b046b10e"
}
Retrieving a Document
● Request (abbr.):
GET /my_db/emp_001 HTTP/1.1
Retrieving a Document
● Response (abbr.):
HTTP/1.1 200 OK
{
"_id": "emp_001",
"_rev": "1-4c6114c65e295552ab1019e2b046b10e",
"name": {
"first": "Dante", "last": "Hicks",
},
"phone": "310-555-1212",
"interests": ["philosophy", "Star Wars"]
}
Retrieving a Document
● Request (abbr.):
GET /my_db/missing_emp_007 HTTP/1.1

● Response (abbr.):
HTTP/1.1 404 Object Not Found
{
"error": "not_found",
"reason": "missing"
}
Views
● Secondary indexes for querying by other
than _id
● Written in JavaScript, executed with Mozilla
SpiderMonkey engine
● Defined in the design document
Views
● Define view emps_by_interest:
function(emp_doc) {
for (var i = 0; i < emp_doc.interests.length; ++i) {
var interest = emp_doc.interests[i];
emit(interest.toLowerCase(), null);
}
}

● Request (abbr):
GET
/my_db/_design/my_dd/_view/emps_by_interest?key=philosophy
HTTP/1.1
Views
● Response (abbr):
HTTP/1.1 200 OK
{
"total_rows": 1, "offset": 0,
"rows": [
{"id": "emp_001", "key": "philosophy", "value": null}
]
}

● Append &include_docs=true in request to
return documents with results
Multiversion Concurrency
Control (MVCC)
● Every document has a _rev attribute
○ Only required field other than _id

● Ensures that client is updating latest data
○ No accidental clobbering

● Lock-free concurrency control
● If multiple clients attempt to write
concurrently, exactly one succeeds every
time
○ Always making "forward progress"
Multiversion Concurrency
Control (MVCC)
CLIENT 1

CLIENT 2
GET id=doc_id

GET id=doc_id

_rev=X, v=1

_rev=X, v=1
_rev=X, v=2

_rev=Y, v=2
HTTP 200, _rev=Y

_rev=X, v=2
HTTP 409
GET id=doc_id
_rev=Y, v=2
_rev=Y, v=3

_rev=Z, v=3
t

HTTP 200, _rev=Z
Benchmarking
https://github.com/mgp/iron-cushion

● Setup:
○
○
○
○
○

Server: Intel Core 2 2.83GHz quad-core, 4GB RAM
Client: 1.83 GHz Intel Core Duo MacBook
100Mbit LAN, 100 concurrent connections
first, bulk insert 2,000,000 documents
second, intersperse 20,000 create and read
operations, 30,000 update and delete operations

● Caveat: no indexes
Benchmarking
bulkInsertRate: 10,003.030 docs/sec
createProcessingRate: 949.141
readProcessingRate: 9,015.862
updateProcessingRate: 980.172
deleteProcessingRate: 980.154

docs/sec
docs/sec
docs/sec
docs/sec
Thanks!
http://couchdb.apache.org/
michael.g.parker@gmail.com
https://github.com/mgp
http://mgp.github.com/couchdb-la-hn.pdf

More Related Content

KEY
Introduction to MongoDB
KEY
PDF
Mongo Presentation by Metatagg Solutions
PPTX
Back to Basics Webinar 2: Your First MongoDB Application
PDF
Mongo db basics
PPT
PhpstudyTokyo MongoDB PHP CakePHP
PPT
Introduction to MongoDB
PPTX
Back to Basics Webinar 1: Introduction to NoSQL
Introduction to MongoDB
Mongo Presentation by Metatagg Solutions
Back to Basics Webinar 2: Your First MongoDB Application
Mongo db basics
PhpstudyTokyo MongoDB PHP CakePHP
Introduction to MongoDB
Back to Basics Webinar 1: Introduction to NoSQL

What's hot (20)

PDF
Superficial mongo db
ODP
MongoDB : The Definitive Guide
PPTX
Mongo db
PPTX
MongoDB for Beginners
PPTX
MongoDB basics & Introduction
PPTX
Back to Basics Webinar 3: Schema Design Thinking in Documents
ODP
MongoDB - Ekino PHP
KEY
MongoDB at GUL
PPTX
Back to Basics: My First MongoDB Application
PPTX
Intro to mongodb mongouk jun2010
PPTX
Mongo db queries
PPTX
MongoDB 101
PPTX
Webinaire 2 de la série « Retour aux fondamentaux » : Votre première applicat...
PPTX
MongoDB
PPTX
Mongo DB: Fundamentals & Basics/ An Overview of MongoDB/ Mongo DB tutorials
PPTX
Webinar: Building Your First MongoDB App
ODP
PPTX
Mongo db1
PDF
Building your first app with mongo db
Superficial mongo db
MongoDB : The Definitive Guide
Mongo db
MongoDB for Beginners
MongoDB basics & Introduction
Back to Basics Webinar 3: Schema Design Thinking in Documents
MongoDB - Ekino PHP
MongoDB at GUL
Back to Basics: My First MongoDB Application
Intro to mongodb mongouk jun2010
Mongo db queries
MongoDB 101
Webinaire 2 de la série « Retour aux fondamentaux » : Votre première applicat...
MongoDB
Mongo DB: Fundamentals & Basics/ An Overview of MongoDB/ Mongo DB tutorials
Webinar: Building Your First MongoDB App
Mongo db1
Building your first app with mongo db

Similar to Introduction to CouchDB - LA Hacker News (20)

PDF
Couch db
PPTX
Cluster of unreliable commodity hardware (couchdb) (2)
PPTX
Couch DB
PDF
CouchDB
PPT
Couch db
PPTX
CouchDB
PDF
Python-CouchDB Training at PyCon PL 2012
PPT
No sql Database
KEY
Couchdb: No SQL? No driver? No problem
KEY
CouchDB : More Couch
PDF
Couchbase - Yet Another Introduction
PDF
CouchDB in The Room
PDF
CouchDB at its Core: Global Data Storage and Rich Incremental Indexing at Clo...
PDF
Apache CouchDB talk at Ontario GNU Linux Fest
ODP
Introducing CouchDB
PDF
NoSQL and CouchDB: the view from MOO
PPT
NoSQL - "simple" web monitoring
PDF
No sq lv1_0
PPTX
Couchbase Data Platform | Big Data Demystified
Couch db
Cluster of unreliable commodity hardware (couchdb) (2)
Couch DB
CouchDB
Couch db
CouchDB
Python-CouchDB Training at PyCon PL 2012
No sql Database
Couchdb: No SQL? No driver? No problem
CouchDB : More Couch
Couchbase - Yet Another Introduction
CouchDB in The Room
CouchDB at its Core: Global Data Storage and Rich Incremental Indexing at Clo...
Apache CouchDB talk at Ontario GNU Linux Fest
Introducing CouchDB
NoSQL and CouchDB: the view from MOO
NoSQL - "simple" web monitoring
No sq lv1_0
Couchbase Data Platform | Big Data Demystified

Recently uploaded (20)

PDF
Approach and Philosophy of On baking technology
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Encapsulation theory and applications.pdf
PDF
Electronic commerce courselecture one. Pdf
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
Cloud computing and distributed systems.
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
MYSQL Presentation for SQL database connectivity
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
Approach and Philosophy of On baking technology
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Encapsulation theory and applications.pdf
Electronic commerce courselecture one. Pdf
Network Security Unit 5.pdf for BCA BBA.
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Reach Out and Touch Someone: Haptics and Empathic Computing
Understanding_Digital_Forensics_Presentation.pptx
Unlocking AI with Model Context Protocol (MCP)
Cloud computing and distributed systems.
Encapsulation_ Review paper, used for researhc scholars
MYSQL Presentation for SQL database connectivity
NewMind AI Weekly Chronicles - August'25 Week I
Chapter 3 Spatial Domain Image Processing.pdf
Digital-Transformation-Roadmap-for-Companies.pptx
Mobile App Security Testing_ A Comprehensive Guide.pdf
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
20250228 LYD VKU AI Blended-Learning.pptx

Introduction to CouchDB - LA Hacker News

  • 2. What is CouchDB? ● One of those hipster NoSQL databases ● Document-oriented, not relational ○ No joins, prefer denormalization ● RESTful API for all operations ● Views add structure via secondary indexes ● Other perks: ○ ○ ○ ○ Multiversion concurrency control (lock-free) MapReduce framework Great web admin interface Multi-master replication
  • 3. Document-oriented ● Like a key-value store, flat namespace ○ In CouchDB, key is called a document identifier, or doc id ● Documents stored in JSON format ○ "Schemaless," but the schema resides in your app ○ JSON also format for HTTP body ● Similar: MongoDB (no relation), Redis, Cassandra
  • 4. JSON Document in CouchDB { "_id": "emp_001", "_rev": "1-4c6114c65e295552ab1019e2b046b10e", "name": { "first": "Dante", "last": "Hicks", }, "phone": "310-555-1212", "interests": ["philosophy", "Star Wars"] }
  • 5. RESTful URLs ● All resources in database identified by URL ● Basic URL structure: ○ /db_id: A database ○ /db_id/doc_id: A document in a database ● URL components and JSON fields starting with an underscore are special, e.g.: ○ /_config: CouchDB configuration parameters ○ /db_id/_all_docs: A cursor across all documents ○ /db_id/_design/design_doc: A design document for a database
  • 6. RESTful Methods ● HTTP methods for CRUD actions ○ GET: Read data, typically a document ○ HEAD: Like GET without a body, typically used to check if a document exists ○ PUT: Creates new databases, documents, and other resources ○ POST: Updates these resources ○ DELETE: Deletes these resources
  • 7. RESTful Status Codes ● HTTP status codes for server responses ○ 200 OK: Request completed successfully (e.g. retrieving, updating, deleting documents) ○ 201 Created: Resource created (used with PUT) ○ 202 Accepted: Request completed and operation pending (e.g. for background operations) ○ 401 Unauthorized: Bad username or password ○ 404 Not Found: Resource missing ○ 409 Conflict: MVCC failure, or concurrent modification to a document ○ 500 Internal Server Error: Everybody panic
  • 8. Creating a Database ● Request (abbr.): PUT /new_db/ HTTP/1.1 ● Response (abbr.): HTTP/1.1 201 Created {"ok": true}
  • 9. Creating a Database ● Request (abbr.): GET /_all_dbs HTTP/1.1 ● Response (abbr.): HTTP/1.1 200 OK ["new_db"]
  • 10. Creating a Document ● Request (abbr.): PUT /my_db/emp_001 HTTP/1.1 { "name": { "first": "Dante", "last": "Hicks", }, "phone": "310-555-1212", "interests": ["philosophy", "Star Wars"] }
  • 11. Creating a Document ● Response (abbr.): HTTP/1.1 201 Created { "ok": true, "id": "emp_001", "rev": "1-4c6114c65e295552ab1019e2b046b10e" }
  • 12. Retrieving a Document ● Request (abbr.): GET /my_db/emp_001 HTTP/1.1
  • 13. Retrieving a Document ● Response (abbr.): HTTP/1.1 200 OK { "_id": "emp_001", "_rev": "1-4c6114c65e295552ab1019e2b046b10e", "name": { "first": "Dante", "last": "Hicks", }, "phone": "310-555-1212", "interests": ["philosophy", "Star Wars"] }
  • 14. Retrieving a Document ● Request (abbr.): GET /my_db/missing_emp_007 HTTP/1.1 ● Response (abbr.): HTTP/1.1 404 Object Not Found { "error": "not_found", "reason": "missing" }
  • 15. Views ● Secondary indexes for querying by other than _id ● Written in JavaScript, executed with Mozilla SpiderMonkey engine ● Defined in the design document
  • 16. Views ● Define view emps_by_interest: function(emp_doc) { for (var i = 0; i < emp_doc.interests.length; ++i) { var interest = emp_doc.interests[i]; emit(interest.toLowerCase(), null); } } ● Request (abbr): GET /my_db/_design/my_dd/_view/emps_by_interest?key=philosophy HTTP/1.1
  • 17. Views ● Response (abbr): HTTP/1.1 200 OK { "total_rows": 1, "offset": 0, "rows": [ {"id": "emp_001", "key": "philosophy", "value": null} ] } ● Append &include_docs=true in request to return documents with results
  • 18. Multiversion Concurrency Control (MVCC) ● Every document has a _rev attribute ○ Only required field other than _id ● Ensures that client is updating latest data ○ No accidental clobbering ● Lock-free concurrency control ● If multiple clients attempt to write concurrently, exactly one succeeds every time ○ Always making "forward progress"
  • 19. Multiversion Concurrency Control (MVCC) CLIENT 1 CLIENT 2 GET id=doc_id GET id=doc_id _rev=X, v=1 _rev=X, v=1 _rev=X, v=2 _rev=Y, v=2 HTTP 200, _rev=Y _rev=X, v=2 HTTP 409 GET id=doc_id _rev=Y, v=2 _rev=Y, v=3 _rev=Z, v=3 t HTTP 200, _rev=Z
  • 20. Benchmarking https://github.com/mgp/iron-cushion ● Setup: ○ ○ ○ ○ ○ Server: Intel Core 2 2.83GHz quad-core, 4GB RAM Client: 1.83 GHz Intel Core Duo MacBook 100Mbit LAN, 100 concurrent connections first, bulk insert 2,000,000 documents second, intersperse 20,000 create and read operations, 30,000 update and delete operations ● Caveat: no indexes
  • 21. Benchmarking bulkInsertRate: 10,003.030 docs/sec createProcessingRate: 949.141 readProcessingRate: 9,015.862 updateProcessingRate: 980.172 deleteProcessingRate: 980.154 docs/sec docs/sec docs/sec docs/sec