2

I am working on uber like/cab booking app. I am using Action Cable for this purpose. After creation of new order server get list of 10 nearest drivers and send each in turn order details (with a pause of 40 seconds).

Thread.new do
  nearest_drivers.each do |id|
    order_data_for_driver = { ... }
    ActionCable.server.broadcast("driver_#{id}", order_data_for_driver)

    sleep 40

    Thread.exit if order.reload.canceled_by_user || order.trip
  end

  cancel_data = {canceled_by_timeout: true }
  ActionCable.server.broadcast("order_#{order.id}", cancel_data )
end

Is there a limit to the number of threads that rails in production mode can run at the same time? For example, if 100 users will create new orders. What more elegant solution can be used?

ViT-Vetal-
  • 2,431
  • 3
  • 19
  • 35
  • 1
    “What more elegant solution can be used?”—the language with native concurrency support, like [tag:erlang] or even [tag:golang]. – Aleksei Matiushkin Jan 06 '19 at 16:50
  • have you considered sidekiq gem? you can change the number of processes and threads just through simple configurations. however, your code needs to be aync compatible. https://github.com/mperham/sidekiq/ – Oshan Wisumperuma Jan 07 '19 at 09:10
  • Try using https://github.com/tidwall/tile38 to get nearest one. @ViT-Vetal- Could you please share your github repo ? I am curious to see how it works. – Kiran Patil Mar 24 '21 at 13:30

1 Answers1

1

Usually this kind of tasks is referred as having back pressure. Maximum amount of threads on UNIX systems may vary from circa 10K to max allowed 500K.

The most common way to handle back pressure is to plug in a fast queue in between (like RabbitMQ or something) and increase the amount of queue consumers as the load of requests to proceed increases.

100/s is nothing, but if you plan to handle thousands of concurrent connections, I strongly encourage to rethink the language of choice twice. Rails is not a software created for this kind of task. Neither is Ruby.

Aleksei Matiushkin
  • 119,336
  • 10
  • 100
  • 160