98
CouchApps: Requiem for Accidental Complexity Federico Galassi Giordano Scalzo

CouchApps: Requiem for Accidental Complexity

Embed Size (px)

DESCRIPTION

Talk i gave at Nosqlday with Giordano Scalzo on March 25th 2011. It's about how CouchDB can replace a full serverside mvc stack making development of simple web apps a piece of cake Also http://federico.galassi.net/ http://www.nosqlday.it/ http://couchdb.apache.org/ Follow me on Twitter! https://twitter.com/federicogalassi

Citation preview

Page 1: CouchApps: Requiem for Accidental Complexity

CouchApps: Requiem for Accidental Complexity

Federico Galassi Giordano Scalzo

Page 2: CouchApps: Requiem for Accidental Complexity

We have a problem...

Page 3: CouchApps: Requiem for Accidental Complexity

Writing software is really hard...

Page 4: CouchApps: Requiem for Accidental Complexity

A little bit of history

Page 5: CouchApps: Requiem for Accidental Complexity

It wasn’t easy

Page 6: CouchApps: Requiem for Accidental Complexity

A little bit better

Page 7: CouchApps: Requiem for Accidental Complexity

Something happened

Page 8: CouchApps: Requiem for Accidental Complexity

A different way of programming

Page 9: CouchApps: Requiem for Accidental Complexity

DataBusiness Logic + Presentation

Two tiers architecture

Page 10: CouchApps: Requiem for Accidental Complexity

DataBusiness LogicPresentation

Three tiers architecture

Page 11: CouchApps: Requiem for Accidental Complexity

Tim Berners Lee invented the world wide web

Page 12: CouchApps: Requiem for Accidental Complexity

ModelView+

Thin client

Controller

Three tiers internet style

Page 13: CouchApps: Requiem for Accidental Complexity

Evolution ofMVC Web App

Page 14: CouchApps: Requiem for Accidental Complexity

Browser is a thin client

Page 15: CouchApps: Requiem for Accidental Complexity

Talking to asmart server

Page 16: CouchApps: Requiem for Accidental Complexity

It all starts withan HTTP request

GET / HTTP/1.1Host: example.org

Page 17: CouchApps: Requiem for Accidental Complexity

It hits theController

Page 18: CouchApps: Requiem for Accidental Complexity

Web server parses itController

Page 19: CouchApps: Requiem for Accidental Complexity

Controller

Dispatch tofront controller

Page 20: CouchApps: Requiem for Accidental Complexity

Controller

Routing

Page 21: CouchApps: Requiem for Accidental Complexity

Controller

Custom action

Page 22: CouchApps: Requiem for Accidental Complexity

Gets datafrom Model

Page 23: CouchApps: Requiem for Accidental Complexity

Query onmodel objects

Model

Page 24: CouchApps: Requiem for Accidental Complexity

ORM fits into SQLModel

Page 25: CouchApps: Requiem for Accidental Complexity

Hits a RDBMSModel

Page 26: CouchApps: Requiem for Accidental Complexity

Populates aView

Page 27: CouchApps: Requiem for Accidental Complexity

Template engineView

Page 28: CouchApps: Requiem for Accidental Complexity

HTML back tothe server

View

Page 29: CouchApps: Requiem for Accidental Complexity

HTTP/1.1 200 OK

HTML back tothe client

Page 30: CouchApps: Requiem for Accidental Complexity

Model

View

Controller

That’s MVC

Page 31: CouchApps: Requiem for Accidental Complexity

A little bit old school

Page 32: CouchApps: Requiem for Accidental Complexity

We love AJAX speed

Page 33: CouchApps: Requiem for Accidental Complexity

Model

View

Just need an API

Json View

Api ActionController

Page 34: CouchApps: Requiem for Accidental Complexity

And somejavascript on client

Model

View Json View

Api ActionController

Javascript View

Page 35: CouchApps: Requiem for Accidental Complexity

Niceone ring to rule ‘em all

Page 36: CouchApps: Requiem for Accidental Complexity

Nocontraindications ?

Page 37: CouchApps: Requiem for Accidental Complexity

Accidentalcomplexity

Page 38: CouchApps: Requiem for Accidental Complexity

Every changetouches the server

Page 39: CouchApps: Requiem for Accidental Complexity

Hard to scale

We mustadd another

serveeeeerrrr

Page 40: CouchApps: Requiem for Accidental Complexity

Last technology cycleVisib

ility

maturity

technologytrigger

peak  ofinflated

expectation

disillusionment

productivity

obsolescence

MVCWE WERE HERE

Page 41: CouchApps: Requiem for Accidental Complexity

Last technology cycleVisib

ility

maturity

technologytrigger

peak  ofinflated

expectation

disillusionment

productivity

obsolescence

NOW HERE

Page 42: CouchApps: Requiem for Accidental Complexity

Last technology cycleVisib

ility

maturity

technologytrigger

peak  ofinflated

expectation

disillusionment

productivity

obsolescence

IF YOU’REAT THIS CONFYOU MAY BE

ALREADY HERE

Page 43: CouchApps: Requiem for Accidental Complexity

Visib

ility

maturity

?

Next technology to save us?

Page 44: CouchApps: Requiem for Accidental Complexity

Visib

ility

maturity

It’s a cyclebut we know one thing ...

Page 45: CouchApps: Requiem for Accidental Complexity

Specializationis

Good

Page 46: CouchApps: Requiem for Accidental Complexity
Page 47: CouchApps: Requiem for Accidental Complexity
Page 48: CouchApps: Requiem for Accidental Complexity

Very opinionateddocument DB

Page 49: CouchApps: Requiem for Accidental Complexity

Very good forsome problems

Page 50: CouchApps: Requiem for Accidental Complexity

A document DB{        "company":  "CleanCode",        "members":  [                {  "name":  "Gabriele  Lana",  "role":  "software  craftsman"},                {  "name":  "Federico  Galassi",  "role":  "software  craftsman"},                {  "name":  "Giordano  Scalzo",  "role":  "software  craftsman"}        ]}

Page 51: CouchApps: Requiem for Accidental Complexity

Self contained data

NO JOINS

Page 52: CouchApps: Requiem for Accidental Complexity

Schema-less data

EVOLVE YOUR DATA

Page 53: CouchApps: Requiem for Accidental Complexity

{

"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

Page 54: CouchApps: Requiem for Accidental Complexity

Multi-VersionConcurrency Control

Page 55: CouchApps: Requiem for Accidental Complexity

Crash-onlyarchitecture

lucky by design

Page 56: CouchApps: Requiem for Accidental Complexity

A distributed DB

Page 57: CouchApps: Requiem for Accidental Complexity

Eventualconsistency

Page 58: CouchApps: Requiem for Accidental Complexity

Incremental replication

Page 59: CouchApps: Requiem for Accidental Complexity

Automatic conflict resolution

Page 60: CouchApps: Requiem for Accidental Complexity

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

Page 61: CouchApps: Requiem for Accidental Complexity

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

Page 62: CouchApps: Requiem for Accidental Complexity

Javascript toprogram

//  MAPfunction(doc)  {    if(doc.date  &&  doc.title)  {        emit(doc.date,  doc.title);    }}

Page 63: CouchApps: Requiem for Accidental Complexity

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"}

Page 64: CouchApps: Requiem for Accidental Complexity

Applicationsare

documents

Page 65: CouchApps: Requiem for Accidental Complexity

Replicationas

deploy

Page 66: CouchApps: Requiem for Accidental Complexity

A modern web application server

Page 67: CouchApps: Requiem for Accidental Complexity

CouchApps arejavascript and html5applications served

directly from CouchDB

Page 68: CouchApps: Requiem for Accidental Complexity

Model

View Json View

Api ActionController

Javascript View

Problem

Page 69: CouchApps: Requiem for Accidental Complexity

Javascript View

Simple Solution

HTTP

Page 70: CouchApps: Requiem for Accidental Complexity

Experiment:5 minutes comet chat

Page 71: CouchApps: Requiem for Accidental Complexity

The couchapp tool

Page 72: CouchApps: Requiem for Accidental Complexity

Generate the app

Page 73: CouchApps: Requiem for Accidental Complexity

Configure the app

Page 74: CouchApps: Requiem for Accidental Complexity

Deploy the app

Page 75: CouchApps: Requiem for Accidental Complexity

That’s it

Page 76: CouchApps: Requiem for Accidental Complexity

That’s it

Page 77: CouchApps: Requiem for Accidental Complexity

User can post a message

Add form elements

Page 78: CouchApps: Requiem for Accidental Complexity

On click POST message to couch

User can post a message

Page 79: CouchApps: Requiem for Accidental Complexity

User can post a message

Yes!

Page 80: CouchApps: Requiem for Accidental Complexity

User gets a stream of messages

Add messages container

Page 81: CouchApps: Requiem for Accidental Complexity

User gets a stream of messages

Page 82: CouchApps: Requiem for Accidental Complexity

User gets a stream of messages

Yes!

Page 83: CouchApps: Requiem for Accidental Complexity

Validation?

Page 84: CouchApps: Requiem for Accidental Complexity

Authentication?

Page 85: CouchApps: Requiem for Accidental Complexity

Create a user

Authentication?

Page 86: CouchApps: Requiem for Accidental Complexity

Add Form Elements

Authentication?

Page 87: CouchApps: Requiem for Accidental Complexity

HTTP Basic Auth

Authentication?

Page 88: CouchApps: Requiem for Accidental Complexity

If it goes wrong

Authentication?

Page 89: CouchApps: Requiem for Accidental Complexity

Check sender is the authenticated user

Authentication?

Page 90: CouchApps: Requiem for Accidental Complexity

{ sender: “whitehouse” } // spoofing

Authentication?

Page 91: CouchApps: Requiem for Accidental Complexity

Yes!

Authentication?

Page 92: CouchApps: Requiem for Accidental Complexity

BASIC HTTP ?????

No SSLYes cookie auth via _session api

Page 93: CouchApps: Requiem for Accidental Complexity

UGLY URLS ?????

Rewrites and Virtual hosts

Page 94: CouchApps: Requiem for Accidental Complexity

Easy eh?

Page 95: CouchApps: Requiem for Accidental Complexity

But it’s not that ring

Page 96: CouchApps: Requiem for Accidental Complexity

Very good forCRUD/small/simple

web

Page 97: CouchApps: Requiem for Accidental Complexity