Skip to content

supabase/libcluster_postgres

Repository files navigation

Libcluster Postgres Strategy

Postgres Strategy for libcluster which is used by Supabase on the realtime, supavisor and logflare projects.

You can test it out by running docker compose up

example.png

Installation

The package can be installed by adding libcluster_postgres to your list of dependencies in mix.exs:

def deps do
  [{:libcluster_postgres, "~> 0.1"}]
end

How it works

We connect to a Postgres instance using Postgrex. With the Postgrex.Notifications module we will track for LISTEN events on the configured channel. We'll also use NOTIFY queries to send the node's information.

How to use it

To use it, set your configuration file with the informations for your database:

config :libcluster,
  topologies: [
    example: [
      strategy: Cluster.Strategy.Postgres,
      config: [
          hostname: "localhost",
          username: "postgres",
          password: "postgres",
          database: "postgres",
          port: 5432,
          parameters: [],
          channel_name: "cluster"
      ],
    ]
  ]

Then add it to your supervision tree:

defmodule MyApp do
  use Application

  def start(_type, _args) do
    topologies = Application.get_env(:libcluster, :topologies)
    children = [
      # ...
      {Cluster.Supervisor, [topologies]}
      # ...
    ]
    Supervisor.start_link(children, strategy: :one_for_one)
  end
end

Acknowledgements

A special thank you to @gotbones for creating libcluster and @kevinbuch_ for the original inspiration for this strategy.

About

Postgres strategy for libcluster

Resources

License

Code of conduct

Contributing

Stars

Watchers

Forks

Packages

No packages published

Contributors 8