{"id":22452854,"url":"https://github.com/svsool/distributed_pubsub","last_synced_at":"2025-10-07T21:05:29.031Z","repository":{"id":264497437,"uuid":"893537772","full_name":"svsool/distributed_pubsub","owner":"svsool","description":"Distributed PubSub using Consistent Hashing and Erlang's Process Group","archived":false,"fork":false,"pushed_at":"2025-03-23T15:54:41.000Z","size":62,"stargazers_count":9,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-08T22:23:26.119Z","etag":null,"topics":["distributed","elixir","pubsub"],"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/svsool.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":"2024-11-24T17:42:10.000Z","updated_at":"2025-03-23T15:54:44.000Z","dependencies_parsed_at":"2024-11-24T18:41:41.736Z","dependency_job_id":null,"html_url":"https://github.com/svsool/distributed_pubsub","commit_stats":null,"previous_names":["svsool/distributed_pubsub"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/svsool/distributed_pubsub","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/svsool%2Fdistributed_pubsub","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/svsool%2Fdistributed_pubsub/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/svsool%2Fdistributed_pubsub/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/svsool%2Fdistributed_pubsub/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/svsool","download_url":"https://codeload.github.com/svsool/distributed_pubsub/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/svsool%2Fdistributed_pubsub/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":278846711,"owners_count":26056166,"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","status":"online","status_checked_at":"2025-10-07T02:00:06.786Z","response_time":59,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["distributed","elixir","pubsub"],"created_at":"2024-12-06T06:13:29.212Z","updated_at":"2025-10-07T21:05:29.024Z","avatar_url":"https://github.com/svsool.png","language":"Elixir","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Distributed PubSub\n\nThis is a proof-of-concept implementation of Distributed PubSub using [Distributed Process Group](https://www.erlang.org/doc/apps/kernel/pg.html), [GenServer](https://hexdocs.pm/elixir/GenServer.html), [Consistent Hash Ring](https://github.com/discord/ex_hash_ring), [Manifold](https://github.com/discord/manifold), and [Channels](https://hexdocs.pm/phoenix/channels.html). Scalable PubSub layer is an essential component of message delivery in chat applications and beyond.\n\n[Ring](https://github.com/discord/ex_hash_ring) used to determine Topic's Node, subscribe to Topic's GenServer, and fan out messages to subscribers, subscriber PIDs are grouped by their remote node to reduce number of `send/2` calls.\n\nDirect message publishing helps mitigate the [Bandwidth Bisection](https://en.wikipedia.org/wiki/Bisection_bandwidth) problem, which the default broadcast [PubSub Adapter](https://github.com/phoenixframework/phoenix_pubsub) is more prone to, since number of messages grows quadratically with the number of nodes in the cluster when using broadcast.\n\nTopic is roughly equivalent to a Guild mentioned in [How Discord Scaled Elixir to 5,000,000 Concurrent Users](https://discord.com/blog/how-discord-scaled-elixir-to-5-000-000-concurrent-users).\n\n**NB** Production ready system would involve dynamic topology management, replication, netsplit recovery, stricter delivery semantics, message queueing, [overload protection](https://ferd.ca/handling-overload.html) and other things.\n\n## Architecture\n\n```plaintext\n  +--------------+\n  | Message Flow |\n  +--------------+\n\n\n  Client 1 --(Topic 1)--\u003e  WS Server 1  --\u003e  Topic Server 1  --------+\n                                                                     |\n                                                                     |\n  Client 2 \u003c--(Topic 1)--  WS Server 2  \u003c--  Topic 1 (GenServer) ----+\n\n\n                                ProcessGroup\n                         +------------------+\n  +------------+ Topic 1 | +--------------+ |           GenServers\n  |  Client 1  |---------| | WS Server 1  | |  +-----------------+\n  +------------+         | +-------|------+ |  | +-------------+ |\n                         |         |        |  | |   Topic 1   |-----+\n                         | +-------+------+ |  | +-------------+ |   |\n                         | |Topic Server 1|----+ +-------------+ |   |\n                         | +--------------+ |  | |   Topic 2   | |   |\n                         |  |            |  |  | +-------------+ |   |\n                         |  |            |  |  +-----------------+   |\n                         |  +(Hash Ring) +  |  +-----------------+   |\n                         |  |            |  |  | +-------------+ |   |\n                         |  |            |  |  | |   Topic N   | |   |\n                         | +--------------+ |  | +-------------+ |   |\n                         | |Topic Server X|----+ +-------------+ |   |\n                         | +--------------+ |  | | Topic N + 1 | |   |\n                         |                  |  | +-------------+ |   |\n  +------------+ Topic 1 | +--------------+ |  +-----------------+   |\n  |  Client 2  |+--------|-- WS Server X  |+-------------------------|\n  +------------+         | +--------------+ |\n                         +------------------+\n```\n\n## Getting started\n\n```\n# relevant for macos\nbrew install asdf wxwidgets\n\n# install erlang and elixir\nasdf plugin add elixir\nasdf install elixir 1.17.1-otp-27\nasdf plugin add erlang\nasdf install erlang 27.0\n\n# install dependencies\nmix deps.get\n```\n\n## Run cluster\n\nApplication can be started in multiple modes: `ws` (websocket) and `ts` (topic server).\n\n```\n# start websocket servers\nDPS_PORT=4000 DPS_APP=ws iex --sname dps-a -S mix phx.server\nDPS_PORT=4001 DPS_APP=ws iex --sname dps-b -S mix phx.server\n\n# start topic servers\nDPS_APP=ts iex --sname dps-ts-a -S mix\nDPS_APP=ts iex --sname dps-ts-b -S mix\n```\n\nRing is static for demonstration purpose, and can be adjusted in [config/dev.exs](./config/dev.exs) given cluster changes.\n\n## Usage\n\n```\n# terminal 1\nwebsocat \"ws://127.0.0.1:4000/socket/websocket?vsn=2.0.0\"\n[\"1\", \"1\", \"topics:matrix\", \"phx_join\", {}]\n\n# terminal 2\nwebsocat \"ws://127.0.0.1:4001/socket/websocket?vsn=2.0.0\"\n[\"1\", \"1\", \"topics:matrix\", \"phx_join\", {}]\n[\"1\", \"1\", \"topics:matrix\", \"publish\", [\"event\", { \"message\": \"red pill or blue pill?\"}]]\n\n# =\u003e the following message should appear in both terminals\n[\"1\",null,\"topics:matrix\",\"event\",{\"message\":\"red pill or blue pill?\"}]\n```\n\n## Commands\n\n```\n# tests\nmix test\n\n# quality\nmix quality\n\n# debugging\n:observer.start()\n```\n\n## Resources\n\n- [How Discord Scaled Elixir to 5,000,000 Concurrent Users](https://discord.com/blog/how-discord-scaled-elixir-to-5-000-000-concurrent-users)\n- [Pushing Discord’s Limits with a Million+ Online Users in a Single Server](https://discord.com/blog/maxjourney-pushing-discords-limits-with-a-million-plus-online-users-in-a-single-server)\n- [How WhatsApp Scaled Erlang Cluster to 10000 Nodes](https://www.youtube.com/watch?v=FJQyv26tFZ8)\n- [RPC in WhatsApp with 40000+ Nodes](https://www.youtube.com/watch?v=A5bLRH-PoMY)\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsvsool%2Fdistributed_pubsub","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsvsool%2Fdistributed_pubsub","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsvsool%2Fdistributed_pubsub/lists"}