Friday, April 13, 2012

Saas Course: Architecture of SaaS Applications (Chapter Two)


SaaS Architectures and patterns

Architectures and different patterns has been introduced in Unit2 at multiple granularity. The bookware uses altitude as an analogy to represent the levels of SaaS Architectures, but I have chosen a different metaphor (mileage) for simplicity. I hope it will be not annoying. The 3-tier architecture is predefined because of the necessity of horizontally scalable system, but the applied pattern, the MVC (Model-View-Controller), for web application is only one possible solution to separate responsibilities, isolate the user interface components from the business logic. It is very ideal for view-centric web applications, but there are also other possibilities such as 

  • Template View pattern for web applications with mostly static content (used by PHP and Django) 
  • Page Controller pattern can be used for a small number of distinct pages by giving each page its own controller (used by Sinatra)
  • Front Controller can handle all incoming request to generate a collection of views (used by j2ee servlets)
Rails is a very scalable framework by considering the presentation- and logic-tier, but not the persistence-tier based on traditional relational databases. The persistence storage is a critical point of a shared-nothing architecture so therefore relational and non-relational databases (NoSQL) are combined for better scalability.


Download the Full-Size Visual Map for Free

Creative Commons License

SaaS Course (Architecture of SaaS Applications) Visual Map by Csaba Thamm is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.
Based on a work at www.coursera.org/course/saas.

In the 3-tier shared-nothing architecture the components in a tier do not communicate with each other allows adding further computers to each tier independently. The statelessness of HTTP allows the presentation and logic tiers to be shared-nothing, since all requests are independent, any server in the presentation or logic tier can be assigned to any request.


Routes

We can get a list of defined routes if we type 

> rake routes

      movies GET    /movies(.:format)          movies#index
           POST   /movies(.:format)          movies#create
 new_movie GET    /movies/new(.:format)      movies#new
edit_movie GET    /movies/:id/edit(.:format) movies#edit
     movie GET    /movies/:id(.:format)      movies#show
           PUT    /movies/:id(.:format)      movies#update
           DELETE /movies/:id(.:format)      movies#destroy

The :id token represents the primary key (ID) of a model instance and the optional (.:format) token allows a route to request resources in an output format (for example GET /movies.xml requests an XML document).
In a Web app, two interactions are required to create or update a movie, because before we can submit a form, a form must be presented to fill it up. Routes connect URIs to specific controller actions. When an URI matching a defined route is received, the correspondent controller action will be triggered.

RESTful Rails

REST stands for REpresentational State Transfer. REST is an architectural style. Using REST it's not required to use HTTP protocol, but most of the time it will be used, because it is simple and HTTP has four basic operation (GET, PUT, POST, DELETE) to make CRUD operations. By using PUT the server get data for overwriting resources. If you execute PUT more times, your resource will be always replaced. So PUT can be used to update resources.
By using POST more times, the server will create always new resources.
Services and APIs follow this principle are RESTful.

Resource manipulation in Rails:

Operation
SQL
REST
create
insert
POST
read
select
GET
update
update
PUT
delete
delete
DELETE

A RESTful interface simplifies participating in a SOA architecture, because every request is self-contained so interactions between services don't need to establish an ongoing session.

Concrete example with operations on an e-commerce site:

Operation
non-RESTful
RESTful
login to site:
POST /login/csaba
POST /login/csaba
Welcome page:
GET /welcome
GET /user/301/welcome
Add item ID 254 t cart:
POST /add/254
POST /user/301/add/254
View cart:
GET /cart
GET /user/301/cart
Pay:
POST /checkout
POST /user/301/checkout


Template Views

Rails provides a built-in "PHP-like" templating system, it calls erb (Embedded Ruby), but it is highly recommended to use rather HAML (HTML Abstraction Markup Language), a beautiful lightweight markup language to simplify the generation of views. 

Template with ERB:










in HAML:



No comments:

Post a Comment