Home > English, Erlang > Process group in erlang: some thoughts about the pg module

Process group in erlang: some thoughts about the pg module


One of the most common ways to achieve fault tolerance in distributed systems, consists in organizing several identical processes into a group, that can be accessed by a common name. The key concept here is that whenever a message is sent to the group, all members of the group receive it. This is a really nice feature, since if one process in the group fails, some other process can take over for it and handle the message, doing all the operations required.

Process groups allow also abstraction: when we send a message to a group, we don’t need to know who are the members and where they are. In fact process groups are all but static. Any process can join an existing group or leave one at runtime, moreover a process can be part of more groups at the same time.

Since this blog is about Erlang I will not go further into the general topic, which can be explored online or by reading the well known book by Prof. Andrew Tanenbaum‘s “Distributed Systems: Principles and Paradigms” which is bloody expensive but worths its price.

Instead, I will introduce you one of the Erlang modules which implements process groups: the module pg. This module, which is anyhow still experimental, is very interesting, very useful and can be easily understood by reading its source code. Even though the module pg is inspired by the ISIS system, not all of its features are in place.

As I wrote above all messages are sent to the group, thus all members of the group will receive the message.

The two things I like the most about pg are:

  1. messages are serialized. This means that if one process P1 sends the message M1, and process P2 sends the message M2 at the same time, all the members of the group will receive the two messages in the same order.
  2. whenever one of the members of a group terminates, it is automatically removed from the group, and this totally handled by pg module.

There are a couple of things I don’t like about pg module as well:

  1. the aforesaid serialization is achieved by using a group master process which takes care of forwarding the message to all the group members. This could potentially explode in your face if the amount of messages and members is very lange, but still we are talking about Erlang here, so when I say very large I mean VERY VERY LARGE). That’s why as a common practice, you had better benchmark carefully your code when using such a module and understand how to tune your process group.
  2. causal ordering is not implemented in pg module. Message ordering is a really complex topic, so in order understand it well I suggest you once again the very good book a linked above, which helped me a lot during my Distributed Systems lessons  at University of Trento.

Ok, I know you want to see some erlang code here, but actually pg group functions are somehow similar to pg2 ones, which I discussed in one of my previous posts. There are big differences though between the two modules: for example with pg each message is sent to all members in the group while using pg2, each message may be sent to one, some, or all members using a custom function.

Last thing: if you are using a gen_server behaviour, you can easily handle messages coming from pg group in your handle_info/2. So, for example you can have something like:

handle_info({pg_message, From , PgName, Message}, State) ->
      io:format("Received message: ~p ~n", [Message]),
      {noreply, State};

That’s all folks! For more information about pg, take a look at its documentation!

Categories: English, Erlang Tags: ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: