{"id":17464436,"url":"https://github.com/brainlid/demo_service_pool_with_leader","last_synced_at":"2025-06-13T09:08:23.609Z","repository":{"id":138241594,"uuid":"109980468","full_name":"brainlid/demo_service_pool_with_leader","owner":"brainlid","description":"Demonstration project that uses Registry to coordinate a Service Pool where the services have a single leader.","archived":false,"fork":false,"pushed_at":"2017-11-22T18:00:43.000Z","size":33,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-28T07:26:58.635Z","etag":null,"topics":["meetup-demonstration"],"latest_commit_sha":null,"homepage":"","language":"Elixir","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/brainlid.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-11-08T13:37:34.000Z","updated_at":"2018-01-09T13:38:25.000Z","dependencies_parsed_at":null,"dependency_job_id":"d2120a46-5965-4622-98c2-f17c7bf2a57c","html_url":"https://github.com/brainlid/demo_service_pool_with_leader","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/brainlid/demo_service_pool_with_leader","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brainlid%2Fdemo_service_pool_with_leader","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brainlid%2Fdemo_service_pool_with_leader/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brainlid%2Fdemo_service_pool_with_leader/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brainlid%2Fdemo_service_pool_with_leader/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/brainlid","download_url":"https://codeload.github.com/brainlid/demo_service_pool_with_leader/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brainlid%2Fdemo_service_pool_with_leader/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259616383,"owners_count":22884883,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["meetup-demonstration"],"created_at":"2024-10-18T10:45:45.604Z","updated_at":"2025-06-13T09:08:23.558Z","avatar_url":"https://github.com/brainlid.png","language":"Elixir","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ServicePoolLeader\n\nDemo project that shows how to create a pool of services across multiple nodes\nin a cluster where only one is a \"Leader\" at a time. When the Leader node goes\ndown, a different node's service takes over as the Leader.\n\nUsing only built-in Elixir/Erlang features.\n\n## Usage\n\nYou can start multiple nodes on a single machine (each in a different terminal\nwindow).\n\nStart 3 named nodes. Use these same names since the `join/0` function assumes\nthey are named this way to make it easy for playing with it.\n\n```bash\niex --sname a@localhost -S mix\niex --sname b@localhost -S mix\niex --sname c@localhost -S mix\n```\n\nIn one of the IEx terminals, run `join/0` to link up the nodes as a cluster.\n\n```elixir\nServicePoolLeader.join()\n```\n\nPerform some work using the simple/naive approach. Try it from different nodes.\nWho does the work?\n\n```elixir\nServicePoolLeader.simple_work()\nServicePoolLeader.simple_work(10)\nServicePoolLeader.simple_work(5)\n```\n\nThe other primary example is the `ServicePoolLeader.Coordinator` module. It\ncoordinates the `TrackedService`.\n\nThe design decisions were to keep the complexity in the `Coordinator` and out\nof the services being managed.\n\nExperiment with those examples using the following:\n\n```elixir\nServicePoolLeader.tracked_work()\nServicePoolLeader.tracked_work(\"a\")\nServicePoolLeader.tracked_work(10)\n```\n\nWhat happens when you kill one of the services? (Can use `:observer.start` to\nexplore and kill it.)\n\nWhat happens when you kill one of the nodes? (`ctrl+c, ctrl+c`)\n\nSome things to experiment with:\n\n* Start multiple nodes that join as a cluster.\n* Use `:observer.start` to see how it's running, inspect ETS tables and kill things.\n* Kill the leader node. What happens?\n* Restart that node and rejoin the cluster.\n* Kill a non-leader node, what happened? Rejoin.\n* Do a kind shutdown on the leader node, what happened?, Rejoin.\n\n## Leadership Election Options\n\nThere are a number of Leader Election strategies and options you might consider.\n\n* Hardware differences? Prefer to run on the \"big\" machine?\n* Other services running? Prefer to run on a different node than service Y.\n  * May want to avoid two IO intensive services from thrashing the disk on the same node.\n  * Allows it to still run together in a single-node setup.\n* Oldest running assumes all else is equal.\n\nThis demo project uses the \"longest running\" service as to how a leader is\nelected.\n\n## Features Used for Discussion/Experimentation\n\n* Clustering\n* `:observer.start` for killing and viewing ETS tables\n* Process monitoring\n* `:pg2`\n* `:ets` tables\n* `GenServer.multi_call()`\n* Supervisor `:rest_for_one` strategy\n* `Registry` example is built-in too\n\n## Ideas for future experiments\n\n* Provide a function or MFA to call when new leader should be appointed\nbecause the current leader left or went down. Externally define the function\nthat selects the leader.\n* Add a new Supervisor to manage the Coordinator and TrackedService so the\n`:rest_for_one` strategy is more controlled.\n* TrackedService have unique behavior based on being a the leader. Could ask\nCoordinator if it is the leader.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbrainlid%2Fdemo_service_pool_with_leader","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbrainlid%2Fdemo_service_pool_with_leader","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbrainlid%2Fdemo_service_pool_with_leader/lists"}