A passionate full stack software engineer having 7 years of experience in building scalable enterprise systems. I have worked predominantly with the JVM and Javascript ecosystem but currently exploring Rails. For me, software engineering is all about solving problems in the most optimal way. I am an avid practitioner of clean code practices and love to read about System Design and Architectures. When I am not coding I love to play playstation, read anything about aviation or go out for a long drive and of-course I am foodie by heart.
3 minute read
Rails 6
had added the :destroy_async option to delete the associated records in a background job whenever the foreign_key constraint would be disabled.
But what if the associated records are too large?
In this case,
by default, the single background job will delete all of them
and that can be time-consuming.
To handle this,
and make it fast,
Rails now allows adding the active record configuration to specify the maximum number of records
that will be destroyed in a single background job
and enqueue additional jobs
when the number of records exceeds that limit.
Example:
Suppose we have a Product model
and a Review model.
Notice that we have added the :destroy_async option to delete review records in a background job.
Before
As it can be seen all the reviews get deleted in a single async job with ID 880513a7-f6c4-4a35-8d33-6d69a737031e.
After
Now, we can add the configuration to specify the maximum number of records that will be destroyed in a single background job.
Now, if we try to delete a Product with 50 reviews,
Rails will enqueue 5 DestroyAssociationAsyncJobs to delete all of them.
Note: The enhancement is yet to be released in the official Rails version