Agency Cost Optimization: How to Run 200 Isolated PocketBase Applications on a Single 4GB RAM VPS Using Kamal 2
The Infrastructure Dilemma for Modern Digital Agencies
Digital agencies, software houses, and indie development shops face a recurring financial bottleneck: infrastructure overhead. When managing dozens or hundreds of client projects—ranging from Minimum Viable Products (MVPs) and portfolio sites to internal tools and content management backends—the traditional approach dictates provisioning separate cloud instances or relying on expensive Platform-as-a-Service (PaaS) providers.
While separating client environments is non-negotiable for security and stability, assigning a dedicated $5 to $10 virtual private server (VPS) or managed database to every single low-traffic application quickly erodes profit margins. For an agency managing 200 small-to-medium client apps, monthly infrastructure bills can easily skyrocket to $1,000–$2,000.
What if you could securely host all 200 of those isolated applications on a single, high-performance VPS with only 4GB of RAM, costing as little as $20 to $40 per month? Thanks to the modern combination of PocketBase and Kamal 2, this extreme cost optimization is not only possible—it is production-ready.
The Ingredients: Why PocketBase and Kamal 2?
Achieving this level of density requires a radical departure from heavy, resource-hungry tech stacks. We achieve it by combining a hyper-efficient backend with a next-generation deployment tool.
1. PocketBase: The Ultimate Lightweight Backend
PocketBase is an open-source, single-file Go backend consisting of an embedded SQLite database, real-time subscriptions, user authentication, and an intuitive admin dashboard. Unlike traditional stacks (such as Node.js or Python paired with a separate PostgreSQL instance) which can consume 150MB to 500MB of idle RAM per instance, an idle PocketBase instance consumes roughly 15MB to 20MB of RAM.
Because SQLite reads and writes directly to a single file on the local disk, there is no separate database process consuming memory. This micro-footprint is the fundamental secret behind fitting hundreds of apps on minimal hardware.
2. Kamal 2: Zero-Downtime, Zero-PaaS Orchestration
Developed by 37signals, Kamal 2 is a lightweight, configuration-driven deployment tool that brings PaaS-like simplicity to standard, bare-metal or cloud VPS hosting. Kamal leverages Docker to containerize applications, but unlike Kubernetes, it introduces almost zero memory overhead.
Kamal 2 introduces a built-in reverse proxy (Kamal Proxy) that handles automated SSL certificate provisioning via Let's Encrypt, intelligent request routing, and zero-downtime rolling deploys. It allows you to manage multiple distinct applications on a single server using simple configuration files, eliminating the need for complex control panels or heavy orchestration layers.
The Math Behind the Density
Let's break down the technical feasibility of hosting 200 instances on a 4GB RAM VPS. It comes down to understanding idle versus peak resource utilization.
- Operating System Overhead: A clean Linux distribution (like Ubuntu Server) consumes roughly 400MB to 500MB of RAM at idle.
- Kamal Proxy Overhead: The core routing infrastructure of Kamal 2 requires less than 50MB of RAM.
- PocketBase Idle Consumption: 200 instances × 15MB per instance = 3,000MB (3GB) of RAM.
Total Idle RAM Consumption: ~3.55GB. This leaves approximately 450MB of headroom on a 4GB system.
Crucial Architectural Note: In an agency ecosystem, 80% to 90% of client applications are low-traffic or intermittent-traffic systems. They are not receiving requests simultaneously. Because SQLite and Go handle concurrent requests efficiently through connection pooling and Go-routines, the operating system's virtual memory subsystem and swap space can comfortably handle minor traffic spikes without collapsing.
Step-by-Step Architecture and Implementation
To successfully deploy this architecture without individual applications interfering with one another, we use a structured configuration pattern with Kamal 2.
Step 1: Preparing the VPS and Swap Space
Before launching containers, configuring a robust swap file is mandatory. Swap acts as an emergency safety valve for RAM spikes, preventing the Linux kernel's Out-Of-Memory (OOM) killer from shutting down your client databases.
- Provision a high-quality cloud VPS with 4GB RAM, multiple vCPUs, and NVMe SSD storage (brands like Hetzner, DigitalOcean, or Linode work perfectly).
- Allocate a 4GB or 8GB swap file on the NVMe drive to absorb unexpected usage bursts.
Step 2: Structuring the Kamal 2 Configuration
Each client application gets its own directory containing a lightweight Dockerfile and a customized config/deploy.yml file. Below is an example of how to structure a production-grade Kamal 2 deployment configuration for a PocketBase instance:
service: client-app-123
image: your-registry/pocketbase-app:latest
servers:
web:
- 192.168.1.100
proxy:
host: app123.youragencyclient.com
port: 8090
ssl: true
env:
clear:
DATA_DIR: /pb_data
volumes:
- /var/lib/agency/app123:/pb_dataBy binding each application to a distinct host volume (e.g., /var/lib/agency/app123), you ensure complete data isolation. If Client A terminates their contract, you can safely delete their volume and container without touching Client B's database.
Step 3: Automated Multi-Tenant Deployment
Deploying or updating an application is reduced to a single command executed from your local machine or a CI/CD pipeline:
kamal deploy
Kamal 2 automatically connects to the server via SSH, pulls the container image, provisions the Let's Encrypt SSL certificate for app123.youragencyclient.com, updates the Kamal Proxy routing table, boots the new container, and shuts down the old one with zero downtime.
Best Practices for Managing a High-Density VPS
While this architecture is incredibly cost-efficient, operating at this density requires strict adherence to system administration best practices:
- Automate Backups: Because PocketBase relies on SQLite, backups are straightforward. Run a daily cron job on the host machine to safely copy the
pb_datadirectories or utilize PocketBase's native S3 backup integration to stream encrypted database snapshots directly to Cloudflare R2 or AWS S3. - Implement Aggressive Resource Limits: Prevent a single rogue client script from crashing the entire server. You can configure Kamal to pass CPU and memory constraints directly to Docker:
boot:
options:
memory: "128m"
memory-swap: "256m"- Centralized Logging and Monitoring: Do not manually log into the server to debug. Use lightweight agents like Prometheus, Grafana, or Glances to monitor memory consumption, and aggregate container logs using a centralized tool like Vector or Grafana Loki.
Conclusion: Reclaim Your Profit Margins
By leveraging the ultra-low footprint of PocketBase and the seamless orchestration of Kamal 2, digital agencies can fundamentally change the economics of software development. Moving from a fragmented infrastructure model to a centralized, high-density architecture turns DevOps from a cost center into a competitive advantage.
Stop overpaying for idle cloud computing resources. Embrace containerized SQLite execution, automate with Kamal 2, and scale your agency sustainably.
