When deploying a backend application built with Bun and Elysia, along with a PostgreSQL database, the best option for price and performance depends on your specific requirements, such as expected traffic, scalability, and ease of management. Below are some of the best options for deploying your stack:
1. Fly.io
Why Fly.io?
Fly.io is a great choice for deploying full-stack applications close to your users, as it allows you to deploy your app to multiple regions globally.
It supports running lightweight applications like those built with Bun and Elysia.
Fly.io also provides managed PostgreSQL as a service, which integrates seamlessly with your app.
Pricing:
Free tier includes 3 shared-CPU-1x instances with 256MB RAM and 160GB outbound data transfer.
Paid plans start at $2/month for additional resources.
Managed PostgreSQL starts at $1.50/month for small databases.
Performance:
Excellent for low-latency applications due to its edge deployment model.
Bun's fast runtime pairs well with Fly.io's lightweight VMs.
Recommendation:
Use Fly.io for both your Bun + Elysia backend and PostgreSQL database for simplicity and cost-effectiveness.
2. Railway
Why Railway?
Railway is a developer-friendly platform that makes deploying applications and databases extremely simple.
It supports deploying Bun applications out of the box and offers managed PostgreSQL.
Pricing:
Free tier includes $5 of usage per month (sufficient for small apps and databases).
Paid plans start at $10/month for additional usage.
Performance:
Railway provides good performance for small to medium-scale applications.
It automatically scales your app and database as needed.
Recommendation:
Use Railway if you want a simple, no-ops deployment experience with minimal configuration.
Vercel is optimized for serverless deployments and works well with Bun for backend APIs.
Supabase is a managed PostgreSQL service with additional features like authentication, real-time subscriptions, and storage.
Pricing:
Vercel: Free tier includes 100GB bandwidth and 125 execution hours/month.
Supabase: Free tier includes 500MB database storage and 2GB bandwidth.
Paid plans for both start at $20/month.
Performance:
Vercel's serverless model ensures high performance and scalability for your backend.
Supabase provides a robust and scalable PostgreSQL database.
Recommendation:
Use this combination if you want a modern, serverless architecture with additional features like real-time updates and authentication.
4. DigitalOcean App Platform
Why DigitalOcean?
DigitalOcean App Platform is a PaaS solution that supports deploying Bun applications and offers managed PostgreSQL.
It provides a balance between simplicity and control.
Pricing:
Starter plan: $5/month for the app.
Managed PostgreSQL starts at $15/month.
Free trial available.
Performance:
Good performance for small to medium-scale applications.
More control over resources compared to fully serverless platforms.
Recommendation:
Use DigitalOcean if you want more control over your infrastructure while still benefiting from managed services.
5. AWS (EC2 or Fargate) + RDS
Why AWS?
AWS offers the most flexibility and scalability for deploying applications and databases.
You can deploy your Bun + Elysia app on EC2 or Fargate (serverless containers) and use RDS for managed PostgreSQL.
Pricing:
EC2: Starts at ~$3.50/month for t4g.micro instances (free tier available for 12 months).
RDS: Starts at ~$15/month for managed PostgreSQL.
Fargate: Pay-per-use pricing.
Performance:
High performance and scalability, but requires more setup and management.
Best suited for production-grade applications with high traffic.
Recommendation:
Use AWS if you need enterprise-grade scalability and are comfortable managing infrastructure.
Summary of Recommendations:
Platform
Best For
Free Tier?
Starting Price (Paid)
Fly.io
Edge deployments, low-latency
Yes
$2/month
Railway
Simplicity, no-ops deployment
Yes
$10/month
Vercel + Supabase
Serverless, modern stack
Yes
$20/month
DigitalOcean
Balance of control and simplicity
No
$20/month
AWS
Enterprise-grade scalability
Yes
~$18/month
Best Option for Price and Performance:
Fly.io is the best option for most use cases due to its low cost, excellent performance, and built-in support for both backend apps and PostgreSQL.
If you prioritize simplicity, Railway is a close second.
For serverless and modern architecture, go with Vercel + Supabase.
User
I already have a VPS on OVH, can i use it?
Assistant
Yes, you can absolutely use your existing VPS on OVH to deploy your backend application built with Bun and Elysia, along with a PostgreSQL database. Using your VPS can be a cost-effective solution since you're already paying for it, and it gives you full control over your infrastructure. However, it will require more manual setup and management compared to managed platforms like Fly.io or Railway.
Here’s a step-by-step guide to deploying your application on your OVH VPS:
1. Prepare Your VPS
Update the system:
Make sure your VPS is up-to-date with the latest security patches and software:
bash
sudo apt update && sudo apt upgrade -y
Install necessary tools:
Install essential tools like curl, git, and build-essential:
bash
sudo apt install curl git build-essential -y
2. Install Bun
Download and install Bun:
Follow the official Bun installation instructions:
bash
curl -fsSL https://bun.sh/install | bash
After installation, ensure Bun is available in your shell:
bash
export PATH="$HOME/.bun/bin:$PATH"
Verify installation:
bash
bun --version
3. Set Up Your Backend Application
Clone your application:
Clone your Bun + Elysia application repository to the VPS:
bash
git clone https://github.com/your-repo.git
cd your-repo
Install dependencies:
Use Bun to install your dependencies:
bash
bun install
Test your application:
Run your application locally to ensure it works:
bash
User
ok let's start with a docker in my vps
Assistant
Great choice! Using Docker on your VPS simplifies deployment and makes it easier to manage your application and its dependencies. Below is a step-by-step guide to deploying your Bun + Elysia backend and PostgreSQL database using Docker.
Follow the official Docker installation guide: Install Docker
Verify Docker Installation:
bash
docker --version
docker-compose --version
2. Create a Project Directory
Create a directory on your VPS to store your application and Docker configuration files.
bash
mkdir ~/bun-elysia-app
cd ~/bun-elysia-app
3. Write a Dockerfile for Your Bun + Elysia App
Create a Dockerfile to containerize your Bun application.
bash
nano Dockerfile
Add the following content to the Dockerfile:
dockerfile
# Use the official Bun image
FROM oven/bun:latest
# Set the working directory
WORKDIR /app
# Copy package files and install dependencies
COPY package.json bun.lockb ./
RUN bun install
# Copy the rest of the application code
COPY . .
# Expose the port your app runs on
EXPOSE 3000
# Start the application
CMD ["bun", "run", "start"]
4. Create a docker-compose.yml File
Use Docker Compose to manage both your Bun app and PostgreSQL database.
bash
User
how i can deploy a new version from github ?
Assistant
Deploying a new version of your application from GitHub to your Dockerized environment is straightforward. Below is a step-by-step guide to update your application with the latest code from GitHub and redeploy it on your VPS.
1. Pull the Latest Code from GitHub
Navigate to your project directory on the VPS where your docker-compose.yml and Dockerfile are located.
bash
cd ~/bun-elysia-app
Pull the latest changes from your GitHub repository:
bash
git pull origin main
Replace main with the branch you want to deploy (e.g., main, master, or develop).
2. Rebuild the Docker Image
After pulling the latest code, you need to rebuild the Docker image to include the new changes.
bash
docker-compose build
This will rebuild the app service defined in your docker-compose.yml file using the updated code.
3. Restart the Containers
Once the image is rebuilt, restart the containers to apply the changes:
bash
docker-compose up -d
The -d flag ensures the containers run in detached mode (in the background).
4. Verify the Deployment
Check if the updated application is running:
bash
docker ps
View the logs to ensure there are no errors:
bash
docker-compose logs -f
Test your application by accessing it in the browser or using curl:
bash
curl http://<your-vps-ip>:3000
5. Automate Deployment (Optional)
If you frequently deploy updates from GitHub, you can automate the process using a GitHub Webhook or a manual script.
Set up a database and user:
Log in to the PostgreSQL shell and create a database and user for your application:
bash
sudo -u postgres psql
Inside the PostgreSQL shell:
sql
CREATE DATABASE your_database_name;
CREATE USER your_user_name WITH PASSWORD 'your_password';
GRANT ALL PRIVILEGES ON DATABASE your_database_name TO your_user_name;
\q
Update your application’s database connection string:
Update your .env file or configuration to point to the PostgreSQL database:
If you’re using Caddy, HTTPS is automatically enabled.
8. Monitor and Maintain
Regularly update your application and dependencies.
Monitor your VPS resources (CPU, RAM, disk usage) using tools like htop or glances.
Set up automatic backups for your PostgreSQL database.
Advantages of Using Your OVH VPS
Cost-effective: You’re already paying for the VPS, so there’s no additional cost.
Full control: You have complete control over the server and can customize it as needed.
Scalability: You can upgrade your VPS resources (CPU, RAM, storage) as your application grows.
Disadvantages
Manual setup: Requires more effort to set up and maintain compared to managed platforms.
No auto-scaling: You’ll need to manually scale your VPS if traffic increases significantly.
Security responsibility: You’re responsible for securing the server (firewalls, updates, etc.).
Conclusion
Yes, you can use your OVH VPS to deploy your Bun + Elysia application with PostgreSQL. While it requires more manual setup, it’s a cost-effective and flexible solution. If you’re comfortable managing your own server, this is a great option!
nano docker-compose.yml
Add the following content:
yaml
version: "3.8"
services:
app:
build:
context: .
dockerfile: Dockerfile
container_name: bun-elysia-app
ports:
- "3000:3000" # Map port 3000 on the host to port 3000 in the container
environment:
- DATABASE_URL=postgresql://user:password@db:5432/mydatabase
depends_on:
- db
restart: always
db:
image: postgres:15
container_name: postgres-db
ports:
- "5432:5432" # Map port 5432 on the host to port 5432 in the container
environment:
POSTGRES_USER: user
POSTGRES_PASSWORD: password
POSTGRES_DB: mydatabase
volumes:
- postgres_data:/var/lib/postgresql/data
restart: always
volumes:
postgres_data:
5. Update Your Application Code
Ensure your application is configured to use the DATABASE_URL environment variable for the PostgreSQL connection. For example, in your .env file or configuration:
Ensure Docker starts on system reboot and your containers are automatically restarted:
Enable Docker to start on boot:
bash
sudo systemctl enable docker
Ensure your containers restart automatically:
Add restart: always to your docker-compose.yml file (already included in the example above).
11. Monitor and Maintain
Use docker stats to monitor resource usage of your containers.
Regularly update your application and rebuild the Docker image:
bash
docker-compose pull
docker-compose up -d --build
Conclusion
By using Docker on your OVH VPS, you’ve created a portable and easily manageable deployment for your Bun + Elysia backend and PostgreSQL database. This setup is scalable, easy to maintain, and works well for production environments. If you need further assistance, feel free to ask! 🚀
Option 1: Use a Deployment Script
Create a simple shell script to automate the steps above.
Create a script file:
bash
nano deploy.sh
Add the following content to the script:
bash
#!/bin/bash
echo "Pulling latest changes from GitHub..."
git pull origin main
echo "Rebuilding Docker image..."
docker-compose build
echo "Restarting containers..."
docker-compose up -d
echo "Deployment complete!"
Make the script executable:
bash
chmod +x deploy.sh
Run the script whenever you want to deploy a new version:
bash
./deploy.sh
Option 2: Use GitHub Webhooks
You can configure a GitHub Webhook to trigger the deployment process automatically whenever you push changes to your repository.
Install a Webhook Listener:
Use a tool like Webhook or create a custom webhook listener using Node.js, Python, or any other language.
Set Up the Webhook on GitHub:
Go to your GitHub repository.
Navigate to Settings > Webhooks > Add Webhook.
Enter your VPS's public URL (e.g., http://your-vps-ip:port) as the Payload URL.
Set the Content type to application/json.
Select the events you want to trigger the webhook (e.g., push).
Trigger the Deployment Script:
Configure the webhook listener to execute the deploy.sh script whenever a push event is received.
6. Roll Back to a Previous Version (Optional)
If something goes wrong with the new deployment, you can roll back to a previous version.
Check out the previous commit in your Git repository:
bash
git checkout <commit-hash>
Rebuild and restart the containers:
bash
docker-compose build
docker-compose up -d
Once the issue is resolved, return to the latest version:
bash
git checkout main
7. Clean Up Unused Docker Images (Optional)
Over time, Docker may accumulate unused images, which can take up disk space. Clean them up periodically:
bash
docker system prune -f
This will remove all unused containers, networks, and dangling images.
Summary of Commands
Here’s a quick summary of the commands you’ll use to deploy a new version from GitHub:
Pull the latest code:
bash
git pull origin main
Rebuild the Docker image:
bash
docker-compose build
Restart the containers:
bash
docker-compose up -d
(Optional) Automate with a script:
bash
./deploy.sh
By following these steps, you can easily deploy new versions of your application from GitHub to your Dockerized environment on your VPS. If you need help setting up automation or troubleshooting, feel free to ask! 🚀