Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Supacron

What?

This is an example repository to provide a stress test the following features of Supabase:

The aim of these tests are to determine the scale of using Jobs with Supabase in production. The tests have been split into 3 scenarios but they contain the same core concept: make a POST request to an endpoint on a minutely basis.

Requirements

You need an endpoint to test against. We recommend using Upstash Redis as it provides a REST API and allows you to store logic. When you create your Redis on Upstash, you will need to save the following:

  • UPSTASH_REDIS_REST_TOKEN
  • UPSTASH_REDIS_REST_URL to your database Vault.

Add them to your .env files using dotenvx

For Production

  1. npx @dotenvx/dotenvx set UPSTASH_REDIS_REST_TOKEN "Bearer TOKEN" -f supabase/.env.production
  2. In config.toml add upstash_redis_rest_token = "env(UPSTASH_REDIS_REST_TOKEN)" under the db.vault section

For Branches

  1. npx @dotenvx/dotenvx set UPSTASH_REDIS_REST_TOKEN "Bearer TOKEN" -f supabase/.env.preview
  2. In config.toml add UPSTASH_REDIS_REST_TOKEN = "env(UPSTASH_REDIS_REST_TOKEN)" under the edge_runtime.secrets section

Now you can run npx supabase@latest secrets set --env-file supabase/env.keys

Scenarios

pg_cron

Using the Cron integration, configure 1000 jobs to make a request to an endpoint every minute (where X is the number of jobs you want to run simultaneously)

The job is configured to call the Upstash endpoint directly with the Authorization header providing the UPSTASH_REDIS_REST_TOKEN.

pg_cron + Edge Functions

Using the Cron integration, configure 1000 jobs to call an Edge Function every minute

The job is configured to call an Edge Function with the Authorization header. The Edge Function invoked is the redis-counter function that makes a call to Upstash and needs the same secrets as above in your Edge Functions.

pg_cron + Single Chained Edge Functions

Using the Cron integration, configure a job to call an Edge Function every minute that chains to call X Edge Functions as background tasks

This job is configured to call an Edge Function. The Edge Function invoked is the (main-chain)[supacron/supabase/functions/main-chain/index.ts] which invokes 1000 Edge Functions to make calls to Upstash.

Monitoring

This repo uses two extensions and the monitoring of each of these can be done in their own schemas

pg_cron

The cron schema has two tables:

  1. jobs - This is all of the configured jobs for pg_cron to run, their frequency and the action they take
  2. job_run_details - This contains the outcome of all of the jobs that have been executed by pg_cron

pg_net

The net schema has two tables:

  1. _http_response - This contains responses from calls made by pg_net in the last 6 hours
  2. http_request_queue - This contains the queue of requests waiting to be made by pg_net

Configuring

  • You can customize some Postgres settings, such as max_parallel_workers using the Supabase CLI
  • Testing different values of X
  • Running on different instance sizes

Running

There are multiple ways to run through these scenarios.

Scripting

Using the scripts folder, you can apply each scenario using psql, running for a period of time and then stopping it with drop_jobs.sql. For example:

- `psql "$CONN_STRING" -f scripts/2_many_crons_one_function.sql`
- leave to run for X minutes
- `psql "$CONN_STRING" -f scripts/drop_jobs.sql`
- `psql "$CONN_STRING" -f scripts/get_job_details.sql`

Branching

Each scenario can be created as a separate branch within a Supabase project. Follow the Branching guide first and integrate with your github repo. For each branch, you will then need to add the scenario script as a migration. For example:

- `git checkout -b scenario/many_crons`
- `supabase migrations new many_crons`
- copy `scripts/1_many_crons.sql` into the new migration file
- commit the file and push, then open a new PR
- repeeat for each scenario and close the PRs when the tests are run.

About

Benching scheduled jobs with Supabase, Edge Functions and pg_cron

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages