The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
Blog post: Elixir Clustering using Postgres (#20236)
Ramiro Nuñez Dosiodcde591c7df0
diff --git a/apps/www/_blog/2022-11-08-authentication-in-ionic-angular.mdx b/apps/www/_blog/2022-11-08-authentication-in-ionic-angular.mdxindex 3b497cb2b54..e34c4d02fa7 100644--- a/apps/www/_blog/2022-11-08-authentication-in-ionic-angular.mdx+++ b/apps/www/_blog/2022-11-08-authentication-in-ionic-angular.mdx@@ -5,7 +5,7 @@ author: simon_grimmimage: simmon-grim-ionic-angular-authentication/ionic-angular-supabase.jpgthumb: simmon-grim-ionic-angular-authentication/ionic-angular-supabase.jpgcategories:- - engineering+ - developerstags:- ionic- angulardiff --git a/apps/www/_blog/2022-11-22-flutter-authorization-with-rls.mdx b/apps/www/_blog/2022-11-22-flutter-authorization-with-rls.mdxindex afcc6519237..34badf23844 100644--- a/apps/www/_blog/2022-11-22-flutter-authorization-with-rls.mdx+++ b/apps/www/_blog/2022-11-22-flutter-authorization-with-rls.mdx@@ -5,7 +5,7 @@ author: tyler_shukertimage: flutter-chat-auth/supabase-flutter-auth.jpegthumb: flutter-chat-auth/supabase-flutter-auth.jpegcategories:- - engineering+ - developerstags:- flutter- authdiff --git a/apps/www/_blog/2022-12-14-mfa-auth-via-rls.mdx b/apps/www/_blog/2022-12-14-mfa-auth-via-rls.mdxindex 7c3af2ccf7c..42bf196a8a0 100644--- a/apps/www/_blog/2022-12-14-mfa-auth-via-rls.mdx+++ b/apps/www/_blog/2022-12-14-mfa-auth-via-rls.mdx@@ -5,7 +5,7 @@ author: joel,kangmingtay,stojanimage: lw6-auth/mfa-banner.jpgthumb: lw6-auth/mfa-banner.jpgcategories:- - engineering+ - producttags:- launch-week- authdiff --git a/apps/www/_blog/2023-02-14-flutter-real-time-multiplayer-game.mdx b/apps/www/_blog/2023-02-14-flutter-real-time-multiplayer-game.mdxindex 62bf648db92..2e8840fa656 100644--- a/apps/www/_blog/2023-02-14-flutter-real-time-multiplayer-game.mdx+++ b/apps/www/_blog/2023-02-14-flutter-real-time-multiplayer-game.mdx@@ -5,7 +5,7 @@ author: tyler_shukertimage: flutter-realtime-game/flutter-flame-realtime-multiplayer-game.pngthumb: flutter-realtime-game/flutter-flame-realtime-multiplayer-game.pngcategories:- - engineering+ - developerstags:- flutter- mobilediff --git a/apps/www/_blog/2023-03-01-geo-queries-with-postgis-in-ionic-angular.mdx b/apps/www/_blog/2023-03-01-geo-queries-with-postgis-in-ionic-angular.mdxindex aa2da11c60f..1fe257b49b2 100644--- a/apps/www/_blog/2023-03-01-geo-queries-with-postgis-in-ionic-angular.mdx+++ b/apps/www/_blog/2023-03-01-geo-queries-with-postgis-in-ionic-angular.mdx@@ -5,7 +5,7 @@ author: simon_grimmimage: 2023-02-28-geoqueries-postgis/postgis-ionic-angular.pngthumb: 2023-02-28-geoqueries-postgis/postgis-ionic-angular.pngcategories:- - engineering+ - developerstags:- ionic- postgisdiff --git a/apps/www/_blog/2023-05-04-flutter-multi-factor-authentication.mdx b/apps/www/_blog/2023-05-04-flutter-multi-factor-authentication.mdxindex ab90ec306a5..4251119825d 100644--- a/apps/www/_blog/2023-05-04-flutter-multi-factor-authentication.mdx+++ b/apps/www/_blog/2023-05-04-flutter-multi-factor-authentication.mdx@@ -5,7 +5,7 @@ author: tyler_shukertimage: flutter-mfa/flutter-mfa-thumb-og.pngthumb: flutter-mfa/flutter-mfa-thumb.jpgcategories:- - engineering+ - developerstags:- flutter- authdiff --git a/apps/www/_blog/2023-05-15-building-chatgpt-plugins-template.mdx b/apps/www/_blog/2023-05-15-building-chatgpt-plugins-template.mdxindex 8cca833e100..f14ec6d0a78 100644--- a/apps/www/_blog/2023-05-15-building-chatgpt-plugins-template.mdx+++ b/apps/www/_blog/2023-05-15-building-chatgpt-plugins-template.mdx@@ -2,7 +2,7 @@title: 'Building ChatGPT Plugins with Supabase Edge Runtime'description: We're releasing a ChatGPT plugin template written in TypeScript and running on Deno!categories:- - engineering+ - developerstags:- AI- tutorialdiff --git a/apps/www/_blog/2023-05-25-chatgpt-plugins-support-postgres.mdx b/apps/www/_blog/2023-05-25-chatgpt-plugins-support-postgres.mdxindex b97e1446bf5..23e9302f780 100644--- a/apps/www/_blog/2023-05-25-chatgpt-plugins-support-postgres.mdx+++ b/apps/www/_blog/2023-05-25-chatgpt-plugins-support-postgres.mdx@@ -2,7 +2,7 @@title: 'ChatGPT plugins now support Postgres & Supabase'description: Supabase recently contributed to the OpenAI Retrieval Plugin repo with a Postgres and a Supabase implementation to help developers build ChatGPT plugins using pgvector.categories:- - engineering+ - developerstags:- AI- tutorialdiff --git a/apps/www/_blog/2023-07-18-flutter-authentication.mdx b/apps/www/_blog/2023-07-18-flutter-authentication.mdxindex a1f7dee6898..c1842d4dbe6 100644--- a/apps/www/_blog/2023-07-18-flutter-authentication.mdx+++ b/apps/www/_blog/2023-07-18-flutter-authentication.mdx@@ -2,7 +2,7 @@title: 'Getting started with Flutter authentication'description: Learn how authentication on Flutter works through Google sign in with Supabase auth.categories:- - engineering+ - developerstags:- flutter- authdiff --git a/apps/www/_blog/2023-08-01-react-native-storage.mdx b/apps/www/_blog/2023-08-01-react-native-storage.mdxindex 3d0b91aa081..36c783a33ad 100644--- a/apps/www/_blog/2023-08-01-react-native-storage.mdx+++ b/apps/www/_blog/2023-08-01-react-native-storage.mdx@@ -5,7 +5,7 @@ author: simon_grimmimage: 2023-08-01-react-native-storage/react-native-storage.pngthumb: 2023-08-01-react-native-storage/react-native-storage.pngcategories:- - engineering+ - developerstags:- react-native- storagediff --git a/apps/www/_blog/2023-10-08-react-native-offline-first-watermelon-db.mdx b/apps/www/_blog/2023-10-08-react-native-offline-first-watermelon-db.mdxindex fdf9f1b24d2..8f60daee717 100644--- a/apps/www/_blog/2023-10-08-react-native-offline-first-watermelon-db.mdx+++ b/apps/www/_blog/2023-10-08-react-native-offline-first-watermelon-db.mdx@@ -5,7 +5,7 @@ author: benediktmuellerimage: 2023-10-08-react-native-offline-first-watermelon-db/expo-watermelondb-supabase.pngthumb: 2023-10-08-react-native-offline-first-watermelon-db/expo-watermelondb-supabase.pngcategories:- - engineering+ - developerstags:- react-native- authdiff --git a/apps/www/_blog/2023-11-16-react-native-authentication.mdx b/apps/www/_blog/2023-11-16-react-native-authentication.mdxindex 32bd108fac3..821035b68a5 100644--- a/apps/www/_blog/2023-11-16-react-native-authentication.mdx+++ b/apps/www/_blog/2023-11-16-react-native-authentication.mdx@@ -2,7 +2,7 @@title: 'Getting started with React Native authentication'description: Learn how to implement authentication in your React Native applications.categories:- - engineering+ - developerstags:- react-native- authdiff --git a/apps/www/_blog/2023-11-21-oauth2-login-python-flask-apps.mdx b/apps/www/_blog/2023-11-21-oauth2-login-python-flask-apps.mdxindex fc90f06293f..59d335e8ae3 100644--- a/apps/www/_blog/2023-11-21-oauth2-login-python-flask-apps.mdx+++ b/apps/www/_blog/2023-11-21-oauth2-login-python-flask-apps.mdx@@ -2,7 +2,7 @@title: 'GitHub OAuth in your Python Flask app'description: A step-by-step guide on building Login with Github into your Python apps.categories:- - engineering+ - developerstags:- python- authdiff --git a/apps/www/_blog/2024-01-09-elixir-clustering-using-postgres.mdx b/apps/www/_blog/2024-01-09-elixir-clustering-using-postgres.mdxnew file mode 100644index 00000000000..f8778146a35--- /dev/null+++ b/apps/www/_blog/2024-01-09-elixir-clustering-using-postgres.mdx@@ -0,0 +1,173 @@+---+title: Elixir clustering using Postgres+description: 'Learn about our approach to connecting multiple nodes in Elixir using Postgres'+author: filipe+image: elixir-clustering-using-postgres/elixir-clustering-OG.png+thumb: elixir-clustering-using-postgres/elixir-clustering-thumb.png+categories:+ - engineering+tags:+ - supabase-engineering+ - planetpg+date: '2024-01-09'+toc_depth: 3+---++<Admonition type="note" label="Supabase Engineering">++Introducing Supabase Engineering—a new blog series where our engineering teams discuss what they build, share tools, technologies, best practices, and how they overcome the challenges they face. Kicking off the series is a post by Filipe Cabaço from the Realtime team.++</Admonition>++Elixir offers a powerful feature by allowing multiple nodes to communicate between them without extra services in the middle, reducing the overall complexity of your system.++<Img+ src={{+ dark: '/images/blog/elixir-clustering-using-postgres/elixir-multiple-nodes.png',+ light: '/images/blog/elixir-clustering-using-postgres/elixir-multiple-nodes-light.png',+ }}+ alt="Elixir nodes communicating between each other without extra services in the middle"+/>++However, when it comes to connecting the servers, there seems to be a barrier of entry that many people encounter, including ourselves, on how to provide the name discovery required to connect said servers. We have released our approach to solving this problem by open-sourcing [libcluster Postgres Strategy](https://github.com/supabase/libcluster_postgres) and today, we explore the motivations behind its creation and the methodologies employed in its development.++<Img+ src={{+ dark: '/images/blog/elixir-clustering-using-postgres/libcluster.png',+ light: '/images/blog/elixir-clustering-using-postgres/libcluster-light.png',+ }}+ alt="Elixir nodes communicating between each other without extra services in the middle"+/>++## Why do we need a distributed Erlang Cluster?++At Supabase, we use clustering in all of our Elixir projects which include [Logflare](https://supabase.com/docs/guides/database/extensions/wrappers/logflare), [Supavisor](https://supabase.com/blog/supavisor-postgres-connection-pooler) and [Realtime](https://supabase.com/docs/guides/realtime). With multiple servers connected, we can load shed, create globally distributed services, and provide the best service to our customers so we’re closer to them geographically and to their instances, reducing overall latency.++<Img+ src={{+ dark: '/images/blog/elixir-clustering-using-postgres/realtime-architecture.png',+ light: '/images/blog/elixir-clustering-using-postgres/realtime-architecture-light.png',+ }}+ alt="Example of Realtime architecture where a customer from CA will connect to the server closest to them and their Supabase instance"+/>++To achieve a connected cluster, we wanted to be as cloud-agnostic as possible. This makes our self-hosting options more accessible. We don’t want to introduce extra services to solve this single issue - Postgres is the logical way to achieve it.++The other piece of the puzzle was already built by the Erlang community being the defacto library to facilitate the creation of connected Elixir servers: [libcluster](https://github.com/bitwalker/libcluster).++## What is libcluster?++[libcluster](https://github.com/bitwalker/libcluster) is the go-to package for connecting multiple BEAM instances and setting up healing strategies. libcluster provides out-of-the-box strategies and it allows users to define their own strategies by implementing a simple behavior that defines cluster formation and healing according to the supporting service you want to use.++## How did we use Postgres?++Postgres provides an event system using two commands: [NOTIFY](https://www.postgresql.org/docs/current/sql-notify.html) and [LISTEN](https://www.postgresql.org/docs/current/sql-listen.html) so we can use them to propagate events within our Postgres instance.++To use these features, you can use psql itself or any other Postgres client. Start by listening on a specific channel, and then notify to receive a payload.++```markdown+postgres=# LISTEN channel;+LISTEN+postgres=# NOTIFY channel, 'payload';+NOTIFY+Asynchronous notification "channel" with payload "payload" received from server process with PID 326.+```++Now we can replicate the same behavior in Elixir and [Postgrex](https://hex.pm/packages/postgrex) within IEx (Elixir's interactive shell).++```elixir+Mix.install([{:postgrex, "~> 0.17.3"}])+config = [+ hostname: "localhost",+ username: "postgres",+ password: "postgres",+ database: "postgres",+ port: 5432+]+{:ok, db_notification_pid} = Postgrex.Notifications.start_link(config)+Postgrex.Notifications.listen!(db_notification_pid, "channel")+{:ok, db_conn_pid} = Postgrex.start_link(config)+Postgrex.query!(db_conn_pid, "NOTIFY channel, 'payload'", [])++receive do msg -> IO.inspect(msg) end+# Mailbox will have a message with the following content:+# {:notification, #PID<0.223.0>, #Reference<0.57446457.3896770561.212335>, "channel", "test"}+```++## Building the strategy++Using the libcluster `Strategy` behavior, inspired by [this GitHub repository](https://github.com/kevbuchanan/libcluster_postgres) and knowing how `NOTIFY/LISTEN` works, implementing a strategy becomes straightforward:++1. We send a `NOTIFY` to a channel with our `node()` address to a configured channel++```elixir+# lib/cluster/strategy/postgres.ex:52+def handle_continue(:connect, state) do+ with {:ok, conn} <- Postgrex.start_link(state.meta.opts.()),+ {:ok, conn_notif} <- Postgrex.Notifications.start_link(state.meta.opts.()),+ {_, _} <- Postgrex.Notifications.listen(conn_notif, state.config[:channel_name]) do+ Logger.info(state.topology, "Connected to Postgres database")++ meta = %{+ state.meta+ | conn: conn,+ conn_notif: conn_notif,+ heartbeat_ref: heartbeat(0)+ }++ {:noreply, put_in(state.meta, meta)}+ else+ reason ->+ Logger.error(state.topology, "Failed to connect to Postgres: #{inspect(reason)}")+ {:noreply, state}+ end+ end+```++1. We actively listen for new `{:notification, pid, reference, channel, payload}` messages and connect to the node received in the payload++```elixir+# lib/cluster/strategy/postgres.ex:80+def handle_info({:notification, _, _, _, node}, state) do+ node = String.to_atom(node)++ if node != node() do+ topology = state.topology+ Logger.debug(topology, "Trying to connect to node: #{node}")++ case Strategy.connect_nodes(topology, state.connect, state.list_nodes, [node]) do+ :ok -> Logger.debug(topology, "Connected to node: #{node}")+ {:error, _} -> Logger.error(topology, "Failed to connect to node: #{node}")+ end+ end++ {:noreply, state}+ end+```++1. Finally, we configure a heartbeat that is similar to the first message sent for cluster formation so libcluster is capable of heal if need be++```elixir+# lib/cluster/strategy/postgres.ex:73+def handle_info(:heartbeat, state) do+ Process.cancel_timer(state.meta.heartbeat_ref)+ Postgrex.query(state.meta.conn, "NOTIFY #{state.config[:channel_name]}, '#{node()}'", [])+ ref = heartbeat(state.config[:heartbeat_interval])+ {:noreply, put_in(state.meta.heartbeat_ref, ref)}+end+```++These three simple steps allow us to connect as many nodes as needed, regardless of the cloud provider, by utilizing something that most projects already have: a Postgres connection.++## Conclusion++In this post, we have described our approach to connecting multiple nodes in Elixir using Postgres. We have also made this strategy available for anyone to use. Please check the code at [github.com/supabase/libcluster_postgres](https://github.com/supabase/libcluster_postgres)++A special thank you to [@gotbones](https://twitter.com/gotbones) for creating libcluster and [@kevinbuch\_](https://twitter.com/kevinbuch_) for the original inspiration for this strategy.++## More Supabase Realtime++- [Realtime docs](https://supabase.com/docs/guides/realtime)+- [Realtime: Multiplayer Edition](https://supabase.com/blog/supabase-realtime-multiplayer-general-availability)+- [Video - How to subscribe to real-time changes on your database](https://www.youtube.com/watch?v=2rUjcmgZDwQ)+- [Video - Listening to real-time changes on the database with Flutter and Supabase](https://www.youtube.com/watch?v=gboTC2lcgzw)diff --git a/apps/www/lib/authors.json b/apps/www/lib/authors.jsonindex f0cb2ff2cdb..e5924f754ca 100644--- a/apps/www/lib/authors.json+++ b/apps/www/lib/authors.json@@ -397,5 +397,13 @@"position": "Guest Author","author_url": "https://twitter.com/psteinroe","author_image_url": "/images/philipp-steinrotter.jpg"+ },+ {+ "author_id": "filipe",+ "author": "Filipe Cabaço",+ "username": "filipe",+ "position": "Engineering",+ "author_url": "https://twitter.com/filipecabaco",+ "author_image_url": "https://github.com/filipecabaco.png"}]diff --git a/apps/www/public/images/blog/elixir-clustering-using-postgres/elixir-clustering-OG.png b/apps/www/public/images/blog/elixir-clustering-using-postgres/elixir-clustering-OG.pngnew file mode 100644index 00000000000..6c0cbab902eBinary files /dev/null and b/apps/www/public/images/blog/elixir-clustering-using-postgres/elixir-clustering-OG.png differdiff --git a/apps/www/public/images/blog/elixir-clustering-using-postgres/elixir-clustering-thumb.png b/apps/www/public/images/blog/elixir-clustering-using-postgres/elixir-clustering-thumb.pngnew file mode 100644index 00000000000..3304df1a01aBinary files /dev/null and b/apps/www/public/images/blog/elixir-clustering-using-postgres/elixir-clustering-thumb.png differdiff --git a/apps/www/public/images/blog/elixir-clustering-using-postgres/elixir-multiple-nodes-light.png b/apps/www/public/images/blog/elixir-clustering-using-postgres/elixir-multiple-nodes-light.pngnew file mode 100644index 00000000000..258dc4e90e1Binary files /dev/null and b/apps/www/public/images/blog/elixir-clustering-using-postgres/elixir-multiple-nodes-light.png differdiff --git a/apps/www/public/images/blog/elixir-clustering-using-postgres/elixir-multiple-nodes.png b/apps/www/public/images/blog/elixir-clustering-using-postgres/elixir-multiple-nodes.pngnew file mode 100644index 00000000000..4a35ab8722aBinary files /dev/null and b/apps/www/public/images/blog/elixir-clustering-using-postgres/elixir-multiple-nodes.png differdiff --git a/apps/www/public/images/blog/elixir-clustering-using-postgres/libcluster-light.png b/apps/www/public/images/blog/elixir-clustering-using-postgres/libcluster-light.pngnew file mode 100644index 00000000000..1d72505572bBinary files /dev/null and b/apps/www/public/images/blog/elixir-clustering-using-postgres/libcluster-light.png differdiff --git a/apps/www/public/images/blog/elixir-clustering-using-postgres/libcluster.png b/apps/www/public/images/blog/elixir-clustering-using-postgres/libcluster.pngnew file mode 100644index 00000000000..32d7f36268fBinary files /dev/null and b/apps/www/public/images/blog/elixir-clustering-using-postgres/libcluster.png differdiff --git a/apps/www/public/images/blog/elixir-clustering-using-postgres/realtime-architecture-light.png b/apps/www/public/images/blog/elixir-clustering-using-postgres/realtime-architecture-light.pngnew file mode 100644index 00000000000..23ea6bb3060Binary files /dev/null and b/apps/www/public/images/blog/elixir-clustering-using-postgres/realtime-architecture-light.png differdiff --git a/apps/www/public/images/blog/elixir-clustering-using-postgres/realtime-architecture.png b/apps/www/public/images/blog/elixir-clustering-using-postgres/realtime-architecture.pngnew file mode 100644index 00000000000..c3e0a5e3a0cBinary files /dev/null and b/apps/www/public/images/blog/elixir-clustering-using-postgres/realtime-architecture.png differdiff --git a/apps/www/public/planetpg-filipe-rss.xml b/apps/www/public/planetpg-filipe-rss.xmlnew file mode 100644index 00000000000..8c8374f7380--- /dev/null+++ b/apps/www/public/planetpg-filipe-rss.xml@@ -0,0 +1,19 @@++ <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">+ <channel>+ <title>'Postgres | Supabase Blog</title>+ <link>https://supabase.com/blog</link>+ <description>Latest Postgres news from Filipe Cabaço at Supabase</description>+ <language>en</language>+ <lastBuildDate>Mon, 08 Jan 2024 23:00:00 GMT</lastBuildDate>+ <atom:link href="https://supabase.com/planetpg-filipe-rss.xml" rel="self" type="application/rss+xml"/>+ <item>+ <guid>https://supabase.com/blog/elixir-clustering-using-postgres</guid>+ <title>Elixir clustering using Postgres</title>+ <link>https://supabase.com/blog/elixir-clustering-using-postgres</link>+ <description>Learn about our approach to connecting multiple nodes in Elixir using Postgres</description>+ <pubDate>Mon, 08 Jan 2024 23:00:00 GMT</pubDate>+</item>++ </channel>+ </rss>diff --git a/apps/www/public/rss.xml b/apps/www/public/rss.xmlindex 57e040fa9c7..cf5f0c4cf9e 100644--- a/apps/www/public/rss.xml+++ b/apps/www/public/rss.xml@@ -5,9 +5,16 @@<link>https://supabase.com</link><description>Latest news from Supabase</description><language>en</language>- <lastBuildDate>Thu, 04 Jan 2024 23:00:00 GMT</lastBuildDate>+ <lastBuildDate>Mon, 08 Jan 2024 23:00:00 GMT</lastBuildDate><atom:link href="https://supabase.com/rss.xml" rel="self" type="application/rss+xml"/><item>+ <guid>https://supabase.com/blog/elixir-clustering-using-postgres</guid>+ <title>Elixir clustering using Postgres</title>+ <link>https://supabase.com/blog/elixir-clustering-using-postgres</link>+ <description>Learn about our approach to connecting multiple nodes in Elixir using Postgres</description>+ <pubDate>Mon, 08 Jan 2024 23:00:00 GMT</pubDate>+</item>+<item><guid>https://supabase.com/blog/beta-update-december-2023</guid><title>Supabase Beta December 2023</title><link>https://supabase.com/blog/beta-update-december-2023</link>