Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run multiple tiny Ruby (Rack) apps on one server?

I want to run several (>2) tiny Rack-based apps on my VPS server, which already has one large Rails app running.

Rails app uses traditional pair "Unicorn + nginx" and it requires the most of RAM resources I have on my VPS machine.

I've tried adding similar Unicorn configurations for each app and it led me to the insufficiency of RAM resources.

So my question is: is it possible to setup one small tricky memory-saving server which will allow running all Sinatra apps at once?

UPDATE: in case if it matters, I don't care much about the performance. Those apps are not intended to do any serious jobs.

UPDATE2: an approach based on socket shared with Nginx is preferred over the one with ports.

Thanks!

like image 881
Stanislav Pankevich Avatar asked Aug 25 '12 21:08

Stanislav Pankevich


1 Answers

I did my own exploration of this question and I think I found a solution which will allow me having one web-server running all my tiny apps at once.

It is based on RackStack not-yet-a-gem created by Remi Taylor (@remi on Github) https://github.com/remi/rack-stack.

RackStack is inspired by Rack::Builder, which as well seems being good for accomplishing a task like this - RackStack just goes in the same direction further, abstracting "stack" functionality in a way I found very nice and handy.

Here is a demonstration of RackStack which consists of two sample apps (Sinatra and Rack): https://github.com/stanislaw/skeletons/tree/master/rack_stack. To mimic stack app behavior on a real server I modified my /etc/hosts file to have localhost2 host pointing to 127.0.0.1.

I fire up Thin server and then run requests on localhost or localhost2: the requests to 'localhost' are served by FirstApp, to 'localhost2' by SecondApp.

I can't now foresee any problems that can appear, when I will test my apps on a real server, but now this approach seems to be exactly what I was looking for: I imagine, that on a real server Nginx will pass requests to all the hosts associated with my rack apps to a socket listened by Thin server. So, RackStack will meet only those requests which are addressed to the apps I have in my stack.

Any suggestions, improvements of this scheme or alternatives are still appreciated!

like image 101
Stanislav Pankevich Avatar answered Sep 20 '22 04:09

Stanislav Pankevich