Seamless Integration: Enhancing Backend Services with Traefik and Supabase
Replace Kong with Traefik in your Supabase stack for scalable, secure, cloud-native routing with automatic SSL.

Introduction
Discover the transformative integration of Supabase, the open-source Firebase alternative, with Traefik, a modern reverse proxy and load balancer. This guide offers a concise exploration into replacing Kong with Traefik in your Supabase setup, enhancing scalability, security, and ease of management. Ideal for developers and DevOps enthusiasts, we'll navigate through the necessary changes, aiming to streamline your backend services for the modern web landscape.
Traefik: A Modern Reverse Proxy and Load Balancer
Traefik is swiftly gaining popularity as a modern, cloud-native reverse proxy and load balancer. It excels in dynamic environments often found in microservices architectures. Unlike traditional reverse proxies, Traefik is designed for ease of configuration and automatic service discovery. This makes it particularly well-suited for containerized applications managed with orchestrators like Docker.
Key Features of Traefik
- Automatic Service Discovery: Traefik automatically detects changes in your service configurations.
- Dynamic Configuration: No need for manual restarts; Traefik updates its configuration in real-time.
- Simplified SSL/TLS Management: Traefik can automatically obtain and renew SSL certificates using Let's Encrypt.
- Load Balancing: Efficiently distributes traffic among different services and servers.
- High Availability: Ensures that your services are always accessible, even if one or more servers fail.
Running Traefik with Docker Compose
To run Traefik with Docker Compose, you need a docker-compose.yml file that defines Traefik as a service. Here's a basic example to get you started:
```yaml
version: '3'
services:
traefik:
image: traefik:v2.10
command:
- "--log.level=DEBUG"
- "--api.insecure=true"
- "--providers.docker=true"
- "--entryPoints.web.address=:80"
ports: - "80:80"
- "8080:8080"
volumes: - "/var/run/docker.sock:/var/run/docker.sock"
```
This basic setup is enough to get Traefik running and start experimenting with its capabilities. It can be expanded with additional settings for SSL/TLS, more complex routing rules, middleware, and integration with other services in your environment.
Supabase: Revolutionizing Backend Development
Supabase is an open-source platform that provides developers with a suite of tools to build and manage their backend infrastructure effectively. It's often referred to as an alternative to Google's Firebase, offering similar functionalities but with the added advantage of being open-source and more customizable. Supabase primarily focuses on providing database storage, real-time subscriptions, authentication services, instant APIs over Postgres, and blob storage capabilities.
Key Features of Supabase
- Database Storage: Built on top of PostgreSQL, it offers robust, scalable, and flexible database storage options.
- Real-time Subscriptions: Enables real-time functionality, allowing applications to update instantly as database changes occur.
- Authentication: Offers simple yet secure user authentication, including options for magic links, OAuth, and more.
- Instant APIs: Automatically generates APIs based on your database schema, speeding up backend development.
- Blob Storage: Supports the management and storage of large binary objects, such as images, audio files, and other multimedia content.
Deploying Supabase locally
```
git clone https://github.com/supabase/supabase
cd supabase/docker
docker compose up
```
This command starts all the necessary services for a complete Supabase environment, including the Postgres database, Auth services, and Realtime server.
Integrating Supabase with Traefik
Integrating Supabase with Traefik involves configuring Traefik to handle the routing and load balancing for Supabase services. This integration not only brings the robust features of Traefik into play but also optimizes the performance and security of your Supabase deployment.
By default, Supabase uses Kong as its API gateway. To switch to Traefik, remove or disable Kong in your Docker Compose setup and introduce Traefik as the primary reverse proxy. Each Supabase service (PostgREST, Realtime, GoTrue) then needs labels to define routing rules, for example on the storage service:
```yaml
labels:
- "traefik.http.routers.storage.rule=Host(
${SUPABASE_HOST}) && PathPrefix(/storage/v1)" - "traefik.http.middlewares.storage-rx.replacepathregex.regex=^/storage/v1(.*)"
- "traefik.http.middlewares.storage-rx.replacepathregex.replacement=$$1"
- "traefik.http.services.storage.loadbalancer.server.port=5000"
```
Supabase Studio doesn't ship with credentials for access, so add your own authentication middleware with Traefik:
```yaml
labels:
- "traefik.http.routers.supabase.middlewares=auth-studio"
- "traefik.http.middlewares.auth-studio.basicauth.users=${SUPABASE_BASIC_AUTH_CREDS}"
- "traefik.http.middlewares.auth-studio.basicauth.removeheader=true"
```
Finally, use Traefik's Let's Encrypt integration to enable HTTPS automatically and redirect all HTTP traffic to HTTPS.
Benefits of This Integration
- Improved Load Balancing: Traefik efficiently distributes traffic, enhancing the performance of your services.
- Enhanced Security: With automatic SSL/TLS, your Supabase setup gains an added layer of security.
- Simplified Configuration: Traefik's dynamic configuration and service discovery make managing your services easier.
- Scalability: As your application grows, Traefik can easily scale to meet increased demand.
Testing and Validation
After configuring Traefik with your Supabase services, verify that all services (PostgREST, Realtime, GoTrue) are accessible through Traefik, confirm HTTPS is set up correctly with HTTP→HTTPS redirects, simulate different traffic loads to test load-balancing efficiency, test real-time subscriptions, and validate authentication flows through GoTrue.
Consider also enabling Traefik's dashboard (secured), custom middleware for rate limiting and headers, detailed logging with a monitoring tool like Prometheus, optimized Docker networking, and regular database backups.
Conclusion
This combination offers a compelling solution for managing backend services with efficiency, scalability, and security. Traefik, with its dynamic routing and load balancing, complements the robust features of Supabase, enhancing the overall performance and manageability of your backend infrastructure — a significant step towards a more streamlined, cloud-native approach to backend management.