apps/www/_blog/2024-12-05-supabase-queues.mdx
Today we're releasing Supabase Queues, for durable background task processing.
Supabase Queues is a Postgres-native, durable Message Queue with guaranteed delivery, improving the scalability and resiliency of your applications. It's designed to work seamlessly with the entire Supabase platform.
<div className="video-container mb-8"> <iframe className="w-full" src="https://www.youtube-nocookie.com/embed/UEwfaElBnZk" title="Introducing Supabase Cron" allow="accelerometer; autoplay; clipboard-write; encrypted-media; fullscreen; gyroscope; picture-in-picture; web-share" allowfullscreen /> </div> <Admonition>Supabase Queues is built on the pgmq extension by the team at Tembo.
It's a Supabase policy to support existing tools wherever possible, and the Tembo team have generously licensed their extension with the OSI-compatible PostgreSQL license.
We're very thankful to all the contributors and we look forward to working with the Tembo community.
</Admonition>pgmq database extension, create and manage Queues with any Postgres tooling.A Queue is used to manage and process tasks asynchronously. Typically, you use a Queue for long-running tasks to ensure that your application is robust.
For example, sending emails:
Let's say you want to send a welcome email to a user after they register on your website. Instead of sending the email immediately within the registration process - which could slow down the user's experience - you can place the “email task” into a Queue.A separate email service can then process this task, sending the email without affecting the registration flow. Even better: if the email bounces then the task could "reappear" in the Queue to get processed again.
In this scenario, Queues have improved your application's performance and resilience. Other cases include:
Queues can be created in the Dashboard or using SQL / database migrations.
<Admonition> For this section we'll focus on the Dashboard. You can refer to the [documentation](/docs/guides/queues/api) for SQL. </Admonition>There are several types of queues available:
Basic Queues: Simple, reliable queues with core functionality, ideal for most use cases. Messages are stored and processed within Postgres using standard transactional guarantees.
Unlogged Queues: Optimized for performance, unlogged queues avoid writing messages to disk, making them faster but less durable in case of a database crash. Suitable for transient or less critical workloads.
Partitioned Queues (coming soon): Designed for high throughput and scalability, partitioned queues distribute messages across multiple partitions, enabling parallel processing and more efficient load handling.
Supabase Queues are compatible with Postgres Row-Level Security (RLS), providing fine-grained access control to Messages. RLS Policies restrict which users or roles can insert, select, update, or delete messages in specific queues.
Once your Queue is configured you can begin adding Messages.
Let's create a new Basic Queue and add a Message.
If you're connecting to your Postgres database from a server, you can add messages using SQL from any Postgres client:
select * from pgmq.send(
queue_name => 'foo',
msg => '{ "hello": "world" }'
);
We have provided several functions that can be invoked from the client libraries if you need to add messages from a browser or mobile app. For example:
import { createClient } from '@supabase/supabase-js'
const url = 'SUPABASE_URL'
const key = 'SUPABASE_ANON_KEY'
const queues = createClient(url, key, {
db: { schema: 'pgmq_public' },
})
const { data, error } = await queues.rpc('send', {
queue_name: 'foo',
message: { hello: 'world' },
})
console.log('Message: ', data)
For security, this feature is disabled by default. There are several functions defined in the pgmq_public schema: send, send_batch, read, pop, archive, delete. You can find more details in the docs.
By default, Queues are only accessible via SQL and not exposed over the Supabase Data API. You can manage this in the Data API settings by exposing the pgmq_public schema. If you expose this schema, you must use Row Level Security (RLS) to manage access to your queues.
Beyond RLS, Postgres roles can be granted granular permissions to interact with Queues.
For example, the following permissions allow authenticated users can fully manipulate messages, whereas anonymous users can only retrieve messages:
The postgres and service_role roles receive permissions by default and should remain enabled for server-side operations.
You can use the Dashboard to inspect your Messages, including: status, number of retries, and payload. You can also postpone, archive, or delete messages at any time.
From the Queues page, just click on a Queue to inspect it. From there you can click on a message to see more details:
Supabase Queues runs entirely in your database so there's no additional costs to use the functionality.
We recommend you configure your database's Compute and Disk settings appropriately to support your Queues workload.
Using Postgres for your Queue system keeps your stack lean and familiar. You can add Messages to Queues within the same transaction that modifies related data, preventing inconsistencies and reducing the need for additional coordination. Postgres' robust indexing, JSONB support, and partitioning also enable scalable, high-performance queue management directly in your database.
By eliminating the need for separate infrastructure like RabbitMQ or Kafka, you reduce costs, streamline deployments, and leverage existing Postgres tools for monitoring, backups, and security. Features like Row-Level Security, rich SQL querying, and built-in archiving make Postgres a powerful, unified solution for both data storage and messaging.