How can I serve requests concurrently with Rails 4? How can I serve requests concurrently with Rails 4? ruby ruby

How can I serve requests concurrently with Rails 4?


I invite you to read about the configuration options of config.threadsafe! in this article Removing config.threadsafe!It will help you to understand better the options of config.threadsafe!, in particular to allow concurrency.

In Rails 4 config.threadsafe! is set by default.


Now to the answer

In Rails 4 requests are wrapped around a Mutex by the Rack::Lock middleware in DEV environments by default.

If you want to enable concurrency you can set config.allow_concurrency=true. This will disable the Rack::Lock middleware. I would not delete it as mentioned in another answer to your question; that looks like a hack to me.

Note: If you have config.cache_classes=true then an assignment to config.allow_concurrency (Rack::Lock request mutex) won't take effect, concurrent requests are allowed by default. If you have config.cache_classes=false, then you can set config.allow_concurrency to either true or false. In DEV environment you would want to have it like this

config.cache_classes=falseconfig.allow_concurrency=true

The statement: Which means that if config.cache_classes = false (which it is by default in dev env) we can't have concurrent requests. is not correct.

Appendix

You can refer to this answer, which sets up an experiment testing concurrency using MRI and JRuby. The results are surprising. MRI was faster than JRuby.

The experiment with MRI concurrency is on GitHub.The experiment only tests concurrent request. There are no race conditions in the controller. However, I think it is not too difficult to implement example from the article above to test race conditions in a controller.


It seems that by default, in Rails 4, concurrent requests are not enabled in the Development environment.

I found this quote in the documentation.

Rack::Lock wraps the app in mutex so it can only be called by a single thread at a time. Only enabled when config.cache_classes is false.

Which means that if config.cache_classes = false (which it is by default in dev env) we can't have concurrent requests.

Concurrent requests do work with my example in production environment.

One solution is to set config.cache_classes = true in the development environment, but then the code doesn't reload on a change, which doesn't really work for development.

The second kind of hacky solution is to disable the Rack::Lock middleware in development.

So if you were to add in development.rb the following line:

config.middleware.delete Rack::Lock

you should be able to have concurrent requests in development environment with cache classes disabled.


you can try unicorn this is very simple in development mode:

http://dave.is/unicorn.html