Rails server Multi threading

Whats the issue?: There are many Rails servers that are multi threaded. Lets take the example of Puma, it uses threads pool. The default thread count is 16.  This means the server can server 16 requests concurrently.

This is sufficient for normal applications. But if you are integrating with a third party services which is going to take long time for response you have the risk of taking the site down if all the threads are used at the same time. Take a look at the following picture.

Screen Shot 2018-05-23 at 7.15.30 AM

If a hacker tries to hit the site with multiple(more than the thread count) concurrent 3rd party api requests the site goes down when once the threads gets exhausted.

Whats the solution:

We can fix this by request pooling which is not a recommended solution. I recommend websockets is way to go. They are light weight(as they dont carry more information in request headers) and can have a two way communication once connection is established.

It doesn’t exhaust you thread pool.

There are many ways to implement web sockets. Check this post which has a detailed explanation of different ways.

I personally have burnt my hands trying to use ActionCable for Rails + F/E framework  app. You can find it here.

Having said all this i found another solution Pusher . After getting mad at ActionCable i found Pusher to the rescue. I recommend pusher for following reasons.

  • Maintenance: As we communicate through Pusher api we dont need to worry about websocket server. No need to worry about socket connection errors.
  • Ease of Integration: Pusher has api to integrate in almost all the languages. It takes less than 5 mins to use it when you signup.
  • Usage: Their free account gives us adequate amount of messages per day. Good for us to try it.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.