Separated threads according to URL path

58 Views Asked by At

I'm using Puma and Nginx to run my Rubinius app.

I would like to separate my URL requests.

The first one would be for API requests, the second one for other requests.

I think Puma already makes threading but I want to be sure that web requests won't block a thread what leads to a stop during my API requests. I suppose that if a thread is busy, Puma will create another one but I want to be sure that one is always available for API requests.

My main point here is to "save" a thread for URL requests which are what my users need the most.

2

There are 2 best solutions below

3
blelump On

As Puma consider each request within separated thread, the only bottleneck here is database access by such threads. Besides that, you cannot guarantee some threads are 'better' than others.

One of possible solutions worth noting here is to deal with it using nginx. Let's say you app is serving content on http://some_host.com and API is available within http://some_host.com/api. You can configure your nginx to handle requests for http://some_host.com and http://some_host.com/api separately. In this case you'll need two separate instances of Puma server. One for base app and one for api request. What I mean here is when a request comes to http://some_host.com, it is handled by Puma A and when to http://some_host.com/api/..., by Puma B.

Just remember one thing, you can handle requests by separated instances, but you still have only one database, unless you're caching content. Here comes another question. Do you cache your content? If not, wouldn't it be greater idea to start with caching first?

0
Anatoly On

Why not to split main application and API? It's easy to serve two distinct applications with Nginx:

  location / {
    proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header  X-Forwarded-Proto $scheme;
    proxy_set_header  Host $http_host;
    proxy_redirect    off;
    proxy_pass        http://puma1;
  }


  location /api/ {
    proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header  X-Forwarded-Proto $scheme;
    proxy_set_header  Host $http_host;
    proxy_redirect    off;
    proxy_pass        http://puma2/;
  }

Please pay an attention on trailing slash for the second location proxy_pass, it helps to rewrite requests and omit '/api' prefix.