SaaSNext.jsPostgreSQLArchitectureWeb Development

Scalable SaaS Architecture with Next.js 15 and PostgreSQL: A Production Blueprint

Most SaaS architecture guides are either too abstract or too toy-project-simple. This one covers the decisions that matter for a real multi-tenant product: database design, auth, billing integration, rate limiting, and deployment.

P
Prashant Mishra
Founder & AI Engineer
13 min read
Back to Articles
Scalable SaaS Architecture with Next.js 15 and PostgreSQL: A Production Blueprint

Building a SaaS product that works for five users in a demo is easy. Building one that handles thousands of tenants with different plan tiers, accurate billing, predictable performance under load, and a zero-downtime deployment story is a genuinely hard engineering problem. This blueprint covers the key architectural decisions we have made building production SaaS at Innovativus.

Multi-Tenancy: Row-Level Security in PostgreSQL

The first and most fundamental decision in SaaS architecture is your multi-tenancy model. The three options are: separate databases per tenant (maximum isolation, high operational complexity), separate schemas per tenant (good isolation, manageable complexity), and shared tables with a tenant_id column protected by Row-Level Security (good performance, simpler operations, less isolation).

For most SaaS products in the startup phase, shared tables with PostgreSQL Row-Level Security (RLS) is the pragmatic choice. RLS policies enforce tenant isolation at the database level, not just in application code. Every query runs through the policy automatically, which eliminates the class of bugs where application code forgets to filter by tenant_id.

The essential pattern: every table has a tenant_id column. You create an RLS policy that checks tenant_id = current_setting('app.current_tenant_id'). Your application sets this session variable immediately after establishing a database connection. Queries automatically return only rows belonging to the current tenant.

Authentication Architecture

In 2026, rolling your own auth from scratch is almost never the right choice. The security surface area is large and the maintenance burden is significant. Use a proven auth library or service.

For Next.js projects, Auth.js (formerly NextAuth.js) remains the most popular open-source option and supports a wide range of OAuth providers, email magic links, and credential-based auth. For teams that want managed auth infrastructure, Clerk offers a polished UI and developer experience at a reasonable price for most startup scale ranges.

The key things to get right regardless of which library you use: session tokens should be short-lived with sliding expiration, refresh tokens should be rotated on use, and all authentication events (login, logout, password change, MFA enrollment) should be logged with IP address and user agent for security auditing.

The Database Schema Foundation

A SaaS product's database schema revolves around a small set of core tables that everything else references. At minimum:

  • tenants (or organizations): id, name, plan_tier, billing_customer_id, created_at, status
  • users: id, tenant_id, email, role, created_at, last_seen_at
  • subscriptions: id, tenant_id, plan_id, status, current_period_start, current_period_end, billing_provider_id
  • usage_events: id, tenant_id, event_type, quantity, recorded_at (for metered billing)

Design your schema for query patterns, not just data storage. If your most common query is "get all active subscriptions for a tenant," add an index on (tenant_id, status). Over-indexing wastes write performance; under-indexing kills read performance at scale.

Billing Integration: Stripe or Cashfree

For billing, the choice for Indian SaaS products is typically between Stripe (best for international customers, widely documented) and Cashfree (better for Indian payment methods, UPI, domestic cards). Many products support both.

The critical engineering requirement is that your billing state should be the subscription status in your billing provider's webhook, not in your database. Your database records the billing provider's customer and subscription IDs. When a payment succeeds or fails, your billing provider sends a webhook. Your webhook handler updates the subscription status in your database. This event-driven model keeps your system in sync even if your application was down during a billing event.

Always verify webhook signatures before processing. Both Stripe and Cashfree provide signature verification mechanisms. An unverified webhook endpoint is a significant security vulnerability.

Rate Limiting

Rate limiting protects your application from abuse and ensures fair resource distribution across tenants. The two axes to rate limit are: per-user (to prevent any single user from hammering endpoints) and per-tenant (to enforce plan tier limits).

For Next.js deployed on Vercel or a similar edge-capable platform, implement rate limiting at the middleware layer using a fast key-value store like Redis (via Upstash for serverless-compatible Redis). A sliding window counter per user per endpoint, stored in Redis with a TTL equal to your window size, is a robust and fast implementation.

Deployment Architecture

For most early-stage SaaS products, Vercel for the Next.js application and a managed PostgreSQL provider (Neon, Supabase, or AWS RDS) is a productive and cost-effective setup. The tradeoff is vendor lock-in and costs that scale with usage rather than with instance size. At sufficient scale, migrating to a dedicated cloud setup on AWS or GCP becomes cost-effective.

Zero-downtime deployments are a requirement from day one. Vercel handles this for the application layer. For database migrations, use a migration tool like Drizzle Kit or Prisma Migrate that runs migrations as separate deployment steps, never as part of application startup.

We build SaaS applications at Innovativus using patterns very similar to what is described here. If you are planning a SaaS product and want to discuss architecture before writing any code, we are happy to help you think it through.

PM

Written by

Prashant Mishra

Founder & MD, Innovativus Technologies · Creator of Pacibook

Technologist and AI engineer with a B.Tech in CSE (AI & ML) from VIT Bhopal. Builds production-grade AI applications, RAG pipelines, and digital publishing platforms from New Delhi, India.

Share this article to support us.