Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Explain "Leader/Follower" Pattern

I can't seem to find a good and accessible explanation of "Leader/Follower" pattern. All explanations are simply completely meaningless as in 1.

Can anyone explain to the the mechanics of how this pattern works, and why and how it improves performance over more traditional asynchronous IO models? Examples and links to diagrams are appreciated too.

like image 730
Alex B Avatar asked Jun 17 '10 01:06

Alex B


People also ask

What is the meaning of leader and follower?

Leaders are willing to step up and take control of a project or task. They enjoy a challenge and embrace change as they know it will benefit them in the long term. Followers have to be forced to drive a project by their boss. They rarely, if ever, volunteer to take on a project.

What are the types of leaders and followers?

Kelley (1992) posited that there are five followership styles. These include exemplary, conformist, passive, alienated and pragmatist styles (Kelley, 1992). These followership styles are based on a combination of two different followership dimensions: engagement and critical thinking (Kelley, 1992).

How leaders and followers work together?

Leaders and followers are influenced by each other Leaders influence their followers according to the company's needs, while followers influence leaders with their attitude and actions – it can have both positive and negative impacts.


2 Answers

As you might have read, the pattern consists of 4 components: ThreadPool, HandleSet, Handle, ConcreteEventHandler (implements the EventHandler interface).

You can think of it as a taxi station at night, where all the drivers are sleeping except for one, the leader. The ThreadPool is a station managing many threads - cabs.

The leader is waiting for an IO event on the HandleSet, like how a driver waits for a client.

When a client arrives (in the form of a Handle identifying the IO event), the leader driver wakes up another driver to be the next leader and serves the request from his passenger.

While he is taking the client to the given address (calling ConcreteEventHandler and handing over Handle to it) the next leader can concurrently serve another passenger.

When a driver finishes he take his taxi back to the station and falls asleep if the station is not empty. Otherwise he become the leader.

The pros for this pattern are:

  • no communication between the threads are necessary, no synchronization, nor shared memory (no locks, mutexes) are needed.
  • more ConcreteEventHandlers can be added without affecting any other EventHandler
  • minimizes the latency because of the multiple threads

The cons are:

  • complex
  • network IO can be a bottleneck
like image 168
Bálint Fodor Avatar answered Sep 29 '22 20:09

Bálint Fodor


I want to add to Jake's answer by linking another PDF from the same author that details a use case where they chose the Leader/Follower pattern over other alternatives: http://www.dre.vanderbilt.edu/~schmidt/PDF/OM-01.pdf

like image 42
stan Avatar answered Sep 29 '22 22:09

stan