Содержание

Слайд 2

Outline and § ELLS sections

From 100,000 foot view to 500 foot

Outline and § ELLS sections From 100,000 foot view to 500 foot
view of SaaS
Client-server architecture, HTTP, URIs, cookies (§2.1-2.2)
HTML & CSS, XML & XPath (§2.2-2.3)
3-tier shared-nothing architecture, horizontal scaling (§2.4)
model-view-controller design pattern (§2.5)
Models: ActiveRecord & CRUD (§2.6)
Routes, controllers, and REST (§2.7)
Template views (§2.8)
Fallacies & pitfalls, perspectives (§2.9-2.12)
Patterns, architecture, & perspective (§2.10)

Слайд 3

The Web as a Client-Server System; TCP/IP intro

ELLS §2.1–2.2
Armando Fox

The Web as a Client-Server System; TCP/IP intro ELLS §2.1–2.2 Armando Fox

Слайд 4

Reminder

This material expands on and gives additional perspectives/demos about the material in

Reminder This material expands on and gives additional perspectives/demos about the material
ELLS
We assume you will watch the screencasts too (screencast.saasbook.info)
It does not replace reading that material

http://saasbook.info

Слайд 5

Web at 100,000 feet

The web is a client/server architecture
It is fundamentally request/reply

Web at 100,000 feet The web is a client/server architecture It is
oriented

Web browser

Слайд 7

Client-Server vs. Peer-to-Peer

High-level architecture of the overall system
Soon we’ll talk about architecture

Client-Server vs. Peer-to-Peer High-level architecture of the overall system Soon we’ll talk
“inside” boxes
Client & server each specialized for their tasks
Client: ask questions on behalf of users
Server: wait for & respond to questions, serve many clients
Design Patterns capture common structural solutions to recurring problems
Client-Server is an architectural pattern

Слайд 8

GET /bears/

Nuts and bolts: TCP/IP protocols

IP (Internet Protocol) address identifies a physical

GET /bears/ Nuts and bolts: TCP/IP protocols IP (Internet Protocol) address identifies
network interface with four octets, e.g. 128.32.244.172
Special address 127.0.0.1 is “this computer”, named localhost, even if not connected to the Internet!
TCP/IP (Transmission Control Protocol/Internet Protocol)
IP: no-guarantee, best-effort service that delivers packets from one IP address to another
TCP: make IP reliable by detecting “dropped” packets, data arriving out of order, transmission errors, slow networks, etc., and respond appropriately
TCP ports allow multiple TCP apps on same computer
Vint Cerf & Bob Kahn: 2004 Turing Award for Internet architecture & protocols, incl. TCP/IP

GET /bears/

HTTP/0.9 200 OK

HTTP/0.9 200 OK

Слайд 9

Web at 100,000 feet

The web is a client/server architecture
It is fundamentally request/reply

Web at 100,000 feet The web is a client/server architecture It is
oriented
Domain Name System (DNS) is another kind of server that maps names to IP addresses

Web browser

DNS server

Слайд 10

Now that we’re talking, what do we say? Hypertext Transfer Protocol

an ASCII-based request/reply

Now that we’re talking, what do we say? Hypertext Transfer Protocol an
protocol for transferring information on the Web
HTTP request includes:
request method (GET, POST, etc.)
Uniform Resource Identifier (URI)
HTTP protocol version understood by the client
headers—extra info regarding transfer request
HTTP response from server
Protocol version & Status code =>
Response headers
Response body

HTTP status codes:
2xx — all is well
3xx — resource moved
4xx — access problem
5xx — server error

Слайд 11

HTTP > TCP > IP

TCP > DNS

All the above are correct




Assuming “>”

HTTP > TCP > IP TCP > DNS All the above are
means “relies on”, which statement is NOT correct:

Слайд 12

Cookies

Observation: HTTP is stateless
Early Web 1.0 problem: how to guide a user

Cookies Observation: HTTP is stateless Early Web 1.0 problem: how to guide
“through” a flow of pages?
use IP address to identify returning user? ✖ public computers, users sharing single IP
embed per-user junk into URI query string? ✖ breaks caching
Quickly superseded by cookies
Watch: screencast.saasbook.info

Слайд 13

Uses of cookies

Most sites quickly realized that the per-user state could

Uses of cookies Most sites quickly realized that the per-user state could
be used for lots of things:
customization (“My Yahoo”)
click tracking/flow tracking
authentication (logged in or not)
Which of these could be implemented on the client side? Which ones shouldn’t be and why?
A golden rule: don’t trust the client—cookies must be tamper-evident

Слайд 14

SaaS app; browser

HTTP request; browser

SaaS app; HTTP response




A ____ can create and

SaaS app; browser HTTP request; browser SaaS app; HTTP response ☐ ☐
modify cookies; the ____ is responsible for including the correct cookie with each request

Слайд 15

HTML+CSS

ELLS §2.3 Armando Fox

HTML+CSS ELLS §2.3 Armando Fox

Слайд 17

CS 142 Lecture Notes: HTML

Slide

Introduction
This article is a review of the

CS 142 Lecture Notes: HTML Slide Introduction This article is a review
book
Dietary Preferences of Penguins,
by Alice Jones and Bill Smith. Jones
and Smith's controversial work makes
three hard-to-swallow claims about
penguins:
First, that penguins actually prefer
tropical foods such as bananas and
pineapple to their traditional diet
of fish
Second, that tropical foods give
penguins an odor that makes them
unattractive to their traditional
predators

Слайд 18

CS 142 Lecture Notes: HTML

Slide

Introduction


This article is a review of

CS 142 Lecture Notes: HTML Slide Introduction This article is a review
the book
Dietary Preferences of Penguins,
by Alice Jones and Bill Smith. Jones and Smith's controversial work makes three hard-to-swallow claims about penguins:




  • First, that penguins actually prefer tropical foods such as bananas and pineapple to their traditional diet of fish


  • Second, that tropical foods give penguins an odor that makes them unattractive to their traditional predators


...

Слайд 19

CS 142 Lecture Notes: HTML

Slide

Introduction


This article is a review of the

CS 142 Lecture Notes: HTML Slide Introduction This article is a review
book
Dietary Preferences of Penguins,
by Alice Jones and Bill Smith. Jones
and Smith's controversial work makes
three hard-to-swallow claims about
penguins:


  • First, ...

    Introduction
    This article is a review of the book Dietary Preferences of Penguins, by Alice Jones and Bill Smith. Jones and Smith's controversial work makes two hard-to-swallow claims about penguins:
    First, that penguins actually prefer tropical foods such as bananas and pineapple to their traditional diet of fish
    Second, that tropical foods give penguins an odor that makes them unattractive to their traditional predators
    ...

Слайд 20

HTML ~1.0

Descendant of IBM’s Generalized Markup Language (1960’s) via SGML (Standard Generalized

HTML ~1.0 Descendant of IBM’s Generalized Markup Language (1960’s) via SGML (Standard
Markup Language, 1986)
Document = Hierarchical collection of elements
inline (headings, tables, lists...)
embedded (images, JavaScript code...)
forms—allow user to submit simple input (text, radio/check buttons, dropdown menus...)
Each element can have attributes (many optional) and some elements also have content
of particular interest: id and class attributes, for styling

Слайд 21

Cascading Style Sheets

Idea: visual appearance of page described in a separate document

Cascading Style Sheets Idea: visual appearance of page described in a separate
(stylesheet)
accessibility
branding/targeting
separate designers’ & developers’ concerns
Current best practice: HTML markup should contain no visual styling information

Слайд 22

How does it work?

(inside element) says what stylesheet

How does it work? (inside element) says what stylesheet goes with this
goes with this HTML page
HTML id & class attributes important in CSS
id must be unique within this page
same class can be attached to many elements

Слайд 23

Selectors identify specific tag(s)

Selectors identify specific tag(s) Welcome, Armando tag name: h1 class name: .pageFrame
src="welcome.jpg" id="welcome"/>

tag name: h1
class name: .pageFrame
element ID: #pageHead
tag name & class: div.pageFrame
tag name & id: img#welcome (usually redundant)
descendant relationship: div .custName
Attributes inherit browser defaults unless overridden

Слайд 24

p .a

.a span

All of these




Which CSS selector will select only the word

p .a .a span All of these ☐ ☐ ☐ Which CSS
“bar” for styling:

foo, bar


Слайд 25

3-tier shared-nothing architecture & scaling

ELLS §2.4
Armando Fox

3-tier shared-nothing architecture & scaling ELLS §2.4 Armando Fox

Слайд 27

Dynamic content generation

In the Elder Days, most web pages were (collections of)

Dynamic content generation In the Elder Days, most web pages were (collections
plain old files
But most interesting Web 1.0/e-commerce sites actually run a program to generate the “page”
Originally: templates with embedded code “snippets”
Eventually, code became “tail that wagged the dog” and moved out of the Web server

Слайд 28

Sites that are really programs (SaaS)

How do you:
“map” URI to correct program

Sites that are really programs (SaaS) How do you: “map” URI to
& function?
pass arguments?
invoke program on server?
handle persistent storage?
handle cookies?
handle errors?
package output back to user?
Frameworks support these common tasks

presentation (Web server)

your app

Common Gateway Interface (CGI)

Filesystem
or database

persistence

logic (app)

client (browser)

Слайд 29

Developer environment vs. medium-scale deployment

Developer environment vs. medium-scale deployment

Слайд 30

“Shared nothing”

“Shared nothing”

Слайд 31

Sharding vs. Replication

Partition data across independent “shards”?
+ Scales great
Bad when operations touch

Sharding vs. Replication Partition data across independent “shards”? + Scales great Bad
>1 table
Example use: user profile
Replicate all data everywhere?
+ Multi-table queries fast
Hard to scale: writes must propagate to all copies => temporary inconsistency in data values
Example: Facebook wall posts/“likes”

Слайд 32

Summary: Web 1.0 SaaS

Browser requests web resource (URI) using HTTP
HTTP is a

Summary: Web 1.0 SaaS Browser requests web resource (URI) using HTTP HTTP
simple request-reply protocol that relies on TCP/IP
In SaaS, most URI’s cause a program to be run, rather than a static file to be fetched
HTML is used to encode content, CSS to style it visually
Cookies allow server to track client
Browser automatically passes cookie to server on each request
Server may change cookie on each response
Typical usage: cookie includes a handle to server-side information
That’s why some sites don’t work if cookies are completely disabled
Frameworks make all these abstractions convenient for programmers to use, without sweating the details
...and help map SaaS to 3-tier, shared-nothing architecture

Слайд 33

(a) Firefox (b) Apache web server (c) PostgreSQL

(a) Microsoft Internet Information

(a) Firefox (b) Apache web server (c) PostgreSQL (a) Microsoft Internet Information
Server (b) Rack+Rails (c) Apache web server

(a) Firefox (b) Microsoft Internet Information Server (c) MySQL




Match the terms:
(a) presentation tier, (b) logic tier, (c) persistence tier

Слайд 34

Model-View-Controller

ELLS §2.5
Armando Fox

Model-View-Controller ELLS §2.5 Armando Fox

Слайд 36

The MVC Design Pattern

Goal: separate organization of data (model) from UI &

The MVC Design Pattern Goal: separate organization of data (model) from UI
presentation (view) by introducing controller
mediates user actions requesting access to data
presents data for rendering by the view
Web apps may seem “obviously” MVC by design, but other alternatives are possible...

Controller

Model

View

Слайд 37

Each entity has a model, controller, & set of views

Each entity has a model, controller, & set of views

Слайд 38

Alternatives to MVC

Rails supports SaaS apps structured as MVC, but other architectures

Alternatives to MVC Rails supports SaaS apps structured as MVC, but other
may be better fit for some apps.

Слайд 39

All MVC apps have both a “client” part (e.g. Web browser) and

All MVC apps have both a “client” part (e.g. Web browser) and
a “cloud” part (e.g. Rails app on cloud).

Model-View-Controller is just one of several possible ways to structure a SaaS app.

Peer-to-peer apps can be structured as Model-View-Controller.




Which statement is NOT true about the Model-View-Controller (MVC) architectural pattern:

Слайд 40

Models, Databases, and Active Record

ELLS §2.6
Armando Fox

Models, Databases, and Active Record ELLS §2.6 Armando Fox

Слайд 42

In-Memory vs. In-Storage objects

How to represent persisted object in storage
Example: Movie and

In-Memory vs. In-Storage objects How to represent persisted object in storage Example:
Reviews
Basic operations on object: CRUD (Create, Read, Update, Delete)
ActiveRecord: every model knows how to CRUD itself, using common mechanisms

#
m.name, m.rating, ...

?

marshal/serialize

unmarshal/deserialize

#
m.name, m.rating, ...

Слайд 43

Rails Models Store Data in Relational Databases (RDBMS)

Each type of model

Rails Models Store Data in Relational Databases (RDBMS) Each type of model
gets its own database table
All rows in table have identical structure
1 row in table == one model instance
Each column stores value of an attribute of the model
Each row has unique value for primary key (by convention, in Rails this is an integer and is called id)
Schema: Collection of all tables and their structure

Слайд 44

Alternative: DataMapper

Data Mapper associates separate mapper with each model
Idea: keep mapping independent

Alternative: DataMapper Data Mapper associates separate mapper with each model Idea: keep
of particular data store used => works with more types of databases
Used by Google AppEngine
Con: can’t exploit RDBMS features to simplify complex queries & relationships
We’ll revisit when talking about associations

Слайд 45

Part of the Model’s job is to convert between in-memory and stored

Part of the Model’s job is to convert between in-memory and stored
representations of objects.

Although Model data is displayed by the View, a Models’ direct interaction is with Controllers.

Although DataMapper doesn’t use relational databases, it’s a valid way to implement a Model.




Which statement is not true about the Model in Model-View-Controller:

Слайд 46

Controllers, Routes, and RESTfulness

ELLS §2.7
Armando Fox

Controllers, Routes, and RESTfulness ELLS §2.7 Armando Fox

Слайд 48

Routes

In MVC, each interaction the user can do is handled by a

Routes In MVC, each interaction the user can do is handled by
controller action
Ruby method that handles that interaction
A route maps to controller action

Слайд 49

Brief Intro to Rails’ Routing Subsystem

dispatch to correct controller action
provides helper

Brief Intro to Rails’ Routing Subsystem dispatch to correct controller action provides
methods that generate a pair given a controller action
parses query parameters from both URI and form submission into a convenient hash
Built-in shortcuts to generate all CRUD routes (though most apps will also have other routes)

Слайд 50

GET /movies/3/edit HTTP/1.0

Matches route:
GET /movies/:id/edit {:action=>"edit", :controller=>"movies"}
Parse wildcard parameters: params[:id] = "3"
Dispatch

GET /movies/3/edit HTTP/1.0 Matches route: GET /movies/:id/edit {:action=>"edit", :controller=>"movies"} Parse wildcard parameters:
to edit method in movies_controller.rb
To include a URI in generated view that will submit the form to the update controller action with params[:id]==3, call helper: update_movie_path(3) # => PUT /movies/3

Слайд 51

REST (Representational State Transfer)

Idea: Self-contained requests specify what resource to operate on

REST (Representational State Transfer) Idea: Self-contained requests specify what resource to operate
and what to do to it
Roy Fielding’s PhD thesis, 2000
Wikipedia: “a post hoc description of the features that made the Web successful”
A service (in the SOA sense) whose operations are like this is a RESTful service
Ideally, RESTful URIs name the operations
Let’s see an anti-example:

http://pastebin.com/edF2NzCF

Слайд 52

Every route must eventually trigger a controller action.

One common set of

Every route must eventually trigger a controller action. One common set of
RESTful actions is the CRUD actions on models.

The route always contains one or more parameters, such as :id, to identify the resource




Which statement is NOT true regarding Rails RESTful routes and the resources to which they refer:

Слайд 53

Template Views and Haml

ELLS §2.8
Armando Fox

Template Views and Haml ELLS §2.8 Armando Fox

Слайд 55

Template View pattern

View consists of markup with selected interpolation to happen at

Template View pattern View consists of markup with selected interpolation to happen
runtime
Usually, values of variables or result of evaluating short bits of code
In Elder Days, this was the app (e.g. PHP)
Alternative: Transform View

Haml

Closure

Closure

Слайд 56

Haml is HTML on a diet

%h1.pagename All Movies
%table#movies
%thead
%tr
%th Movie

Haml is HTML on a diet %h1.pagename All Movies %table#movies %thead %tr
Title
%th Release Date
%th More Info
%tbody
- @movies.each do |movie|
%tr
%td= movie.title
%td= movie.release_date
%td= link_to "More on #{movie.title}", | movie_path(movie) |
= link_to 'Add new movie', new_movie_path

Слайд 57

Don’t put code in your views

Syntactically, you can put any code in

Don’t put code in your views Syntactically, you can put any code
view
But MVC advocates thin views & controllers
Haml makes deliberately awkward to put in lots of code
Helpers (methods that “prettify” objects for including in views) have their own place in Rails app
Alternative to Haml: html.erb (Embedded Ruby) templates, look more like PHP

Слайд 58

It will work when developing against a “toy” database, but not in

It will work when developing against a “toy” database, but not in
production

It won’t work, because Views can’t communicate directly with Models

Behavior varies depending on the app




What happens if you embed code in your Rails views that directly accesses the model?

Слайд 59

Summary & Reflections: SaaS Architecture

Armando Fox

Summary & Reflections: SaaS Architecture Armando Fox

Слайд 60

The big picture (technologies)

Controller

View

Model

• URI’s, HTTP, TCP/IP stack
• REST & RESTful routes

The big picture (technologies) Controller View Model • URI’s, HTTP, TCP/IP stack
Databases & migrations
• CRUD

• HTML & CSS
• XML & XPath

Слайд 61

c. 2008: “Rails doesn’t scale”

Scalability is an architectural concern—not confined to language

c. 2008: “Rails doesn’t scale” Scalability is an architectural concern—not confined to
or framework
The stateless tiers of 3-tier arch do scale
With cloud computing, just worry about constants
Traditional relational databases do not scale
Various solutions combining relational and non-relational storage (“NoSQL”) scale much better
DataMapper works well with some of them
Intelligent use of caching (later in course) can greatly improve the constant factors

Слайд 62

Frameworks, Apps, Design patterns

Many design patterns so far, more to come
In 1995,

Frameworks, Apps, Design patterns Many design patterns so far, more to come
it was the wild west: biggest Web sites were minicomputers, not 3-tier/cloud
Best practices (patterns) “extracted” from experience and captured in frameworks
But API’s transcended it: 1969 protocols + 1960s markup language + 1990 browser + 1992 Web server works in 2011

Слайд 63

Architecture is about Alternatives

As you work on other SaaS apps beyond this

Architecture is about Alternatives As you work on other SaaS apps beyond
course, you should find yourself considering different architectural choices and questioning the choices being made.

Слайд 64

Summary: Architecture & Rails

Model-view-controller is a well known architectural pattern for structuring

Summary: Architecture & Rails Model-view-controller is a well known architectural pattern for
apps
Rails codifies SaaS app structure as MVC
Views are Haml w/embedded Ruby code, transformed to HTML when sent to browser
Models are stored in tables of a relational database, accessed using ActiveRecord
Controllers tie views and models together via routes and code in controller methods
Имя файла: SArchitecture.pptx
Количество просмотров: 126
Количество скачиваний: 0