Shared OTP App/named_PIDs between Umbrella Apps

Hi everyone,

I have one OTP application with named pids in large supervision tree, it will be an App under umbrella project but I want the pids to be accessible by other apps under the same umbrella which they will also have thier own supervision trees.

Keep in mind mostly all Apps will depends on the shared OTP app and some may depends(inter-apps between processors on each other.

What is your best way to access and share OTP app(pids) between all of Apps?

Thanks

PIDs (neither raw nor named) know nothing about application boundaries, so if you know a PID or a name, you can use it from everywhere.

3 Likes

To add some details:

  1. All the PIDs on a node share the same namespace i.e. there is one table containing all the PIDs on the node
  2. All the registered names on a node are in one of two namespaces. There is a local registered names table and a global registered names table. Both namespaces are independent of each other. Global registered namespace is shared among all connected nodes. Local registered namespace is local to the node. By default registered names go into the local namespace.

Messages can be sent to locally registered PIDs by their registered name. e.g.

Process.register(Kernel.self, :hold_out)
send :hold_out, {:this_is_a_message, "hello world"}

Using the registered name is the most common way to share PIDs between Apps. Yes, this does mean apps hardcode the services they are interested in.

Local name registration is the most common way to register a GenServer. The OTP app likely has at least one locally registered process which can be interacted with.

2 Likes

Thank you very much, This is very helpful.
So if two connected nodes running same OTP app who share same local_names, now sending msg across the network will be done by a tuple name {NodeName, LocalPidName}, is this correct? And is there anything extra have to be done in advance?

Thank you NobbZ for your solution. This is exatcly what i needed to hear.

It is actually the non-intuitive {LocalRegisteredName, NodeName} to send the message across the network. Yes, this catches me each time too. As long as the registered name exists in NodeName no additional setup is needed.

Typically we use a slightly different mechanism.

  1. Create a globally registered process (e.g. GenServer) which acts as a name register i.e. NameRegister.
    For resilience this application is marked as a distributed application. In the BEAM world distributed application has a specific meaning.
  2. Services that come online register their service and PID with NameRegister.
  3. Processes query NameRegister for a service and is given a PID.
  4. Processes use the PID to send message to.

We do this because a PID can encode the node it is running on. This negates the need to know which node the process is on. When you send a PID reference to a remote node the PID reference is automatically encoded with the node it is running on. You can then use send pid, msg directly.

2 Likes