New Discussion

Notifications

You’re not receiving notifications from this thread.

Using Parallel gem to achieve parallel processing in Ruby for increasing performance and making Rails Application faster.

1
Ruby

Hi everyone, I'm trying to decrease API latency in our largely synchronous Ruby on Rails backend. While we use Sidekiq/Shoryuken for background jobs, the bottleneck is within the request-response cycle itself, and significant code dependencies make standard concurrency approaches difficult. I'm exploring parallelism to speed things up within the request and am leaning towards using the parallel gem (after also considering ractors and concurrent-ruby) due to its apparent ease of use. I'm looking for guidance on how best to structure code (e.g., in controllers or service objects) to leverage the parallel gem effectively during a request, what best practices to follow regarding database connections and resource management in this context, and how to safely avoid race conditions or manage shared state when running parallel tasks for the same flow (e.g for array of elements running the same function parallely and storing the response). Beyond this specific gem, I'd also appreciate any general advice or common techniques you recommend for improving overall Rails application performance and speed.

You need to use includes and select wisely. Avoid N+1 queries with includes and only load the fields you need with select. Especially for things like configuration lookups, permissions or repeated calculations during a request. Use background tasks to pre-process and cache expensive data (e.g. use Redis or rendered views). If you are waiting for multiple external calls, use Parallel or concurrent-ruby to execute them in parallel during the request cycle.

Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 90,109+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.