SaaS Scaling Architecture: Monolith vs Microservices, Database Scaling, Caching & Monitoring
Monolith vs Microservices
Start with a monolith. This is the recommended approach for nearly every early-stage SaaS.
Monolith Advantages
- Simple to develop, deploy, and debug.
- Easy to refactor when requirements change.
- Lower operational overhead.
- Single database, no distributed transaction headaches.
When to Split
- One component needs to scale independently from the rest.
- Multiple teams are stepping on each other with conflicting deploys.
- Different parts have different reliability requirements.
- A component needs a fundamentally different tech stack.
The Modular Monolith
A middle ground: a single deployable with clear internal module boundaries. Each module has its own domain logic and data access, but they deploy together. Shopify runs this way at massive scale. You get most microservice benefits without the operational complexity.
Database Scaling
The database is usually your first bottleneck. Address it in stages:
Strategy 1: Indexing
- Index columns used in WHERE, JOIN, and ORDER BY clauses.
- Use composite indexes for multi-column queries.
- Monitor slow query logs to find what needs indexing.
Strategy 2: Read Replicas
- Route reporting, search, and analytics queries to read replicas.
- Keep all write operations on the primary database.
- Beware of replication lag — don't read from a replica immediately after a write.
Strategy 3: Connection Pooling
Tools like PgBouncer maintain a pool of reusable database connections. This prevents traffic spikes from opening too many connections and crashing the database. Essential once you have more than a handful of application servers.
Caching Layers
Application Cache
Use Redis or Memcached for computed results, sessions, and frequently queried objects. This is your first line of defense against database load.
CDN
Serve static assets from edge servers close to users. Cloudflare and CloudFront are the most common choices. This offloads bandwidth and improves load times globally.
Edge Computing
Run logic at the edge with Cloudflare Workers or Vercel Edge Functions. Useful for personalization, A/B testing, and geo-routing without round-tripping to your origin server.
Cache Invalidation Strategies
- TTL (Time-To-Live): Simple and reliable. Cache expires after a set duration.
- Write-through: Update the cache whenever the underlying data changes.
- Event-driven: Publish change events and let subscribers invalidate their own caches.
Background Jobs and Queues
Not everything belongs in the request-response cycle. Move slow or non-critical work to background jobs.
Common Patterns
- Fire and Forget: Sending emails, writing audit logs, tracking analytics events.
- Scheduled / Cron: Generating reports, processing billing, running cleanup tasks.
- Fan-out: Splitting one large task into many small parallel tasks.
Tools
BullMQ (Node.js), Sidekiq (Ruby), Celery (Python), AWS SQS (cloud-native). Pick what fits your stack.
Always implement retry with exponential backoff and a dead letter queue for jobs that repeatedly fail. Without these, a single bad job can block your entire queue or retry infinitely.
Monitoring and Alerting
Three Pillars of Observability
- Metrics: Numeric measurements over time — request rate, error rate, latency. Tools: Datadog, Prometheus, Grafana.
- Logs: Structured event records for debugging. Tools: ELK Stack, Grafana Loki, Logtail.
- Traces: Follow individual requests across services to find bottlenecks. Tools: Jaeger, Honeycomb, Sentry.
The 10x Rule
Design your infrastructure to handle 10x your current peak load. This gives you headroom for viral moments, press coverage, or seasonal spikes without scrambling to scale in a crisis.
Infrastructure as Code
Manage your infrastructure through code, not manual configuration:
- Terraform: Cloud-agnostic, declarative infrastructure definitions. The industry standard.
- Pulumi: Write infrastructure as code in TypeScript, Python, or Go — no new language to learn.
- SST: Purpose-built for serverless applications on AWS. Great developer experience.
Benefits
- Reproducibility: Spin up identical environments for staging, testing, and production.
- Auditability: Every infrastructure change is tracked in version control.
- Disaster Recovery: Rebuild your entire infrastructure from code if something goes catastrophically wrong.