Rails Architecture in cloud platform
Tags: rails, ruby
Published: 2020-03-11 04:52:28

Thank you all for your immense responses, It really encouraged me to move forward to write the articles like this.

   So when i started to learn about rails, i wanted to create my own website and host it in Cloud platforms like(AWS/Azure/Google cloud platform) and for that i start doing R&D in internet.

Here we’ll understand the basics of how the web, Cloud platform, the routes, and the MVC architecture work, using Ruby on Rails web framework. Let’s dive into the web world!

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/3iwojl37q0miz40jjoae.jpg)

This one request cycle must be complete within 30 sec otherwise you will get an error like "Error Code 502 (Bad Gateway error) [Whoops, Server is taking too much time to respond]"

Mainly this architecture focuses on 4 parts
> 1. Web Server
>2. Middleware
>3. MVC Structure
>4. DB Server

##1. Web Server

Basically web server is:- 
>A web server is a program that takes a request to your website from a user and does some processing on it. Then, it might give the request to your Rails app.

So if you are working on personal project then the best web server is (Puma/Unicorn/Thin) but if you are a start-up company and want to build a enterprise level web app so the best Web server is Nginx and Puma as app server.

**bonus:- You Can use ELB(Elastic Load Balancer) in AWS instead of Nginx.**

I know it's very confusing right now and you guys have lot of questions in your head. I also had so many questions when i am starting R&D. so let's break down the questions one by one.

####a.  So the question is why are we using Nginx in Production???

Nginx is a general webserver, it handles a request & if there is a matching file for that request it will return that file.Having nginx as a web server will help you in handling multiple requests much more efficiently. Being a multi threaded server it will distribute requests into multiple threads making your application more faster.

####b. Why we use nginx with puma ???

Whenever there's a request coming from a client, it will be received by the nginx and then it will be forwarded to the application server which is Puma over there beacause Nginx doesn’t know anything about Rack, or Ruby.That’s why we need Puma or any other Rack-compatible web server.

####c. In Development why can't we use Webrick instead of puma???

So the answer is we can use but, by default Webrick is single threaded, single process. This means that if two requests come in at the same time, the second must wait for the first to finish there our app getting slow so to tackle this situation in Rails 5 we are using Puma which is multi threaded and can process the multiple requests concurrently, which usually results in less memory usage.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/eu8w7ndv3l6x06jw9ztn.png)

So these are the examples of Web server, app server and frameworks used when you are creating an Ruby on Rails Web applications.

##2. Middleware

In Rails by default the middle ware is "Rack".

>Rack provides a minimal, modular, and adaptable interface for developing web applications in Ruby. By wrapping HTTP requests and responses in the simplest way possible, it unifies and distills the API for web servers, web frameworks, and software in between (the so-called middleware) into a single method call.

**Single Method Call**
A Rack application is a Ruby object (not a class) that responds to call. It takes exactly one argument, the environment and returns an Array of exactly three values: status, headers, and body.

You can write your own custom middleware or you can use 3rd party middleware services such as.
>Red Hat JBoss Enterprise Application Platform (EAP)
>IBM WebSphere
>Oracle WebLogic

##3. MVC Structure

So if you are reading this then i assume that you are familiar with this but to summarise this , i will just brief what MVC is???

So MVC stands for (Model view controller)

**Model:-**
>"Maintains the relationship between Object and Database and handles validation, association, transactions"

**View:-**
>"A presentation of data in a particular format, triggered by a controller’s decision to present the data."

**Controller:-**
>"The facility within the application that directs traffic, on the one hand querying the models for specific data, and on the other hand organising that data (searching, sorting) into a form that fits the needs of a given view.

##4. DB Server

>A database server is a server which uses a database application that provides database services to other computer programs or to computers, as defined by the client–server model

source:- [Wikipedia](https://en.wikipedia.org/wiki/Database_server)



###Thanks for reading!!


####If you have any questions or suggestions regarding this article, feel free to comment down below. ^_^