CouchApps: Requiem for Accidental Complexity
Federico Galassi Giordano Scalzo
We have a problem...
Writing software is really hard...
A little bit of history
It wasn’t easy
A little bit better
Something happened
A different way of programming
DataBusiness Logic + Presentation
Two tiers architecture
DataBusiness LogicPresentation
Three tiers architecture
Tim Berners Lee invented the world wide web
ModelView+
Thin client
Controller
Three tiers internet style
Evolution ofMVC Web App
Browser is a thin client
Talking to asmart server
It all starts withan HTTP request
GET / HTTP/1.1Host: example.org
It hits theController
Web server parses itController
Controller
Dispatch tofront controller
Controller
Routing
Controller
Custom action
Gets datafrom Model
Query onmodel objects
Model
ORM fits into SQLModel
Hits a RDBMSModel
Populates aView
Template engineView
HTML back tothe server
View
HTTP/1.1 200 OK
HTML back tothe client
Model
View
Controller
That’s MVC
A little bit old school
We love AJAX speed
Model
View
Just need an API
Json View
Api ActionController
And somejavascript on client
Model
View Json View
Api ActionController
Javascript View
Niceone ring to rule ‘em all
Nocontraindications ?
Accidentalcomplexity
Every changetouches the server
Hard to scale
We mustadd another
serveeeeerrrr
Last technology cycleVisib
ility
maturity
technologytrigger
peak ofinflated
expectation
disillusionment
productivity
obsolescence
MVCWE WERE HERE
Last technology cycleVisib
ility
maturity
technologytrigger
peak ofinflated
expectation
disillusionment
productivity
obsolescence
NOW HERE
Last technology cycleVisib
ility
maturity
technologytrigger
peak ofinflated
expectation
disillusionment
productivity
obsolescence
IF YOU’REAT THIS CONFYOU MAY BE
ALREADY HERE
Visib
ility
maturity
?
Next technology to save us?
Visib
ility
maturity
It’s a cyclebut we know one thing ...
Specializationis
Good
Very opinionateddocument DB
Very good forsome problems
A document DB{ "company": "CleanCode", "members": [ { "name": "Gabriele Lana", "role": "software craftsman"}, { "name": "Federico Galassi", "role": "software craftsman"}, { "name": "Giordano Scalzo", "role": "software craftsman"} ]}
Self contained data
NO JOINS
Schema-less data
EVOLVE YOUR DATA
{
"id": 1,
"day": 20100123,
"checkout": 100
}
{
"id": 2,
"day": 20100123,
"checkout": 42
}
{
"id": 3,
"day": 20100123,
"checkout": 215
}
{
"id": 4,
"day": 20100123,
"checkout": 73
}
100 42 215 73
Reduce: sum(checkouts)
142 288
430
IncrementalMap/Reduce
Map
Reduce
Re-reduce
Multi-VersionConcurrency Control
Crash-onlyarchitecture
lucky by design
A distributed DB
Eventualconsistency
Incremental replication
Automatic conflict resolution
Built of the Web“Django may be built for the Web, but CouchDB is built of the Web. I’ve never seen software that so completely embraces the philosophies behind HTTP.”
Jacob Kaplan-MossCo-creator of Django
Restful APIGET / Returns MOTD
GET /_all_dbs List of databases
POST /_restart Restart the server
GET /_log Tail of the server’s log
GET /_config Entire server config
GET /_config/<section>/<key> Single config value
PUT /_config/<section>/<key> Set a single config value
GET /<db> Database info
PUT /<db> Create new database
DELETE /<db> Delete database
GET /<db>/<doc> Latest revision of the document
PUT /<db>/<doc> Insert a new revision of the document
... and everything else
Javascript toprogram
// MAPfunction(doc) { if(doc.date && doc.title) { emit(doc.date, doc.title); }}
JSON toexchange data
{ "_id":"hello-‐world", "_rev":"43FBA4E7AB",
"title":"Hello World", "body":"Well hello and welcome to my new blog...", "date":"2009/01/15 15:52:20"}
Applicationsare
documents
Replicationas
deploy
A modern web application server
CouchApps arejavascript and html5applications served
directly from CouchDB
Model
View Json View
Api ActionController
Javascript View
Problem
Javascript View
Simple Solution
HTTP
Experiment:5 minutes comet chat
The couchapp tool
Generate the app
Configure the app
Deploy the app
That’s it
That’s it
User can post a message
Add form elements
On click POST message to couch
User can post a message
User can post a message
Yes!
User gets a stream of messages
Add messages container
User gets a stream of messages
User gets a stream of messages
Yes!
Validation?
Authentication?
Create a user
Authentication?
Add Form Elements
Authentication?
HTTP Basic Auth
Authentication?
If it goes wrong
Authentication?
Check sender is the authenticated user
Authentication?
{ sender: “whitehouse” } // spoofing
Authentication?
Yes!
Authentication?
BASIC HTTP ?????
No SSLYes cookie auth via _session api
UGLY URLS ?????
Rewrites and Virtual hosts
Easy eh?
But it’s not that ring
Very good forCRUD/small/simple
web
[email protected] [email protected]/giordanoscalzotwitter.com/federicogalassi
slideshare.net/fgalassi slideshare.net/giordano