Code Concurrency and Two Easy Fixes
Tags: concurrency, rails, ruby, webdev
Published: 2021-10-12 07:08:07

---
title: Code Concurrency and Two Easy Fixes
published: true
description: Not sure how to manage code concurrency in your rails code? We help you manage code concurrency through two easy fixes using Executors and Reloaders.
tags: concurrency, rails, ruby, webdev
canonical_url: https://blog.engineyard.com/code-concurrency-and-fixes?utm_source=dev.to&utm_medium=Devgraph-page&utm_campaign=canonical_url&utm_id=QiWorks.in
//cover_image: https://blog.engineyard.com/hubfs/Blog_Code%20and%20Concurrency%20%281%29.jpg
---
![[Code Concurrency and Two Easy Fixes]](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/avj8tkm86cl30ftck647.jpg)

By [Ritu Chaturvedi](https://blog.engineyard.com/author/ritu-chaturvedi)

Code concurrency is a default in any Rails code. A [threaded web server](https://www.geeksforgeeks.org/multithreaded-servers-in-java/) will simultaneously serve many HTTP requests, and each will hold its controller instance. Threaded active job adapters and action cable channels also handle multiple requests simultaneously. Even when the global process space is shared, work for each instance is managed separately. For scenarios where the shared space is not altered, this process will run smoothly.

Let us see in detail how this is managed in our Rails applications.

1. **Executor: Executors separate the framework and application codes by wrapping the application code.**

The wrapping of code was more complex before Rails 5.0. To protect a code, either separate Rack middleware classes were used or direct wrapping was used. With recent updates in Rails, the executor handles the wrapping in a single step that is easier to understand.

Call the executor to wrap the application code as you invoke it.

For example:

![Code concurrency Executor example](https://blog.engineyard.com/hs-fs/hubfs/image1-2.png?width=625&name=image1-2.png)

One attractive feature of the executor is its reentrancy.

Two callbacks of the executor are '_to run'_ and '_to complete_.' This enables wrapping code in parts when it is not possible to wrap the code as blocks.

![code concurrency fix example](https://blog.engineyard.com/hs-fs/hubfs/image2-2.png?width=629&name=image2-2.png)

The current thread is moved to the '_running'_ mode, temporarily blocking the thread. So, the thread will not be accessible by any other request that tries to do so.

1. **Reloader: The Reloader functions similarly to the Executor. The code that is to be protected is wrapped before another request is hit. This is used in scenarios where application code is invoked multiple times by any long-running process. Most often, in Rails, the web requests and Active Jobs are by default wrapped. So the Reloader is rarely used.**

A Reloader is employed when there is a need to reload the application. When the requested condition asks for reloading, the Reloader delays the application reload until it is secure. And for scenarios where application reloads are mandated, the Reloader waits until the current block is executed and allows the reload. Thus, the code is protected from errors.

The '`to_run`' and '`to_complete`' callbacks are used by the Reloader also.

A _class unload_ is involved in the Reloader process. Here, all of the auto-loaded classes are removed and set ready to be further loaded. The application reload is to happen only before or after the class unload, and so, the two additional callbacks of Reloader are '`before_class_unload`' and '_`after_class_unload`'_.

[Executor and Reloaders](https://guides.rubyonrails.org/threading_and_code_execution.html) are used by the Rails framework components as well. `ActionDispatch::Executor` and `ActionDispatch::Reloader `are included in the default application stack. Whenever there is a code change, the Reloader serves a fresh copy of the application for an HTTP request. Active Job feature also utilizes Reloaders, whereas Action Cable uses Executor. Action Cable also uses the before\_class\_unload of Reloader to disconnect all the connections.

As discussed, code concurrency is handled by default with the threaded active jobs and action cables features of Rails codes. With [recent Rails updates](https://weblog.rubyonrails.org/releases/), Executors and Reloaders are also added to improve the code concurrency handling.

Also, check out [7 Ruby Gems to keep in your toolbox](https://blog.engineyard.com/7-ruby-gems-to-keep-in-your-toolbox).