Ask A Question

Notifications

You’re not receiving notifications from this thread.

Help with concurrency

Matheus Cumpian asked in Ruby

Hi everyone, how’s it going? I’m a Go developer transitioning to Ruby due to a job change. Typically, I solve various problems in web apps using Go’s lightweight threads (goroutines). For example, if I have two operations that involve I/O, I usually launch them in goroutines and wait for their responses. In short, there are countless situations where I use concurrency to tackle different types of problems. I’ve been studying a bit and saw that there are several ways to handle concurrency in Ruby, including a well-known library. However, I would like to know if there’s a “typical” way to handle concurrency within Rails. How do you usually deal with this?

Reply

In Rails, concurrency is often handled through Active Job for background tasks, leveraging tools like Sidekiq for multi-threaded job processing. For simpler cases, you can use Ruby's Thread or Fiber, though it's less common due to Ruby's Global Interpreter Lock (GIL). Rails 7 introduces support for asynchronous queries with ActiveRecord::FutureResult. For external I/O, libraries like Concurrent-Ruby or Async can help manage concurrency effectively. Typically, Rails emphasizes simplicity and offloading intensive tasks to background jobs rather than using goroutine-style patterns directly.

Reply

Answering my own question now that I've been working for a month or two with Rails.

Rails folks usually rely on background jobs to work with concurrency. If you need to send a couple of HTTP requests that can be done in parallel, no problem, you can use the asynchronous functionalities of Ruby 3+ to deal with it. Otherwise, anything compute intensive or that will deal with heavy I/O you'll send it to Sidekiq or something like that to be processed in workers, because you don't want to starve the threads of your main puma server with these kind of load.

Hope newcomers from other languages can benefit from this learning.

Reply
Join the discussion
Create an account Log in

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

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

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