DEV Community

Cover image for Rails Architecture in cloud platform
Sambit Mohanty
Sambit Mohanty

Posted on

Rails Architecture in cloud platform

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

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

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

Thanks for reading!!

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

Top comments (4)

Collapse
 
thorstenhirsch profile image
Thorsten Hirsch

Rack's usage of the term middleware has always been peculiar (not totally wrong, just peculiar):

Between the server and the framework, Rack can be customized to your applications needs using middleware.

So I can understand why you wrote this:

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

But no, you can't. None of these can be used for gluing your web server with your framework and none of these supports ruby applications. They're all based on Java, so perhaps you can fiddle something with JRuby, but that's probably out-of-support at all 3 vendors.

Today the term 'middleware' is commonly used for software that enables communication and management of data in distributed applications. I have copied this definition from wikipedia. And that's also what the products of RedHat, IBM, and Oracle are used for. Well, to be precise: Websphere wasn't even a single product, but a range of products. But IBM doesn't use that brand anymore, their products have been renamed since.

Collapse
 
sambitmohanty954 profile image
Sambit Mohanty

Thanks for pointing the JRuby implementation , it's not like these 3 are not supported , if you go to this and this, then you can find out that we can use Oracle WebLogic, IBM Websphere application server.

But For Red Hat it is not supported at all.

Collapse
 
bizzibody profile image
Ian bradbury

Have you thought about how you would layer security over your architecture?

Where would your firewalls be? Would you break the application up into multiple separated networks? What about load balancing? (I'll stop adding more questions - the list can get very long)

Collapse
 
sambitmohanty954 profile image
Sambit Mohanty

Actually it's just a simple request response cycle , if you talk about adding firewalls and setting protocols. then i will do that in my cloud platform so when a user sent some request then it checks the authorization in cloud platform if the user have authentication then he can go further otherwise not .