How We Scaled a Rails Multi Tenant SaaS with PostgreSQL

Introduction

Scaling a Rails multi tenant SaaS with PostgreSQL is challenging. As your application grows, you must balance performance, maintainability, and tenant isolation. In this article, we’ll share how we scaled our SaaS product, the architectural decisions we made, and lessons learned.

Why Multi-Tenant SaaS Matters

A multi-tenant architecture allows a single application instance to serve multiple customers (tenants). Instead of managing separate applications for each client, all tenants share the same Rails app, while their data is securely isolated in PostgreSQL.

Benefits:

  • Cost-efficient – shared infrastructure reduces overhead.
  • Easier upgrades – a single codebase to maintain.
  • Faster onboarding – spin up new tenants quickly.

Architecture Choices: Rails + PostgreSQL for Multi Tenancy

When we started scaling, we had two main options:

1. Single Database with Shared Schema

All tenants use the same schema, with a tenant_id column to isolate data.

  • ✅ Simple and fast to implement.
  • ❌ Complex queries and risk of cross-tenant data leaks.

2. Single Database with Multiple Schemas (Our Choice)

Each tenant gets its own schema inside PostgreSQL.

  • ✅ Stronger data isolation.
  • ✅ Easier backups & migrations per tenant.
  • ❌ Slightly more complex setup.

Key takeaway: PostgreSQL schemas made our multi-tenant SaaS more scalable and secure while keeping the cost manageable.

Scaling Challenges and How We Solved Them

Tenant Isolation

  • Implemented apartment gem for schema-based multi-tenancy.
  • Used middleware to switch schemas dynamically based on subdomain.

Performance Bottlenecks

  • Added PostgreSQL indexes per tenant schema.
  • Used read replicas for heavy reporting queries.
  • Cached frequent queries with Redis to reduce DB load.

Deployment & Migration

  • Automated migrations per schema using custom Rake tasks.
  • CI/CD pipeline ensured zero downtime during upgrades.

Security & Compliance

  • Role-based access in Rails.
  • Row-level security (RLS) experiments for certain cases.
  • Tenant-specific encryption for sensitive data.

Scaling Challenges in a Rails Multi Tenant SaaS

  1. Choose the right multi-tenant strategy early – schema-based works best for mid to large SaaS.
  2. Use PostgreSQL features fully – indexes, partitions, and connection pooling.
  3. Optimize Rails performance – eager loading, background jobs (Sidekiq), and caching.
  4. Automate onboarding – scripts to create new schemas and seed default data.
  5. Monitor proactively – use tools like PgHero, New Relic, Skylight for query insights.

Lessons Learned Scaling SaaS with Rails

  • Don’t over-engineer early – start simple, then move to schemas once tenants grow.
  • Database is your bottleneck – invest in indexes, query tuning, and caching.
  • Tenant experience matters – keep onboarding smooth and ensure zero data leakage.

Conclusion

Scaling a multi-tenant SaaS product with Rails and PostgreSQL is achievable with the right strategy. By leveraging PostgreSQL schemas, Rails performance tuning, and automated migrations, we built a scalable, secure, and maintainable system that could support thousands of tenants with confidence.

If you’re building or scaling your SaaS, invest in architecture early—it pays off in the long run.