Leader Election
Overview
Leader election mechanisms are used to determine which operator (or Performer) executes a Task at any given time. These mechanisms ensure the orderly allocation of tasks and optimal system performance while avoiding conflicts. Conflicts can occur when multiple operators try to execute the same task simultaneously, leading to duplicate efforts or inconsistent system states.
Developers can write their own algorithm to determine the Task Performer.
Leader Election Mechanisms
Below are different leader election mechanisms commonly used in decentralized networks:
Round Robin
Description: The "Task Performer" is chosen in a round-robin manner, where operators take turns performing tasks in a fixed sequence based on their IDs. The system cycles through the list of operators, ensuring an even distribution of tasks.
Implementation: This can be implemented by taking the block number modulo the number of operators (plus one). The result gives a number in the range [1..count], corresponding to the chosen performer's ID.
Prevrandao Selection
Description: The "Task Performer" is chosen randomly and deterministically from the pool of nodes. This method ensures that no operator is favored over another, and it can prevent systematic bias in task distribution.
Implementation: A random number generator can be used to select a task performer from the pool of operators.
Stake Weighted Leader Selection
Description: The "Task Performer" is selected based on a weighted probability. It ensures that operators with higher Voting Power (or other assigned weights) have a greater likelihood of being chosen while still maintaining an element of randomness.
Implementation: This can be implemented by retrieving the list of all the active operators, Performing a weighted random selection to determine the Task Performer.
Priority-based Selection
Description: Operators are assigned different priority levels, and the highest priority operator is chosen to perform the task. This priority can be based on factors such as reputation, available resources, or past performance.
Implementation: A priority queue can be used to select the operator with the highest priority for the task.
Leader Election via Consensus
Description: In this approach, operators participate in a consensus mechanism, such as RAFT, to elect a leader to perform the task. This is commonly used in distributed systems, where the leader coordinates task execution and ensures system consistency.
Implementation: Operators vote on who should be the leader, and the one with the majority of votes is chosen to perform the task.
Custom Leader Election Logic
When a Task execution is triggered, the Operator node broadcasts a custom message to the P2P network. Upon receiving this message, the peers run a leader election algorithm to select the operator responsible for executing the task.
Publishing the Task Request
Publish the task request as a Custom Message to the P2P network using the JSON-RPC method.
Invoke the Leader Election Algorithm
When a peer receives a custom message, the /p2p/message
endpoint triggers the leader election algorithm to determine which peer will execute the task.
Last updated