distributed couchapps - embracing eventual consistency ... · distributed couchapps - embracing...

108
Distributed CouchApps - Embracing eventual consistency Confoo Kore Nordmann March 9, 2011

Upload: others

Post on 14-Aug-2020

17 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventualconsistency

Confoo

Kore Nordmann

March 9, 2011

Page 2: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 2 / 49

About me

I Kore Nordmann (<[email protected]>, <[email protected]>,<[email protected]>)

I Twitter: @koredn

I More than 10 years of professional PHP

I Open source enthusiast

I Contributing to various FLOSS projectsI Founder of Qafoo GmbH

I Provides training & consulting on PHP software quality tools& processes

Page 3: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 3 / 49

Outline

Overview

CouchDB

Rewrites & VHosts

Replication & Eventual Consistency

Conclusion

Page 4: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 4 / 49

CouchDB is paradigmn shift

I Structure

I Consistency

I API

I Applications

Page 5: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 4 / 49

CouchDB is paradigmn shift

I Structure

I Consistency

I API

I Applications

Page 6: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 4 / 49

CouchDB is paradigmn shift

I Structure

I Consistency

I API

I Applications

Page 7: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 4 / 49

CouchDB is paradigmn shift

I Structure

I Consistency

I API

I Applications

Page 8: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 4 / 49

CouchDB is paradigmn shift

I Structure

I Consistency

I API

I Applications

Page 9: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 5 / 49

CouchDB

I Apache top-level project

Page 10: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 5 / 49

CouchDB

I Apache top-level project

Page 11: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 6 / 49

CouchDB

I Erlang/OTP virtualmachine, developed byEricsson

I Highly concurrent

I Scales nearly linearly withthe amount of CPUs

I High reliability (ninenines)

I CouchDB is fast (enough)

Erlang HTTPd

Couch

ViewEngine

StorageEngine

Repli-cator

Disk

Spider-Monkey

LuceneSearch

HTTPErlang

Page 12: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 6 / 49

CouchDB

I Erlang/OTP virtualmachine, developed byEricsson

I Highly concurrent

I Scales nearly linearly withthe amount of CPUs

I High reliability (ninenines)

I CouchDB is fast (enough)

Erlang HTTPd

Couch

ViewEngine

StorageEngine

Repli-cator

Disk

Spider-Monkey

LuceneSearch

HTTPErlang

Page 13: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 6 / 49

CouchDB

I Erlang/OTP virtualmachine, developed byEricsson

I Highly concurrent

I Scales nearly linearly withthe amount of CPUs

I High reliability (ninenines)

I CouchDB is fast (enough)

Erlang HTTPd

Couch

ViewEngine

StorageEngine

Repli-cator

Disk

Spider-Monkey

LuceneSearch

HTTPErlang

Page 14: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 6 / 49

CouchDB

I Erlang/OTP virtualmachine, developed byEricsson

I Highly concurrent

I Scales nearly linearly withthe amount of CPUs

I High reliability (ninenines)

I CouchDB is fast (enough)

Erlang HTTPd

Couch

ViewEngine

StorageEngine

Repli-cator

Disk

Spider-Monkey

LuceneSearch

HTTPErlang

Page 15: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 6 / 49

CouchDB

I Erlang/OTP virtualmachine, developed byEricsson

I Highly concurrent

I Scales nearly linearly withthe amount of CPUs

I High reliability (ninenines)

I CouchDB is fast (enough)

Erlang HTTPd

Couch

ViewEngine

StorageEngine

Repli-cator

Disk

Spider-Monkey

LuceneSearch

HTTPErlang

Page 16: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 7 / 49

Install now!

I Availbale for:I Linux, MacOSI WindowsI Android

Page 17: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 7 / 49

Install now!

I Availbale for:I Linux, MacOSI WindowsI Android

Page 18: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 7 / 49

Install now!

I Availbale for:I Linux, MacOSI WindowsI Android

Page 19: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 8 / 49

Outline

Overview

CouchDB

Rewrites & VHosts

Replication & Eventual Consistency

Conclusion

Page 20: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 9 / 49

CouchDB speaks HTTP

HTTP

I RESTful

I JSON

Page 21: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 9 / 49

CouchDB speaks HTTP

HTTP

I RESTful

I JSON

Page 22: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 9 / 49

CouchDB speaks HTTP

HTTP

I RESTful

I JSON

Page 23: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 10 / 49

Create a database

1 $ c u r l − i −X PUT ht tp : // l o c a l h o s t :5984/ confoo2 HTTP/1 .1 201 Created3 S e r v e r : CouchDB /1 . 0 . 1 ( E r l ang OTP/R13B)4 Loca t i on : h t tp : // l o c a l h o s t :5984/ confoo5 Content−Type : t e x t / p l a i n ; c h a r s e t=ut f−86 Content−Length : 1278 {”ok” : t rue}

Page 24: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 10 / 49

Create a database

1 $ c u r l − i −X PUT ht tp : // l o c a l h o s t :5984/ confoo2 HTTP/1 .1 201 Created3 S e r v e r : CouchDB /1 . 0 . 1 ( E r l ang OTP/R13B)4 Loca t i on : h t tp : // l o c a l h o s t :5984/ confoo5 Content−Type : t e x t / p l a i n ; c h a r s e t=ut f−86 Content−Length : 1278 {”ok” : t rue}

Page 25: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 10 / 49

Create a database

1 $ c u r l − i −X PUT ht tp : // l o c a l h o s t :5984/ confoo2 HTTP/1 .1 201 Created3 S e r v e r : CouchDB /1 . 0 . 1 ( E r l ang OTP/R13B)4 Loca t i on : h t tp : // l o c a l h o s t :5984/ confoo5 Content−Type : t e x t / p l a i n ; c h a r s e t=ut f−86 Content−Length : 1278 {”ok” : t rue}

Page 26: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 11 / 49

Re-Create a database

1 $ c u r l − i −X PUT ht tp : // l o c a l h o s t :5984/ confoo2 HTTP/1 .1 412 P r e c o nd i t i o n F a i l e d3 S e r v e r : CouchDB /1 . 0 . 1 ( E r l ang OTP/R13B)4 Content−Type : t e x t / p l a i n ; c h a r s e t=ut f−85 Content−Length : 9567 { ” e r r o r ” : ” f i l e e x i s t s ” ,8 ” r ea son ” : ”The database cou ld not be c r ea t ed , the f i l e a l r e a d y e x i s t s . ”9 }

Page 27: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 11 / 49

Re-Create a database

1 $ c u r l − i −X PUT ht tp : // l o c a l h o s t :5984/ confoo2 HTTP/1 .1 412 P r e c o nd i t i o n F a i l e d3 S e r v e r : CouchDB /1 . 0 . 1 ( E r l ang OTP/R13B)4 Content−Type : t e x t / p l a i n ; c h a r s e t=ut f−85 Content−Length : 9567 { ” e r r o r ” : ” f i l e e x i s t s ” ,8 ” r ea son ” : ”The database cou ld not be c r ea t ed , the f i l e a l r e a d y e x i s t s . ”9 }

Page 28: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 11 / 49

Re-Create a database

1 $ c u r l − i −X PUT ht tp : // l o c a l h o s t :5984/ confoo2 HTTP/1 .1 412 P r e c o nd i t i o n F a i l e d3 S e r v e r : CouchDB /1 . 0 . 1 ( E r l ang OTP/R13B)4 Content−Type : t e x t / p l a i n ; c h a r s e t=ut f−85 Content−Length : 9567 { ” e r r o r ” : ” f i l e e x i s t s ” ,8 ” r ea son ” : ”The database cou ld not be c r ea t ed , the f i l e a l r e a d y e x i s t s . ”9 }

Page 29: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 12 / 49

Get a database

1 $ c u r l − i −X GET ht tp : // l o c a l h o s t :5984/ confoo2 HTTP/1 .1 200 OK3 Se r v e r : CouchDB /1 . 0 . 1 ( E r l ang OTP/R13B)4 Content−Type : t e x t / p l a i n ; c h a r s e t=ut f−85 Content−Length : 21167 { ”db name” : ” confoo ” ,8 ” doc count ” : 0 ,9 ” d o c d e l c o un t ” : 0 ,

10 ” upda t e s eq ” : 0 ,11 ” pu rg e s eq ” : 0 ,12 ” compact runn ing ” : f a l s e ,13 ” d i s k s i z e ” : 79 ,14 ” i n s t a n c e s t a r t t i m e ” : ” 1299141142936296” ,15 ” d i s k f o rm a t v e r s i o n ” : 5 ,16 ” commi t t ed update seq ” : 017 }

Page 30: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 13 / 49

Delete a database

1 $ c u r l − i −X DELETE ht tp : // l o c a l h o s t :5984/ confoo2 HTTP/1 .1 200 OK3 Se r v e r : CouchDB /1 . 0 . 1 ( E r l ang OTP/R13B)4 Content−Type : t e x t / p l a i n ; c h a r s e t=ut f−85 Content−Length : 1267 {”ok” : t rue}

Page 31: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 13 / 49

Delete a database

1 $ c u r l − i −X DELETE ht tp : // l o c a l h o s t :5984/ confoo2 HTTP/1 .1 200 OK3 Se r v e r : CouchDB /1 . 0 . 1 ( E r l ang OTP/R13B)4 Content−Type : t e x t / p l a i n ; c h a r s e t=ut f−85 Content−Length : 1267 {”ok” : t rue}

Page 32: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 14 / 49

Get a non-existing database

1 $ c u r l − i −X GET ht tp : // l o c a l h o s t :5984/ confoo2 HTTP/1 .1 404 Object Not Found3 Se r v e r : CouchDB /1 . 0 . 1 ( E r l ang OTP/R13B)4 Content−Type : t e x t / p l a i n ; c h a r s e t=ut f−85 Content−Length : 4467 {” e r r o r ” : ” no t f ound ” , ” r ea son ” : ” n o d b f i l e ”}

Page 33: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 15 / 49

Outline

CouchDBDocuments

Page 34: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 16 / 49

Structure

I Example wiki document

1 { ” t i t l e ” : ”Confoo 2011” ,2 ” t e x t ” : ”Welcome to Confoo . . . ” ,3 ” c r e a t o r ” : ” use r−bar ” ,4 ” e d i t e d ” : 2935678239 ,5 ” r e v i s i o n s ” : [6 { ” t i t l e ” : ”PHPUK 2010” ,7 ” t e x t ” : ”Welcome to Confoo . . . ” ,8 ” c r e a t o r ” : ” use r−f oo ” ,9 ” e d i t e d ” : 2935678183 ,

10 ]11 } ,12 . . .13 ]14 }

Page 35: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 16 / 49

Structure

I Example wiki document

1 { ” t i t l e ” : ”Confoo 2011” ,2 ” t e x t ” : ”Welcome to Confoo . . . ” ,3 ” c r e a t o r ” : ” use r−bar ” ,4 ” e d i t e d ” : 2935678239 ,5 ” r e v i s i o n s ” : [6 { ” t i t l e ” : ”PHPUK 2010” ,7 ” t e x t ” : ”Welcome to Confoo . . . ” ,8 ” c r e a t o r ” : ” use r−f oo ” ,9 ” e d i t e d ” : 2935678183 ,

10 ]11 } ,12 . . .13 ]14 }

Page 36: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 16 / 49

Structure

I Example wiki document

1 { ” t i t l e ” : ”Confoo 2011” ,2 ” t e x t ” : ”Welcome to Confoo . . . ” ,3 ” c r e a t o r ” : ” use r−bar ” ,4 ” e d i t e d ” : 2935678239 ,5 ” r e v i s i o n s ” : [6 { ” t i t l e ” : ”PHPUK 2010” ,7 ” t e x t ” : ”Welcome to Confoo . . . ” ,8 ” c r e a t o r ” : ” use r−f oo ” ,9 ” e d i t e d ” : 2935678183 ,

10 ]11 } ,12 . . .13 ]14 }

Page 37: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 17 / 49

Attachements

I Example wiki document

1 { ” t i t l e ” : ”Confoo 2011” ,2 ” c r e a t o r ” : ” use r−bar ” ,3 ” t e x t ” : ”<h1>Welcome to the Confoo</h1>45 <img s r c=\”con foo 2011 / l ogo . png\” a l t=\”Confoo l ogo\”/>6 . . . ” ,7 ” a t t achment s ” : {8 ” l ogo . png” : {9 ” c on t e n t t y p e ” : ” image\/png” ,

10 ” s tub ” : t rue ,11 ” l e n g t h ” : 42 ,12 }13 }14 }

Page 38: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 17 / 49

Attachements

I Example wiki document

1 { ” t i t l e ” : ”Confoo 2011” ,2 ” c r e a t o r ” : ” use r−bar ” ,3 ” t e x t ” : ”<h1>Welcome to the Confoo</h1>45 <img s r c=\”con foo 2011 / l ogo . png\” a l t=\”Confoo l ogo\”/>6 . . . ” ,7 ” a t t achment s ” : {8 ” l ogo . png” : {9 ” c on t e n t t y p e ” : ” image\/png” ,

10 ” s tub ” : t rue ,11 ” l e n g t h ” : 42 ,12 }13 }14 }

Page 39: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 17 / 49

Attachements

I Example wiki document

1 { ” t i t l e ” : ”Confoo 2011” ,2 ” c r e a t o r ” : ” use r−bar ” ,3 ” t e x t ” : ”<h1>Welcome to the Confoo</h1>45 <img s r c=\”con foo 2011 / l ogo . png\” a l t=\”Confoo l ogo\”/>6 . . . ” ,7 ” a t t achment s ” : {8 ” l ogo . png” : {9 ” c on t e n t t y p e ” : ” image\/png” ,

10 ” s tub ” : t rue ,11 ” l e n g t h ” : 42 ,12 }13 }14 }

Page 40: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 18 / 49

Create a new document

1 $ c u r l − i −X PUT ht tp : // l o c a l h o s t :5984/ confoo / t e s t2 −d ’{” t i t l e ” : ” He l l o wor ld !”} ’3 HTTP/1 .1 201 Created4 S e r v e r : CouchDB /1 . 0 . 1 ( E r l ang OTP/R13B)5 Loca t i on : h t tp : // l o c a l h o s t :5984/ confoo / t e s t6 Etag : ”1−98da4da1fd7d7f90ceb752501d5b2321 ”7 Content−Type : t e x t / p l a i n ; c h a r s e t=ut f−88 Content−Length : 679

10 { ”ok” : t rue ,11 ” i d ” : ” t e s t ” ,12 ” r e v ” : ”1−98da4da1fd7d7f90ceb752501d5b2321 ”13 }

Page 41: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 18 / 49

Create a new document

1 $ c u r l − i −X PUT ht tp : // l o c a l h o s t :5984/ confoo / t e s t2 −d ’{” t i t l e ” : ” He l l o wor ld !”} ’3 HTTP/1 .1 201 Created4 S e r v e r : CouchDB /1 . 0 . 1 ( E r l ang OTP/R13B)5 Loca t i on : h t tp : // l o c a l h o s t :5984/ confoo / t e s t6 Etag : ”1−98da4da1fd7d7f90ceb752501d5b2321 ”7 Content−Type : t e x t / p l a i n ; c h a r s e t=ut f−88 Content−Length : 679

10 { ”ok” : t rue ,11 ” i d ” : ” t e s t ” ,12 ” r e v ” : ”1−98da4da1fd7d7f90ceb752501d5b2321 ”13 }

Page 42: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 18 / 49

Create a new document

1 $ c u r l − i −X PUT ht tp : // l o c a l h o s t :5984/ confoo / t e s t2 −d ’{” t i t l e ” : ” He l l o wor ld !”} ’3 HTTP/1 .1 201 Created4 S e r v e r : CouchDB /1 . 0 . 1 ( E r l ang OTP/R13B)5 Loca t i on : h t tp : // l o c a l h o s t :5984/ confoo / t e s t6 Etag : ”1−98da4da1fd7d7f90ceb752501d5b2321 ”7 Content−Type : t e x t / p l a i n ; c h a r s e t=ut f−88 Content−Length : 679

10 { ”ok” : t rue ,11 ” i d ” : ” t e s t ” ,12 ” r e v ” : ”1−98da4da1fd7d7f90ceb752501d5b2321 ”13 }

Page 43: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 18 / 49

Create a new document

1 $ c u r l − i −X PUT ht tp : // l o c a l h o s t :5984/ confoo / t e s t2 −d ’{” t i t l e ” : ” He l l o wor ld !”} ’3 HTTP/1 .1 201 Created4 S e r v e r : CouchDB /1 . 0 . 1 ( E r l ang OTP/R13B)5 Loca t i on : h t tp : // l o c a l h o s t :5984/ confoo / t e s t6 Etag : ”1−98da4da1fd7d7f90ceb752501d5b2321 ”7 Content−Type : t e x t / p l a i n ; c h a r s e t=ut f−88 Content−Length : 679

10 { ”ok” : t rue ,11 ” i d ” : ” t e s t ” ,12 ” r e v ” : ”1−98da4da1fd7d7f90ceb752501d5b2321 ”13 }

Page 44: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 18 / 49

Create a new document

1 $ c u r l − i −X PUT ht tp : // l o c a l h o s t :5984/ confoo / t e s t2 −d ’{” t i t l e ” : ” He l l o wor ld !”} ’3 HTTP/1 .1 201 Created4 S e r v e r : CouchDB /1 . 0 . 1 ( E r l ang OTP/R13B)5 Loca t i on : h t tp : // l o c a l h o s t :5984/ confoo / t e s t6 Etag : ”1−98da4da1fd7d7f90ceb752501d5b2321 ”7 Content−Type : t e x t / p l a i n ; c h a r s e t=ut f−88 Content−Length : 679

10 { ”ok” : t rue ,11 ” i d ” : ” t e s t ” ,12 ” r e v ” : ”1−98da4da1fd7d7f90ceb752501d5b2321 ”13 }

Page 45: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 19 / 49

Add an attachment

1 $ c u r l − i −X PUT ’ h t tp : // l o c a l h o s t :5984/ confoo / t e s t / l ogo . png? r e v=1−98da4da1fd7d7f90ceb752501d5b2321 ’

2 −−data−b i n a r y ’ @g raph i c s / couchdb . png ’3 −H ’ Content−Type : image/png ’4 HTTP/1 .1 201 Created5 S e r v e r : CouchDB /1 . 0 . 1 ( E r l ang OTP/R13B)6 Loca t i on : h t tp : // l o c a l h o s t :5984/ confoo / t e s t / l ogo . png7 Etag : ”2−d9053f096f374284332575ab6956a658 ”8 Content−Type : t e x t / p l a i n ; c h a r s e t=ut f−89 Content−Length : 67

1011 { ”ok” : t rue ,12 ” i d ” : ” t e s t ” ,13 ” r e v ” : ”2−d9053f096f374284332575ab6956a658 ”14 }

Page 46: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 19 / 49

Add an attachment

1 $ c u r l − i −X PUT ’ h t tp : // l o c a l h o s t :5984/ confoo / t e s t / l ogo . png? r e v=1−98da4da1fd7d7f90ceb752501d5b2321 ’

2 −−data−b i n a r y ’ @g raph i c s / couchdb . png ’3 −H ’ Content−Type : image/png ’4 HTTP/1 .1 201 Created5 S e r v e r : CouchDB /1 . 0 . 1 ( E r l ang OTP/R13B)6 Loca t i on : h t tp : // l o c a l h o s t :5984/ confoo / t e s t / l ogo . png7 Etag : ”2−d9053f096f374284332575ab6956a658 ”8 Content−Type : t e x t / p l a i n ; c h a r s e t=ut f−89 Content−Length : 67

1011 { ”ok” : t rue ,12 ” i d ” : ” t e s t ” ,13 ” r e v ” : ”2−d9053f096f374284332575ab6956a658 ”14 }

Page 47: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 19 / 49

Add an attachment

1 $ c u r l − i −X PUT ’ h t tp : // l o c a l h o s t :5984/ confoo / t e s t / l ogo . png? r e v=1−98da4da1fd7d7f90ceb752501d5b2321 ’

2 −−data−b i n a r y ’ @g raph i c s / couchdb . png ’3 −H ’ Content−Type : image/png ’4 HTTP/1 .1 201 Created5 S e r v e r : CouchDB /1 . 0 . 1 ( E r l ang OTP/R13B)6 Loca t i on : h t tp : // l o c a l h o s t :5984/ confoo / t e s t / l ogo . png7 Etag : ”2−d9053f096f374284332575ab6956a658 ”8 Content−Type : t e x t / p l a i n ; c h a r s e t=ut f−89 Content−Length : 67

1011 { ”ok” : t rue ,12 ” i d ” : ” t e s t ” ,13 ” r e v ” : ”2−d9053f096f374284332575ab6956a658 ”14 }

Page 48: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 19 / 49

Add an attachment

1 $ c u r l − i −X PUT ’ h t tp : // l o c a l h o s t :5984/ confoo / t e s t / l ogo . png? r e v=1−98da4da1fd7d7f90ceb752501d5b2321 ’

2 −−data−b i n a r y ’ @g raph i c s / couchdb . png ’3 −H ’ Content−Type : image/png ’4 HTTP/1 .1 201 Created5 S e r v e r : CouchDB /1 . 0 . 1 ( E r l ang OTP/R13B)6 Loca t i on : h t tp : // l o c a l h o s t :5984/ confoo / t e s t / l ogo . png7 Etag : ”2−d9053f096f374284332575ab6956a658 ”8 Content−Type : t e x t / p l a i n ; c h a r s e t=ut f−89 Content−Length : 67

1011 { ”ok” : t rue ,12 ” i d ” : ” t e s t ” ,13 ” r e v ” : ”2−d9053f096f374284332575ab6956a658 ”14 }

Page 49: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 20 / 49

Get the document back

1 $ c u r l − i −X GET ht tp : // l o c a l h o s t :5984/ confoo / t e s t2 HTTP/1 .1 200 OK3 Se r v e r : CouchDB /1 . 0 . 1 ( E r l ang OTP/R13B)4 Etag : ”2−d9053f096f374284332575ab6956a658 ”5 Content−Type : t e x t / p l a i n ; c h a r s e t=ut f−86 Content−Length : 17778 { ” i d ” : ” t e s t ” ,9 ” r e v ” : ”2−d9053f096f374284332575ab6956a658 ” ,

10 ” t i t l e ” : ” He l l o wor ld ! ” ,11 ” a t t achment s ” : {12 ” l ogo . png” : {13 ” c on t e n t t y p e ” : ” image/png” ,14 ” r e vpo s ” : 2 ,15 ” l e n g t h ” : 10785 ,16 ” s tub ” : t rue17 }18 }19 }

Page 50: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 20 / 49

Get the document back

1 $ c u r l − i −X GET ht tp : // l o c a l h o s t :5984/ confoo / t e s t2 HTTP/1 .1 200 OK3 Se r v e r : CouchDB /1 . 0 . 1 ( E r l ang OTP/R13B)4 Etag : ”2−d9053f096f374284332575ab6956a658 ”5 Content−Type : t e x t / p l a i n ; c h a r s e t=ut f−86 Content−Length : 17778 { ” i d ” : ” t e s t ” ,9 ” r e v ” : ”2−d9053f096f374284332575ab6956a658 ” ,

10 ” t i t l e ” : ” He l l o wor ld ! ” ,11 ” a t t achment s ” : {12 ” l ogo . png” : {13 ” c on t e n t t y p e ” : ” image/png” ,14 ” r e vpo s ” : 2 ,15 ” l e n g t h ” : 10785 ,16 ” s tub ” : t rue17 }18 }19 }

Page 51: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 20 / 49

Get the document back

1 $ c u r l − i −X GET ht tp : // l o c a l h o s t :5984/ confoo / t e s t2 HTTP/1 .1 200 OK3 Se r v e r : CouchDB /1 . 0 . 1 ( E r l ang OTP/R13B)4 Etag : ”2−d9053f096f374284332575ab6956a658 ”5 Content−Type : t e x t / p l a i n ; c h a r s e t=ut f−86 Content−Length : 17778 { ” i d ” : ” t e s t ” ,9 ” r e v ” : ”2−d9053f096f374284332575ab6956a658 ” ,

10 ” t i t l e ” : ” He l l o wor ld ! ” ,11 ” a t t achment s ” : {12 ” l ogo . png” : {13 ” c on t e n t t y p e ” : ” image/png” ,14 ” r e vpo s ” : 2 ,15 ” l e n g t h ” : 10785 ,16 ” s tub ” : t rue17 }18 }19 }

Page 52: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 20 / 49

Get the document back

1 $ c u r l − i −X GET ht tp : // l o c a l h o s t :5984/ confoo / t e s t2 HTTP/1 .1 200 OK3 Se r v e r : CouchDB /1 . 0 . 1 ( E r l ang OTP/R13B)4 Etag : ”2−d9053f096f374284332575ab6956a658 ”5 Content−Type : t e x t / p l a i n ; c h a r s e t=ut f−86 Content−Length : 17778 { ” i d ” : ” t e s t ” ,9 ” r e v ” : ”2−d9053f096f374284332575ab6956a658 ” ,

10 ” t i t l e ” : ” He l l o wor ld ! ” ,11 ” a t t achment s ” : {12 ” l ogo . png” : {13 ” c on t e n t t y p e ” : ” image/png” ,14 ” r e vpo s ” : 2 ,15 ” l e n g t h ” : 10785 ,16 ” s tub ” : t rue17 }18 }19 }

Page 53: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 21 / 49

Get the attachment back

1 $ c u r l − i −X GET ht tp : // l o c a l h o s t :5984/ confoo / t e s t / l ogo . png2 HTTP/1 .1 200 OK3 Se r v e r : CouchDB /1 . 0 . 1 ( E r l ang OTP/R13B)4 ETag : ”2−d9053f096f374284332575ab6956a658 ”5 Content−Type : image/png6 Content−Length : 107857 Content−Encoding : i d e n t i t y89 <PNG data>

Page 54: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 21 / 49

Get the attachment back

1 $ c u r l − i −X GET ht tp : // l o c a l h o s t :5984/ confoo / t e s t / l ogo . png2 HTTP/1 .1 200 OK3 Se r v e r : CouchDB /1 . 0 . 1 ( E r l ang OTP/R13B)4 ETag : ”2−d9053f096f374284332575ab6956a658 ”5 Content−Type : image/png6 Content−Length : 107857 Content−Encoding : i d e n t i t y89 <PNG data>

Page 55: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 21 / 49

Get the attachment back

1 $ c u r l − i −X GET ht tp : // l o c a l h o s t :5984/ confoo / t e s t / l ogo . png2 HTTP/1 .1 200 OK3 Se r v e r : CouchDB /1 . 0 . 1 ( E r l ang OTP/R13B)4 ETag : ”2−d9053f096f374284332575ab6956a658 ”5 Content−Type : image/png6 Content−Length : 107857 Content−Encoding : i d e n t i t y89 <PNG data>

Page 56: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 22 / 49

Attachments

I Attach to a documentI HTMLI JavaScriptI Images

I And you got your CouchApp

Page 57: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 22 / 49

Attachments

I Attach to a documentI HTMLI JavaScriptI Images

I And you got your CouchApp

Page 58: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 23 / 49

Outline

Overview

CouchDB

Rewrites & VHosts

Replication & Eventual Consistency

Conclusion

Page 59: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 24 / 49

Rewrites

I Allows to rewrite URLs

I Specified in design documents

1 { ” i d ” : ” d e s i g n /app” ,2 ” r e w r i t e s ” : [3 { ” from” : ”/ b log ” ,4 ” to ” : ” . . / . . / b l og / i ndex . html ” ,5 } ,. . .67 ]8 }

I Requested as:http://localhost:5984/db/_design/app/_rewrite/

I Rewrites to:http://localhost:5984/db/blog/index.html

Page 60: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 24 / 49

Rewrites

I Allows to rewrite URLs

I Specified in design documents

1 { ” i d ” : ” d e s i g n /app” ,2 ” r e w r i t e s ” : [3 { ” from” : ”/ b log ” ,4 ” to ” : ” . . / . . / b l og / i ndex . html ” ,5 } ,. . .67 ]8 }

I Requested as:http://localhost:5984/db/_design/app/_rewrite/

I Rewrites to:http://localhost:5984/db/blog/index.html

Page 61: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 24 / 49

Rewrites

I Allows to rewrite URLs

I Specified in design documents

1 { ” i d ” : ” d e s i g n /app” ,2 ” r e w r i t e s ” : [3 { ” from” : ”/ b log ” ,4 ” to ” : ” . . / . . / b l og / i ndex . html ” ,5 } ,. . .67 ]8 }

I Requested as:http://localhost:5984/db/_design/app/_rewrite/

I Rewrites to:http://localhost:5984/db/blog/index.html

Page 62: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 24 / 49

Rewrites

I Allows to rewrite URLs

I Specified in design documents

1 { ” i d ” : ” d e s i g n /app” ,2 ” r e w r i t e s ” : [3 { ” from” : ”/ b log ” ,4 ” to ” : ” . . / . . / b l og / i ndex . html ” ,5 } ,. . .67 ]8 }

I Requested as:http://localhost:5984/db/_design/app/_rewrite/

I Rewrites to:http://localhost:5984/db/blog/index.html

Page 63: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 25 / 49

Rewrites

I Parameters are possible

1 { ” i d ” : ” d e s i g n /app” ,2 ” r e w r i t e s ” : [3 { ” from” : ”/ images / : image” ,4 ” to ” : ” . . / . . / images / : image” ,5 } ,. . .67 ]8 }

I Requested as:http://localhost:

5984/db/_design/app/_rewrite/images/favicon.png

I Rewrites to:http://localhost:5984/db/images/favicon.png

Page 64: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 25 / 49

Rewrites

I Parameters are possible

1 { ” i d ” : ” d e s i g n /app” ,2 ” r e w r i t e s ” : [3 { ” from” : ”/ images / : image” ,4 ” to ” : ” . . / . . / images / : image” ,5 } ,. . .67 ]8 }

I Requested as:http://localhost:

5984/db/_design/app/_rewrite/images/favicon.png

I Rewrites to:http://localhost:5984/db/images/favicon.png

Page 65: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 26 / 49

Rewrites

I Match anything, including query parametersI Especially useful for views, will be covered later

1 { ” i d ” : ” d e s i g n /app” ,2 ” r e w r i t e s ” : [3 { ” from” : ”/∗” ,4 ” to ” : ” . . / . . / b l og /404 . html ” ,5 } ,. . .67 ]8 }

Page 66: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 27 / 49

vHosts

I Those are still pretty ugly URLs. . .

I vHosts to the rescue:1 $ ca t / e t c / couchdb/ l o c a l . i n i2 [ . . . ]3 myhost :5984 = /db/ d e s i g n /app/ r e w r i t e4 [ . . . ]

I Requested as:http://myhost:5984/blog

I Rewrites to:http://localhost:5984/db/blog/index.html

I A rewrite rule for "/" is also possible, of course. . .

Page 67: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 27 / 49

vHosts

I Those are still pretty ugly URLs. . .

I vHosts to the rescue:1 $ ca t / e t c / couchdb/ l o c a l . i n i2 [ . . . ]3 myhost :5984 = /db/ d e s i g n /app/ r e w r i t e4 [ . . . ]

I Requested as:http://myhost:5984/blog

I Rewrites to:http://localhost:5984/db/blog/index.html

I A rewrite rule for "/" is also possible, of course. . .

Page 68: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 27 / 49

vHosts

I Those are still pretty ugly URLs. . .

I vHosts to the rescue:1 $ ca t / e t c / couchdb/ l o c a l . i n i2 [ . . . ]3 myhost :5984 = /db/ d e s i g n /app/ r e w r i t e4 [ . . . ]

I Requested as:http://myhost:5984/blog

I Rewrites to:http://localhost:5984/db/blog/index.html

I A rewrite rule for "/" is also possible, of course. . .

Page 69: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 27 / 49

vHosts

I Those are still pretty ugly URLs. . .

I vHosts to the rescue:1 $ ca t / e t c / couchdb/ l o c a l . i n i2 [ . . . ]3 myhost :5984 = /db/ d e s i g n /app/ r e w r i t e4 [ . . . ]

I Requested as:http://myhost:5984/blog

I Rewrites to:http://localhost:5984/db/blog/index.html

I A rewrite rule for "/" is also possible, of course. . .

Page 70: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 28 / 49

Setting up a CouchApp

I Create vHostI Create view document, containing:

I Rewrite rulesI Attachments: HTML, JavaScript, ImagesI Optionally: View functions, show functions, list functionsI Document validation functions

Page 71: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 28 / 49

Setting up a CouchApp

I Create vHostI Create view document, containing:

I Rewrite rulesI Attachments: HTML, JavaScript, ImagesI Optionally: View functions, show functions, list functionsI Document validation functions

Page 72: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 28 / 49

Setting up a CouchApp

I Create vHostI Create view document, containing:

I Rewrite rulesI Attachments: HTML, JavaScript, ImagesI Optionally: View functions, show functions, list functionsI Document validation functions

Page 73: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 28 / 49

Setting up a CouchApp

I Create vHostI Create view document, containing:

I Rewrite rulesI Attachments: HTML, JavaScript, ImagesI Optionally: View functions, show functions, list functionsI Document validation functions

Page 74: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 28 / 49

Setting up a CouchApp

I Create vHostI Create view document, containing:

I Rewrite rulesI Attachments: HTML, JavaScript, ImagesI Optionally: View functions, show functions, list functionsI Document validation functions

Page 75: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 29 / 49

Demo

Page 76: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 30 / 49

Outline

Overview

CouchDB

Rewrites & VHosts

Replication & Eventual Consistency

Conclusion

Page 77: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 31 / 49

Handling consistency

I Two possibilities for handling distributed data:I Force to keep all servers in syncI Be partition tolerant and merge data, if required

I Eventually be consistent

Page 78: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 31 / 49

Handling consistency

I Two possibilities for handling distributed data:I Force to keep all servers in syncI Be partition tolerant and merge data, if required

I Eventually be consistent

Page 79: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 31 / 49

Handling consistency

I Two possibilities for handling distributed data:I Force to keep all servers in syncI Be partition tolerant and merge data, if required

I Eventually be consistent

Page 80: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 31 / 49

Handling consistency

I Two possibilities for handling distributed data:I Force to keep all servers in syncI Be partition tolerant and merge data, if required

I Eventually be consistent

Page 81: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 32 / 49

Remember:

I There is no ensured inter document consistency in CouchDB(relational integrity)

Page 82: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 33 / 49

Scaling: The CAP theoremI The CAP theorem, read more in “CouchDB: The Definitive

Guide” [JCA09]

Availability Partitiontolerance

Consistency

CouchDB

RDBMS PAXON

eventualconsistency

enforcedconsistency

consensusprotocols

I CouchDB employs “Eventual Consistency” [Vog09]

Page 83: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 34 / 49

Outline

Replication & Eventual ConsistencyReplicationFitlered ReplicationConflicts

Page 84: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 35 / 49

Replication

I Replication is trivial

1 $ c u r l −X POST ht tp : // l o c a l h o s t :5984/ r e p l i c a t e \2 −H ’ Content−Type : a p p l i c a t i o n / j s o n ’ \3 −d ’{” sou r c e ” :” confoo ” , \4 ” t a r g e t ” : ” h t tp : // u s e r : pass@192 . 1 6 8 . 1 . 3 : 5 9 8 4 / confoo ”} ’56 { ”ok” : t rue ,7 ” no changes ” : t rue ,8 ” s e s s i o n i d ” : ”73 d69e7b5cdaea059e55ed1db7802151 ” ,9 ” s o u r c e l a s t s e q ” : 141 ,

10 ” h i s t o r y ” : [ {11 . . .12 } ]13 }

I Source and target can be any combination of remote and localURLs

Page 85: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 35 / 49

Replication

I Replication is trivial

1 $ c u r l −X POST ht tp : // l o c a l h o s t :5984/ r e p l i c a t e \2 −H ’ Content−Type : a p p l i c a t i o n / j s o n ’ \3 −d ’{” sou r c e ” :” confoo ” , \4 ” t a r g e t ” : ” h t tp : // u s e r : pass@192 . 1 6 8 . 1 . 3 : 5 9 8 4 / confoo ”} ’56 { ”ok” : t rue ,7 ” no changes ” : t rue ,8 ” s e s s i o n i d ” : ”73 d69e7b5cdaea059e55ed1db7802151 ” ,9 ” s o u r c e l a s t s e q ” : 141 ,

10 ” h i s t o r y ” : [ {11 . . .12 } ]13 }

I Source and target can be any combination of remote and localURLs

Page 86: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 36 / 49

Demo

Page 87: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 37 / 49

Outline

Replication & Eventual ConsistencyReplicationFitlered ReplicationConflicts

Page 88: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 38 / 49

Filtered Replication

Server

Client Client Client

Page 89: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 38 / 49

Filtered Replication

Server

Client Client Client

Page 90: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 38 / 49

Filtered Replication

Server

Client Client Client

Filter

Page 91: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 38 / 49

Filtered Replication

Server

Client Client Client

Page 92: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 39 / 49

Filtered Replication

I Append a filter function to design document

1 { ” i d ” : ” d e s i g n /app” ,2 ” language ” : ” j a v a s c r i p t ” ,3 ” f i l t e r s ” : {4 ” f o r u s e r ” : ” f u n c t i o n ( doc , r eq ) { r e t u r n f a l s e ; }” , . . .56 }. . .78 }

Page 93: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 40 / 49

Filtered Replication

I Common filtering function

1 f u n c t i o n ( doc , r eq ) {2 i f ( ! r eq . u s e rC tx . name ) {3 throw ( ” Unautho r i z ed ! ” ) ;4 }56 i f ( doc . r e c i p i e n t s &&7 doc . r e c i p i e n t s . i ndexOf ( r eq . u s e rC tx . name ) !== −1 )8 {9 r e t u r n t rue ;

10 }1112 r e t u r n f a l s e ;13 }

Page 94: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 41 / 49

Filtered Replication

I Usage during replication

1 $ c u r l −X POST ht tp : // l o c a l h o s t :5984/ r e p l i c a t e \2 −H ’ Content−Type : a p p l i c a t i o n / j s o n ’ \3 −d ’{” sou r c e ” :” h t tp : // u s e r : pass@192 . 1 6 8 . 1 . 3 : 5 9 8 4 / confoo ” , \4 ” t a r g e t ” : ” confoo ” , \5 ” f i l t e r ” : ” app/ f o r u s e r ”} ’ . . .

Page 95: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 42 / 49

Outline

Replication & Eventual ConsistencyReplicationFitlered ReplicationConflicts

Page 96: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 43 / 49

Document conflict handling

Server

Page 97: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 43 / 49

Document conflict handling

Server

Page 98: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 43 / 49

Document conflict handling

Server

Page 99: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 43 / 49

Document conflict handling

Server

Page 100: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 44 / 49

Guidelines

I Keep documents small and segregatedI Aggregate data in views

I Check if you need manual conflict handlingI Document merging is highly application specific

Page 101: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 45 / 49

Examples

I Twitter App

I Blog posts & comments

I A wiki

Page 102: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 45 / 49

Examples

I Twitter App

I Blog posts & comments

I A wiki

Page 103: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 45 / 49

Examples

I Twitter App

I Blog posts & comments

I A wiki

Page 104: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 46 / 49

Security

I Document validation functionsI Limit changes based on users / groups

I Server / database adminsI Limit read / write access to databases

I Filtered replicationI Limit replicated user data based on users / groups

I You still might want to use an HTTP proxy

Page 105: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 46 / 49

Security

I Document validation functionsI Limit changes based on users / groups

I Server / database adminsI Limit read / write access to databases

I Filtered replicationI Limit replicated user data based on users / groups

I You still might want to use an HTTP proxy

Page 106: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 47 / 49

Outline

Overview

CouchDB

Rewrites & VHosts

Replication & Eventual Consistency

Conclusion

Page 107: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 48 / 49

Thanks for listening

I More information:I http://couchdb.orgI http://guide.couchdb.orgI http://couchapp.org

I More about us:I http://qafoo.com

I Please rate & comment:I http://joind.in/2778

Page 108: Distributed CouchApps - Embracing eventual consistency ... · Distributed CouchApps - Embracing eventual consistency2 / 49 About me I Kore Nordmann (, ,

Distributed CouchApps - Embracing eventual consistency 49 / 49

Bibliography I

[JCA09] Noah Slater J. Chris Anderson, Jan Lehnardt, Couchdb: Thedefinitive guide, O’Reilly Media, Inc., 2009.

[Vog09] Werner Vogels, Eventually consistent - revisited,http://www.allthingsdistributed.com/2008/12/eventually_

consistent.html, December 2009.