Skip to main content

Command Palette

Search for a command to run...

How to Architect Multi-Tenant SaaS Backends in 2025 (PostgreSQL + Node.js)

Designing for scale isn’t just about performance, it’s about architecture that makes growth easy.

Updated
3 min read
How to Architect Multi-Tenant SaaS Backends in 2025 (PostgreSQL + Node.js)
F

Principal Technical Consultant at GeekyAnts.

Bootstrapping our own Data Centre services.

I lead the development and management of innovative software products and frameworks at GeekyAnts, leveraging a wide range of technologies including OpenStack, Postgres, MySQL, GraphQL, Docker, Redis, API Gateway, Dapr, NodeJS, NextJS, and Laravel (PHP).

With over 9 years of hands-on experience, I specialize in agile software development, CI/CD implementation, security, scaling, design, architecture, and cloud infrastructure. My expertise extends to Metal as a Service (MaaS), Unattended OS Installation, OpenStack Cloud, Data Centre Automation & Management, and proficiency in utilizing tools like OpenNebula, Firecracker, FirecrackerContainerD, Qemu, and OpenVSwitch.

I guide and mentor a team of engineers, ensuring we meet our goals while fostering strong relationships with internal and external stakeholders. I contribute to various open-source projects on GitHub and share industry and technology insights on my blog at blog.faizahmed.in.

I hold an Engineer's Degree in Computer Science and Engineering from Raj Kumar Goel Engineering College and have multiple relevant certifications showcased on my LinkedIn skill badges.

In the world of SaaS, designing for scale starts with multi-tenancy done right.

Why Multi-Tenant Architecture Matters

SaaS is all about shared infrastructure. Whether you’re serving 5 customers or 5,000, you need to:

  • Isolate customer data securely

  • Scale efficiently without ballooning costs

  • Keep deployments sane

Multi-tenancy lets you do all of that - when designed correctly.

Multi-Tenancy Models: Which One?

ModelDescriptionProsCons
Shared DB, Shared TablesAll tenants share the same schemaEasiest to manageRisk of accidental leakage, hard to scale
Shared DB, Separate SchemasOne DB, one schema per tenantGood balance of isolation and performanceSlightly more infra overhead
Separate DB per TenantOne DB per tenantMax isolation and flexibilityComplex infra, costly at scale

We’ll focus on schema-based multi-tenancy, it’s a sweet spot for SaaS products targeting SMBs to mid-market.

PostgreSQL Multi-Tenancy Strategy

Schema-Per-Tenant

Each customer gets their own schema:

public.users        → shared
tenant_abc.orders   → tenant-specific
tenant_xyz.orders   → tenant-specific

Security with RLS

Use Row-Level Security (RLS) in public/shared tables:

CREATE POLICY tenant_isolation
  ON public.users
  USING (tenant_id = current_setting('app.current_tenant'));

And set this in every DB connection via middleware:

SET app.current_tenant = 'tenant_abc';

Connection Pooling Tips —

  • Use pgbouncer in transaction pooling mode

  • For AWS, consider RDS Proxy with connection pinning (for long sessions)

Backend Design with Node.js

Middleware-Based Tenant Resolution

app.use((req, res, next) => {
  const subdomain = req.hostname.split('.')[0];
  req.tenantId = resolveTenant(subdomain); // resolve to `tenant_abc`
  next();
});

Request-Scoped DB Access

Using a dbForTenant(tenantId) helper to inject the right connection:

const db = dbForTenant(req.tenantId);
const orders = await db('orders').select('*');

Or with ORMs like Prisma or MikroORM:

  • Generate client instances dynamically

  • Use schema switch or connection pool keying

Infra & Operational Considerations

  • Migrations per schema using tools like node-pg-migrate

  • Backup strategies to snapshot all schemas

  • Rate limiting by tenant key

  • Audit logging per tenant using middleware hooks

End-to-End Flow

Real-World Lessons

  • Avoid hardcoding tenant logic — use context-injected data

  • Monitor schema bloat over time

  • Start with fewer shared tables — easier to migrate than split later

  • Automate tenant provisioning (create schema + seed data)

Conclusion

Multi-tenancy isn’t a feature, it’s a foundation.

With PostgreSQL schemas and clean Node.js abstractions, you can scale from one tenant to thousands without rewriting your stack.

Got questions about your SaaS backend?

Drop a comment or DM me.

Always happy to help troubleshoot, scale, or design multi-tenant platforms with you.

Scaling JavaScript & Node.js

Part 6 of 18

Learn how to build scalable, high-performance applications in JavaScript and Node.js — this series breaks down real-world solutions for handling large datasets, improving API speed, and writing production-grade backend code.

Up next

Packaging Node.js Libraries the Right Way: ESM, CommonJS, and Bundlers in 2025

A practical guide for library authors: How to support both module systems, avoid interop headaches, and publish packages that just work.