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
Two tiers architecture




Business Logic +   Data
  Presentation
Three tiers architecture



Presentation   Business Logic   Data
Tim Berners Lee
invented the world wide web
Three tiers internet style


                 View
                  +        Model
              Controller

Thin client
Evolution of
MVC Web App
Browser is a
 thin client
Talking to a
smart server
It all starts with
an HTTP request


      GET / HTTP/1.1
      Host: example.org
It hits the
Controller
Web server parses it
                 Controller
Dispatch to
front controller
                   Controller
Routing
          Controller
Custom action
                Controller
Gets data
from Model
Query on
model objects
                Model
ORM fits into SQL
                    Model
Hits a RDBMS
               Model
Populates a
   View
Template engine
                  View
HTML back to
 the server
               View
HTML back to
 the client


   HTTP/1.1 200 OK
That’s MVC
         Model




                 View




                    Controller
A little bit old school
We love AJAX speed
Just need an API
          Model




             View              Json View




                  Controller        Api Action
And some
javascript on client
                   Model




                      View              Json View


 Javascript View


                           Controller        Api Action
Nice
one ring to rule ‘em all
No
contraindications ?
Accidental
complexity
Every change
touches the server
Hard to scale


          We must
         add another
        serveeeeerrrr
Last technology cycle
                                  WE WERE HERE
              MVC             peak  of
                             inflated
                           expectation
Visibility




                                         productivity




                           disillusionment              obsolescence
              technology
                trigger

                                  maturity
Last technology cycle
                              peak  of
                             inflated
                           expectation


                                          NOW HERE
Visibility




                                         productivity




                           disillusionment              obsolescence
              technology
                trigger

                                  maturity
Last technology cycle
                              peak  of
                             inflated
                           expectation




                                                          IF YOU’RE
Visibility




                                                        AT THIS CONF
                                         productivity


                                                         YOU MAY BE
                                                        ALREADY HERE
                           disillusionment              obsolescence
              technology
                trigger

                                  maturity
Next technology to save us?
                  ?
 Visibility




              maturity
It’s a cycle
        but we know one thing ...
Visibility




                   maturity
Specialization
     is
    Good
Very opinionated
  document DB
Very good for
some 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

                      INS
                J   O
             NO
Schema-less data


                           DATA
                   YO   UR
          VO   LVE
         E
Incremental
        Reduce: sum(checkouts)
           Map/Reduce
                                                    430

Re-reduce
                              142                                          288




 Reduce            100                     42                    215                     73


            {                      {                      {                      {


  Map           "id": 1,
                "day": 20100123,
                "checkout": 100
                                       "id": 2,
                                       "day": 20100123,
                                       "checkout": 42
                                                              "id": 3,
                                                              "day": 20100123,
                                                              "checkout": 215
                                                                                     "id": 4,
                                                                                     "day": 20100123,
                                                                                     "checkout": 73
            }                      }                      }                      }
Multi-Version
Concurrency Control
Crash-only
                  architecture




lucky by design
A distributed DB
Eventual
consistency
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
                                    Jacob Kaplan-Moss
completely embraces the             Co-creator of Django

philosophies behind HTTP.”
Restful API
GET      /                          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 to
     program
//  MAP
function(doc)  {
    if(doc.date  &&  doc.title)  {
        emit(doc.date,  doc.title);
    }
}
JSON to
   exchange 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"
}
Applications
    are
 documents
Replication
    as
  deploy
A modern web
application server
CouchApps are
 javascript and html5
  applications served
directly from CouchDB
Problem
                            Model




                               View              Json View


Javascript View


                                    Controller        Api Action
Simple Solution

                  HTTP
Javascript View
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
User can post a message

On click POST message to couch
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?
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!
BASIC HTTP ?????

No SSL
Yes cookie auth
via _session api
UGLY URLS ?????



  Rewrites and
  Virtual hosts
Easy eh?
But it’s not that ring
Very good for
CRUD/small/simple
      web
federico.galassi@cleancode.it   giordano.scalzo@cleancode.it
twitter.com/federicogalassi     twitter.com/giordanoscalzo
slideshare.net/fgalassi         slideshare.net/giordano

CouchApps: Requiem for Accidental Complexity