Dependency Graphs in Ruby

by | Aug 28, 2020

Or, Making a Sandwich Using Sidekiq

When something goes wrong with your data, Rewind Backups is there to help you restore it back to the way it was. The process for doing this is actually a little more complicated than one might imagine: every platform Rewind integrates with has its own domain model with its own set of rules. In many cases, a certain piece of data depends on other pieces of data existing beforehand, which puts some limitations on what we can restore and when.

For example, in our QuickBooks Online integration, this means we need to restore Accounts before we can restore the Invoices associated with those Accounts. When performing a BigCommerce restore, we need to make sure a Category exists before we can restore a Product that belongs to that Category.

One time, we tried drawing a diagram of all the item types we restored and what other item types they depended on, thinking we would just “figure out” the order and hardcode something in. There were so many lines in the diagram that we could barely read the words on the screen. It became obvious very quickly that we needed something a little more automated and a little easier to use.

A Bit of Math

Let’s start by dipping our toes into graph theory. I promise it will be over soon.

The situation described above, in which Rewind needs to restore certain pieces of data before attempting to restore others, can be modeled as a graph.

This is called a directed graph, because the edge indicates a specific direction with an arrow. It is made obvious that “Bobs depend on Bits”, and not vice versa. Let’s add more nodes to our example:

Now it becomes clearer that not only is our graph directed, it is also acyclic: starting from any node and travelling along the edges in any order, you will never come back to your starting node (remember you can only travel in the correct direction). It wouldn’t make much sense in this context if “Bobs depend on Bits” and “Bits depend on Bobs” were both true.

Directed acyclic graphs (DAGs) have an interesting property: they can be topologically sorted. This sorting algorithm has many applications, such as job scheduling or software dependency resolution. The result of applying a topological sort to a DAG is a list of all nodes, specifying a valid order in which they can be resolved. Note that, depending on the graph, there may be multiple valid ways to order the nodes.

If we were to apply the algorithm to our graph above, we might end up with something like [Restore Bits, Restore Gizmos, Restore Bobs, Restore Trinkets]. By following this list, we know that our entire restore operation will be run in the correct order.

Promise kept?

Introducing Dagwood 🥪

Luckily for us, Ruby’s standard library contains a module for doing the heavy lifting involved in a topological sort: tsort. This is a good starting point, but it doesn’t “just work” on its own. We created Dagwood as a kind of wrapper around tsort to hide some implementation details, improve ease of use and add a couple of features that were important for our use cases. Have a look at Dagwood’s ability to return parallelizable node orderings or generate subgraphs, among others.

We’d love for others to get involved. If you have questions, find a bug, or have an idea on how to make Dagwood better, feel free to reach out to us (or open a PR!).

Back to the Problem at Hand

A customer has requested that their data be restored to a point in the past. There are thousands of items needing to be restored, spanning a dozen different types of items, some of which depend on other types being restored first.

We solved the scaling and reliability issues ages ago: we use Sidekiq to process the restore, one tiny job at a time. Sidekiq also has the concept of Batches, which allow you to run jobs in, well, batches. When a batch is complete, a callback is invoked allowing you to schedule the next set of jobs. In theory, you could hardcode the order in which your operations need to run. Imagine something like this, but with way more workers and callbacks:

class RestoreAllBitsWorker<br /> include Sidekiq::Worker</p>
<p>def perform<br /> bits_batch = Sidekiq::Batch.new<br /> bits_batch.description = 'Restore all Bits'<br /> bits_batch.on(:complete, RestoreBitsDone)</p>
<p># Spawn one worker per Bit to restore<br /> bits_batch.jobs do<br /> Bits.each { |bit| RestoreSingleBitWorker.perform_async(bit) }<br /> end<br /> end<br /> end</p>
<p># Once all the Bits are done, this callback will start a worker<br /> # for restoring all the Bobs.<br /> class RestoreBitsDone<br /> def on_complete(status, options)<br /> RestoreAllBobsWorker.perform_async<br /> end<br /> end

This approach presents some challenges for us:

  • First, it’s ugly and gets complicated fast. Imagine a workflow with even just 5 or 6 different types of items. It would be a nightmare to follow along, let alone make changes to. Now consider that some of the platforms we interact with have over 30 types of items.
  • Second, it’s not platform-agnostic. We would need a separate set of workers and callbacks for every one of the platforms for which we offer restore capabilities. Something a little more reusable would be nice.
  • Third, Rewind offers a lot of flexibility to the customer requesting a restore. They may decide to only restore certain item types instead of restoring the entirety of their data. It would be difficult to capture the dynamic nature of these kinds of requests using a static Sidekiq workflow.


Our solution to this problem uses a combination of Sidekiq batches and Dagwood’s ability to generate the correct order of operations for a given dependency graph.

Let’s start by defining all the dependencies between items, and creating a Dagwood::DependencyGraph.

DEPENDENCY_GRAPH = Dagwood::DependencyGraph.new(<br /> bits: [],<br /> bobs: %i[bits],<br /> gizmos: %i[bits],<br /> trinkets: %i[gizmos]<br /> )<br /> 

We can then feed that graph into our “initiate” worker, which is responsible for kicking off the restore operation as a whole. This worker is also responsible for creating the batch under which all other work will be done. I won’t define RestoreAllItemsByTypeWorker in this article, but let’s pretend that’s where the magic of actually restoring all the data of a single type (e.g “Trinkets”) happens.

class InitiateRestoreWorker<br /> include Sidekiq::Worker</p>
<p>def perform(dependency_graph)<br /> # This will be an array like [:bits, :bobs, :gizmos, :trinkets]<br /> order_of_operations = dependency_graph.order</p>
<p># This is our top-level batch, containing ALL the jobs for this restore.<br /> # This batch only completes once every single item has been restored.<br /> batch = Sidekiq::Batch.new<br /> batch.description = 'Restore all items'<br /> batch.on(:complete, RestoreCompleteCallback)</p>
<p>batch.jobs do<br /> OrderedWorker.perform_async(<br /> order_of_operations,<br /> RestoreAllItemsByTypeWorker<br /> )<br /> end<br /> end<br /> end

The InitiateWorker worker above didn’t actually do much, outside of setting up. The actual work starts when we pass the ordered list of restore types into our next worker:

<br /> class OrderedWorker<br /> include Sidekiq::Worker</p>
<p>def perform(work_to_perform, worker_class)<br /> return if work_to_perform.empty?</p>
<p>current_work, *remaining_work = work_to_perform</p>
<p># This work is done within the top-level batch we<br /> # created in the InitiateWorker<br /> batch.jobs do<br /> # Create a child batch for the current work to run in.<br /> # When it is done, pass along the remaining work to the callback.<br /> child_batch = Sidekiq::Batch.new<br /> child_batch.description = "Batch for #{current_work}"<br /> child_batch.on(<br /> :complete,<br /> OrderedCallback,<br /> 'remaining_work' =&gt; remaining_work<br /> 'worker_class' =&gt; worker_class<br /> )</p>
<p>child_batch.jobs do<br /> worker_class.perform_async(current_work)<br /> end<br /> end<br /> end<br /> end<br /> 

You may have noticed that the OrderedWorker pops the front of the work_to_perform array and only ends up spawning a single restore worker for a single type. When that type is completely done restoring, we will invoke our OrderedCallback with the remaining types we haven’t restored yet… which will re-invoke the OrderedWorker, until all work has been completed.

<br /> class OrderedCallback<br /> def on_complete(status, options)<br /> if options['remaining_work'].present?<br /> Sidekiq::Batch.new(status.parent_bid).jobs do<br /> options['worker_class'].perform_async(<br /> options['remaining_work'],<br /> options['worker_class']<br /> )<br /> end<br /> end<br /> end<br /> end<br /> 

All of the above code is simplified, but hopefully the general idea is clear enough: use the dependency graph to organize the work. Perform the first “piece” of work. When it completes, pass the remaining work along to the callback, which will re-enqueue the worker. Repeat until all work is complete, at which point the top-level batch will complete. Done!

At this point we have a functional solution that is reusable and dynamic, but it’s not optimized for speed. The solution above will restore every type of item serially, always waiting on the previous operation to complete before moving on to the next. If you take another look at the graph near the beginning of this article, you may realize that it would be entirely possible to do some of the work in parallel. For example, Restore Gizmos and Restore Bobs both depend on Restore Bits, but they don’t depend on each other, indicating they could be done at the same time. This is why Dagwood provides the ability to return node orders that work in parallel. All we need to do is slightly modify the example above.

# We change our call to `.order` with a call to `.parallel_order`<br /> class InitiateRestoreWorker<br /> ...<br /> def perform(dependency_graph)<br /> # This will be an array like [[:bits], [:bobs, :gizmos], [:trinkets]]<br /> # Every group in the array represents work that can be done in parallel<br /> order_of_operations = dependency_graph.parallel_order<br /> ...<br /> end<br /> end</p>
<p># The only change we need to make here is to loop over<br /> # current_work, which is now an Array.<br /> class OrderedWorker<br /> ...<br /> child_batch.jobs do<br /> current_work.each { |work| worker_class.perform_async(work) }<br /> end<br /> ...<br /> end<br /> 

And we’re done! We’ve used Sidekiq to run through a sequence of dynamically generated work, ensuring that we’re doing it in the correct order and in the quickest way possible.


Interested in solving problems like this? Want to get paid at the same time?? Check out Rewind’s open positions to start your career in DevOps, Security, Engineering, and more 


Marc Morinville
Marc Morinville
Marc Morinville is a software engineer who enjoys solving problems just as much as he likes reading, writing, and teaching others about them. He spends most of his free time worrying about how he’s going to spend time on all his hobbies, rather than partaking in any of them.