Load balancing and hook extensions

Hi, is there any documentation on how Directus is handling hooks in a load balanced environment?

Let’s say I create a hook extension that reacts on the creation of a new item in collection A by creating another item in collection B. Let’s also say I have two instances running in a load balancing setup.

  • are the hooks triggered on both instances?
  • would each instance now create a new item in B?
  • if not, how are the hooks synced between instances?

Cheers
Michael

Good question!

The way it works is very simple:
Every Directus instance is its own webserver and has its own capabilities to run your hook.
(Assuming that you have added the hook to all instances)
Directus will only run the hook(s) on the instance which handled the request.
Therefore there is only one hook being fired and everything will work as expected. (No duplicated hook executions ect)

For CRON (flows or extensions) Directus relies on Redis to make sure that it is only triggered one time.

Hi Michael,

I believe what you’re looking for might not be solved just by understanding how hooks execute in a load-balanced environment, but rather by reviewing the design of the process that uses them.

From my point of view, it seems like you expect an event (for example, creating an item in collection A) to directly imply a synchronized result across other instances. But in reality, the event should be the trigger for a process — not the entire process itself.

Think of the event as a spark that activates a sequence:

  1. The event happens on one instance (the creation of the item in A).
  2. That instance, or an orchestration system, starts a process that takes care of creating the new item in collection B.
  3. If that item needs to be reflected or distributed to other instances or environments, that is a separate process: synchronization, distribution, updating.

In this sense, the hook shouldn’t handle everything, nor should you expect all instances to react to the same event as if they were one mind. If you need to keep data synchronized across multiple Directus instances, the ideal is to implement well-defined processes for replication or synchronization, not rely on distributed hooks to do this automatically.

In summary:

:white_check_mark: The event can trigger a process.
:white_check_mark: That process can include synchronization, distribution, validation, etc.
:cross_mark: But don’t expect the event itself to contain all that logic or magically propagate to all your instances.

Perhaps what you need is not a solution about how Directus handles hooks, but clearly defining the processes between your instances and ensuring each has a clear role: one receives, another transforms, another distributes, and so on.

Best regards.