Scaling: how to handle communication between Ruby on Rails applications?

I am running Ruby on Rails 3, and I have an application that uses namespaces to handle more "internal concepts." With “internal concepts,” I mean that every namespace is used to process a specific resource of my application. For example, the namespace is “users” and it is used to process user sessions and authorization, the other is “blogs” and it is used to process all posts and comments.

I think this is a “convenient” solution to avoid a lot of problems, but not the best.

My RoR application currently consists of this file system structure:

# "users" and "blogs" are namespaces RAILS_ROOT/app/controllers/users RAILS_ROOT/app/controllers/blogs RAILS_ROOT/app/models/users RAILS_ROOT/app/models/blogs RAILS_ROOT/app/views/users RAILS_ROOT/app/views/blogs ... 

I would like to switch the namespace “users” and “blogs” in the two RoR applications using subdomains to have something like this:

 http://main.com # This is the main RoR application http://users.main.com # This is another RoR application used to handle users http://blogs.main.com # This is another RoR application used to handle blogs 

In a few words, I think I'm trying to scale Out * my application or maybe create a Web service for each RoR application, but my problems are:

1. What problems can I encounter?

I noticed problems with session support (in my case I process cookies) between applications, but I think this is not the only problem.

2. How to handle the communication between the three RoR applications in my case?

I noticed that I can use ActiveResource to exchange information, but I have to pay attention to information such as user authentication.

  • Should I implement the OpenID / Oauth protocol to support user authentication?

  • I think that I should provide authentication of the user using an HTTPS connection, if there is a connection between subdomains. It's true?

3. How do I organize work and resources?

With all that said , I would not use (absolutely) plugins or gems, but if I need to, I would like to implement my own strong> handler .

In the end, I would like to have 3 RoRs of “simple” and separate applications without using namespaces in each of them and which can communicate with each other:

 # "Main" application for http://main.com ROOT_MAIN/app/controllers/ ROOT_MAIN/app/models/ ROOT_MAIN/app/views/users ... # "Users" application for http://users.main.com ROOT_USERS/app/controllers/ ROOT_USERS/app/models/ ROOT_USERS/app/views/users ... # "Blogs" application for http://blogs.main.com ROOT_BLOGS/app/controllers/ ROOT_BLOGS/app/models/ ROOT_BLOGS/app/views/users ... 

By the way: a good approach to using namespaces that I do?

PS: If you need other information, let me know and I will update the question.


* From the O2 Software Process : “Scale Out” refers to the concept of adding more servers to an existing fleet, as opposed to “Scale Out,” which means replacing existing (slow) servers with new (and faster) servers.

+4
source share
3 answers

Is your application really so large that you need to use several applications to solve various problems? It may be that at your post there are simply not enough details to convey the real value of what you are doing, but it looks like you are trying to make the modular application small enough to be good if it weren't for “scaling” as you say. Or maybe I just missed something?

  • I think this will be a difficult problem, but there may be some way to store session data in the database and either share it the way you deal with No. 2, or you will have to download your own solution for that. I think the biggest problem will be the sharing of resources in your application, and also, if you disrupt user management in your own application, you will need to implement your own OpenID / Oauth. This post describes this using Devise / OAuth.

  • You can use activeresource to connect to each application a priori. This post describes one solution for sharing data through rails applications.

  • This question is somewhat vague. You described the use of several applications to separate your problems (blogging and user management), so I assume that you will have your resources at the root of each application without any namespace, as you already did in your existing application.

Now for a more general answer to your entire question, I recently read a blog post about data, context, and interaction (Wikipedia article) on Rails, and I think this might be the best solution for what you are trying to accomplish if you think that your application is getting out of hand.

+1
source

Your problem is much simpler than you think. It all depends on how you handle your routes.

Ruby On Rails 3 has better support for subdomains. Thus, you do not need to divide them into three or more RoR applications. You can put all your code in one RoR application. And redirect user.abc.com to any controller, for example, "users / sessions", redirect blog.abc.com to "blogs / blogs". namespaces are convenient in applications like yours, where they make your work very fast in order to separate contextually different parts of your application into different folders and route formats.

Try the namespace in your heart, I believe that you will not get any errors that you imagine right now. I suggest you write a code for it and come here if you encounter problems.

+2
source

I apologize for this. In fact, if you want to scale the rails application, you do not need to create different applications for each unit (I mean, when you try to separate users and blogs here), you take a step forward in the process of scaling your application, you must first put all the individual blocks as mounted motors and require them as a gem in your main application and mount them in the main application routes. As in your case, blogs can be moved to a separate mounted engine. If you need to scale more in the future, you can move on to use the engines as a standalone application. Here is a link to a video that can give you an idea of ​​what I'm trying to explain here https://www.youtube.com/watch?v=pm94BsoMGik

0
source

Source: https://habr.com/ru/post/1339481/


All Articles