View Source ProcessHub.Strategy.Redundancy.Replication (ProcessHub v0.2.0-alpha)
The replication strategy allows for multiple instances of a process to be started
across the cluster. The number of instances is determined by the replication_factor
option.
The replication strategy also comes with a replication_model
option, which determines
which instances of the process are active and which are passive.
Imagine a scenario where you have a process that is responsible for handling streams of
data. You may want to have multiple instances of this process running across the cluster,
but only one of them should be active at any given time and insert data into the database.
The passive instances of the process should be ready to take over if the active instance
fails.
Replication strategy selects the master/active node based on the
child_id
andchild_nodes
arguments. Thechild_id
is converted to a charlist and summed up. The sum is then used to calculate the index of the master node in thechild_nodes
list. This ensures that the same master node is selected for the samechild_id
andchild_nodes
arguments.
This strategy also allows replication on all nodes in the cluster. This is done by setting
the replication_factor
option to :cluster_size
.
ProcessHub
can notify the process when it is active or passive by sending a :redundancy_signal
.
Using the :redundancy_signal option
When using the
:redundancy_signal
option, make sure that the processes are handling the message{:process_hub, :redundancy_signal, mode}
, where themode
variable is either:active
or:passive
.
Example GenServer
process handling the :redundancy_signal
:
def handle_info({:process_hub, :redundancy_signal, mode}, state) do
# Update the state with the new mode and do something differently.
{:noreply, Map.put(state, :replication_mode, mode)}
end
Summary
Types
@type t() :: %ProcessHub.Strategy.Redundancy.Replication{ redundancy_signal: :none | :active | :passive | :all, replication_factor: pos_integer() | :cluster_size, replication_model: :active_active | :active_passive }
Replication strategy options.
replication_factor
- The number of instances of a single process across the cluster. This option can be positive integer orcluster_size
atom to replicate process on all nodes. Default value is2
.replication_model
- This option determines the mode in which the instances of processes are started. Default value is:active_active
.:active_active
- All instances of a process across the cluster are equal.:active_passive
- Only one instance of a process across the cluster is active; the rest are passive. Remaining replicas are started as passive processes.
redundancy_signal
- This option determines when a process should be notified of its replication mode. Default value is:none
.:none
- No notifications are sent.:active_active
- Only active processes are notified.:active_passive
- Only passive processes are notified.:all
- All processes are notified.