Search code examples
ruby-on-railsrubyrack

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


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

The Rails app uses the traditional pair of Unicorn & nginx, and it requires most of the RAM that I have on my VPS.

I've tried adding similar Unicorn configurations for each app and it led me to conclude the RAM is insufficient.

So my question is: is it possible to set up one small memory-saving server which allows me to run several Sinatra apps at once?

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

UPDATE 2: an approach based on sockets shared with Nginx is preferred over the one with ports.

Thanks!


Solution

  • 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!