Back to articles
DevOps & Systems

Self-Hosting Supabase Local-First Sync Architecture on VPS for Offline-First Applications: Comprehensive Guide

Offline-first applications are increasingly popular due to their ability to function effectively without network connectivity, especially in mobile or unstable network environments....

DPTCloud Engineering5 min read
Self-Hosting Supabase Local-First Sync Architecture on VPS for Offline-First Applications: Comprehensive Guide

Offline-first applications are increasingly popular due to their ability to function effectively without network connectivity, especially in mobile or unstable network environments. To implement this architecture efficiently, self-hosting Supabase on a VPS with local-first sync capabilities is a powerful solution, enabling apps to store data locally and synchronize when connectivity is restored. This guide details the complete process of setting up, configuring, and optimizing Supabase on a VPS to support offline-first applications, including installation, database setup, data synchronization, security, and monitoring. This guide is suitable for developers and DevOps engineers seeking full control over their data infrastructure and scalability for product needs.

Overview of Local-First Architecture and Supabase

Offline-first applications allow users to access and modify data even without internet connectivity. Data is stored locally (on the device) and synchronized with the server when a connection is available. Supabase, an open-source backend platform, provides robust tools such as PostgreSQL, Auth, Storage, and Realtime, making it ideal for implementing this architecture. By self-hosting Supabase on a VPS, you gain full control over the system, including the database, API, and synchronization services.

Supabase uses local-first data synchronization mechanisms based on replication, conflict resolution, and real-time sync. When a user makes changes on a device, the data is updated locally and sent to the server when a connection is established. Supabase handles data conflicts and ensures consistency across devices. Self-hosting Supabase on a VPS allows you to customize these parameters according to specific application requirements.

Requirements and VPS Environment Setup

To deploy Supabase on a VPS, you need a high-performance server with at least 4GB RAM and 2 CPU cores, along with a minimum of 50GB NVMe storage. The VPS should be running Linux (Ubuntu 22.04 or Debian 11). You can use NVMe Cloud VPS for Docker, Nginx & Databases to obtain a stable and high-performance platform.

First, update the system and install necessary tools:

sudo apt update && sudo apt upgrade -y
sudo apt install -y docker.io docker-compose git curl

After installing Docker, you can deploy Supabase using Docker Compose. Create a new directory and create a `docker-compose.yml` file with the following content:

version: '3.8'
services:
  supabase:
    image: supabase/postgres:15
    container_name: supabase
    ports:
      - "5432:5432"
    environment:
      POSTGRES_DB: supabase
      POSTGRES_USER: supabase
      POSTGRES_PASSWORD: supabase
    volumes:
      - ./data:/var/lib/postgresql/data
    restart: unless-stopped
  supabase-auth:
    image: supabase/auth:2.0
    container_name: supabase-auth
    ports:
      - "8080:8080"
    environment:
      SUPABASE_URL: http://localhost:5432
      SUPABASE_JWT_SECRET: your_jwt_secret
    restart: unless-stopped

Run the following command to start Supabase:

docker-compose up -d

Configuring Data Synchronization and Security

To implement a local-first architecture, you need to configure Supabase to support data synchronization between devices and the server. Supabase provides tools like Replication and Realtime Sync. You can use Supabase Realtime to receive notifications when data changes on the server.

To secure data, you should configure HTTPS using Nginx. Create an Nginx configuration file at `/etc/nginx/sites-available/supabase`:

server {
    listen 80;
    server_name your-domain.com;
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl;
    server_name your-domain.com;

    ssl_certificate /etc/letsencrypt/live/your-domain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/your-domain.com/privkey.pem;

    location / {
        proxy_pass http://localhost:8080;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

Run the following command to install Let's Encrypt and enable SSL:

sudo apt install certbot
sudo certbot certonly --nginx -d your-domain.com

Monitoring and Maintenance

To ensure system stability, you should set up monitoring. Use Centralized Logging Management with ELK Stack to collect and analyze logs from Supabase and other services. The ELK Stack includes Elasticsearch, Logstash, and Kibana, allowing you to track performance, detect issues, and optimize the system.

Perform regular data backups using tools like `pg_dump` or `supabase backup`. You should also check for Supabase and Docker updates to ensure the system remains secure and optimized.

Troubleshooting and Verification

When encountering issues, check the Supabase logs using the command:

docker logs supabase

Ensure that services like PostgreSQL and Auth are running. Check network connectivity and firewall settings to ensure ports 80, 443, and 5432 are not blocked.

To verify the connection to Supabase, you can use the following cURL command:

curl -X GET "https://your-domain.com/auth/v1/verify" -H "Authorization: Bearer your_jwt_token"

If the connection is successful, you will receive a JSON response from Supabase.

References

DPTCloud Tier III Infrastructure

Recommended NVMe Cloud VPS Plans to Deploy This Setup

Enterprise RAID 10 NVMe SSDs, CMC & FPT District 9 uplinks with Anycast Anti-DDoS and 99.99% SLA.

View 22 VPS Plans
ECO · vn-02

VPS-2E

94.600 ₫ / mo

10% VAT included

  • CPU: 2 vCPU
  • RAM: 2 GB
  • SSD: 30 GB NVMe
ECO · vn-02Recommended

VPS-3E

170.500 ₫ / mo

10% VAT included

  • CPU: 2 vCPU
  • RAM: 4 GB
  • SSD: 50 GB NVMe
BASIC · vn-01Recommended

VPS-05

457.600 ₫ / mo

10% VAT included

  • CPU: 4 vCPU
  • RAM: 8 GB
  • SSD: 50 GB NVMe

Related Articles

View all
ZaloZalo SupportDiscord0384 753 478