Shipping a multi-tenant SaaS involves more than just writing code. You need to architect for shared resources, distinct customer data, and scalable operations. We've learned a lot running our own platforms, especially around keeping tenants separate, managing billing, handling background tasks, and dealing with inevitable issues.

Key takeaways

  • Tenant isolation is paramount for data security and stability.
  • Billing needs to be accurate, flexible, and automated.
  • Background job queues are essential for performance and reliability.
  • Incident management requires clear protocols and communication.

Tenant Isolation: The Bedrock of Multi-Tenancy

When multiple customers share the same application instance, keeping their data and configurations separate is non-negotiable. We've seen two main approaches:

Pros

  • Database per Tenant: Maximum isolation, easier backups for individual tenants, simpler schema changes per tenant if needed.
  • Schema per Tenant: Good isolation, shared database resources.
  • Shared Schema with Discriminator Column: Most common, cost-effective, but requires strict application-level checks.

Cons

  • Database per Tenant: Higher infrastructure cost, management complexity (many databases).
  • Schema per Tenant: Still some shared resource contention, management complexity.
  • Shared Schema: Higher risk of data leakage if queries aren't perfectly scoped, performance issues with large datasets.

For our SaaS products, including WaZappy, we primarily use a shared database with a tenant ID column. This is usually the most pragmatic balance. Every single database query, API call, and background job must be scoped to the requesting tenant's ID. A single mistake here can expose data across tenants.

From our projects: Implement strict middleware or decorators in your application framework to enforce tenant ID checks on all incoming requests and outgoing data. Automate tests that specifically try to access data from another tenant; these should always fail.

Billing: From Simple Subscriptions to Usage-Based

Billing for a SaaS isn't just about taking money; it's about accurately reflecting value provided and managing revenue. The complexity depends on your pricing model.

Subscription Management

For fixed monthly or annual plans, integrating with services like Stripe or Paddle simplifies things greatly. They handle payment processing, recurring billing, and dunning (failed payment recovery).

Usage-Based Billing

This is where it gets tricky. If you charge based on API calls, storage used, or features consumed, you need a robust metering system. This involves:

  1. Accurate Metering: Log every countable event reliably. For instance, if you charge per SMS sent via our Bulk SMS service, ensure each sent message is logged.
  2. Aggregation: Periodically aggregate usage data per tenant (e.g., daily or hourly).
  3. Rating: Apply your pricing rules to the aggregated usage.
  4. Invoicing: Generate invoices based on subscriptions and usage.

We've built custom billing modules for clients needing complex usage-based models, often integrating with their internal dashboards. The key is to make the metering as close to real-time as possible without impacting core application performance.

Queues: Keeping the Application Snappy

Running time-consuming tasks directly within a user's web request will lead to slow responses and timeouts. Background job queues are essential. Think of sending bulk emails, processing uploaded files, generating reports, or even sending WhatsApp messages via an API.

We use technologies like Redis with libraries like Sidekiq (Ruby) or Celery (Python) to manage these queues. The workflow is generally:

  1. Enqueue Job: When an action needs to happen in the background, create a job object and push it onto a queue. This can be triggered by a user action or a scheduled event.
  2. Worker Process: Dedicated worker processes constantly monitor the queue.
  3. Process Job: When a worker picks up a job, it performs the required task (e.g., send an email).
  4. Handle Failures: Implement retry mechanisms and dead-letter queues for jobs that consistently fail.

Configuration Example (Conceptual Sidekiq):

# config/initializers/sidekiq.rb
Sidekiq.configure_server do |config|
  config.redis = {
    url: ENV.fetch('REDIS_URL') { 'redis://localhost:6379/0' },
    # Add other Redis connection options if needed
  }
end

Sidekiq.configure_client do |config|
  config.redis = {
    url: ENV.fetch('REDIS_URL') { 'redis://localhost:6379/0' }
  }
end

Ensure your queue workers are scaled appropriately. If you have a sudden surge in jobs, your workers need to keep up, or jobs will back up, leading to delays for your users.

Incident Management: When Things Go Wrong

No system is perfect. You will have outages, bugs, and security incidents. Having a plan is crucial for minimising downtime and maintaining customer trust.

Detection and Alerting

Use monitoring tools (e.g., Prometheus, Datadog, Sentry) to track application performance, error rates, and infrastructure health. Set up alerts for critical thresholds.

Triage and Diagnosis

When an alert fires, quickly determine the scope and impact. Is it affecting one tenant or all? Is it a performance degradation or a complete outage? Have runbooks or checklists ready for common issues.

Communication

This is vital. For external incidents, communicate proactively with your customers. A status page is highly recommended. For internal incidents, ensure clear communication channels among your engineering and support teams.

Sample Incident Communication (Internal):

Subject: [INCIDENT] High Latency on User Auth Service (Severity 2)

Time: 2023-10-27 10:15 UTC

Impact: Users experiencing slow logins. Affecting approx 30% of requests.

Team On-Call: Backend Team Alpha

Current Status: Investigating. Suspect database connection pool exhaustion.

Next Update: In 30 minutes or sooner if major change.

Resolution and Post-mortem

Fix the issue, verify the resolution, and then conduct a post-mortem. Understand the root cause, what went well, what could be improved, and create action items to prevent recurrence. This is a continuous improvement loop.

Building Your Multi-Tenant SaaS

Developing a robust multi-tenant SaaS is a significant undertaking. It requires careful consideration of architecture, security, scalability, and operational processes. Whether you're building from scratch or looking to enhance an existing application, focusing on these core areas—tenant isolation, solid billing, efficient queues, and incident preparedness—will set you on the right path. If you're looking for expert help in SaaS product development, our team at Infrared Techno has the experience to guide you.