You are on page 1of 33

Building a

Multi Tenant
SaaS Architecture
on AWS

By
page. 1

Multi tenant Architecture SaaS


Application on AWS Introduction
Multi tenant Architecture is a Sof- SaaS applications are the new normal
tware Architecture that runs multiple nowadays, and software providers
single instances of the software on a are looking to transform their web
single physical server, which serves applications into a Software As a Ser-
multiple tenants. Multitenancy is ano- vice application. For this, the only
ther common term for this practice in solution is to build a Multi tenant
which multiple tenants shared the Architecture SaaS Application.
memory of a server, dynamically allo-
cated and cleaned up as needed.

The next points are what we will explore in a Multi tenant architecture for your SaaS
application, and my contributions will be:

Index
1. Multi tenant architecture benefits
2. SaaS Technology stack for an Architecture on AWS
3. Which Multi tenant architecture suits better for your SaaS Application on AWS?
4. Types of Multi tenant SaaS architectures: Application Layer
5. Types of Multi tenant SaaS Architecture: Database layer
6. Application Code Changes
7. In a nutshell for Python Django
8. Wildcard DNS Subdomain – URL based SaaS application.
9. Web Server Setup – Nginx configuration to support Multi tenant SaaS applica-
tions.
10. Follow the 12-factor methodology framework or die trying!
11. What are the Multi-tenant SaaS architecture best practices?
12. Conclusions

www.clickittech.com info@clickittech.com +52 (844) 141-8485


page. 2

Have you ever wondered how Slack, If you know about SaaS architectu-
Salesforce, AWS (Amazon Web Services), re and multi-tenant, you are proba-
and Zendesk can serve multiple organiza- bly covering 70% of the web applica-
tions? Does each one have its unique and tion architecture landscape that
custom cloud software per customer? For would be available in the future.
example, have you ever noticed that, on
Slack, you have your own URL ‘com- This research is intended to showcase
pany.slack.com’? You probably thought an overview of the strategies, challen-
that in the background, they created a ges and constraints that DevOps and
particular environment for your organiza- Software Developers are likely to face
tion, –application or codebase–, and thin- when architecting a SaaS multi-tenant
king that slack customers have their own application.
server/app environment. If this is you, Multi tenant
Architecture page 13
you might have assumed that they have a
repeatable process to run thousands of
apps across all their customers. Well,
definitely No. And the real solution is a
Multi tenant architecture SaaS appli-
cation on AWS.

I will start with this impressive fact: 70%


of all web applications are considered
SaaS applications according to IDC Re-
search.

70% of all Web Apps are SaaS

www.clickittech.com info@clickittech.com +52 (844) 141-8485


page. 3

Tenant / Organization
Tenant = Organization / Client / Customer
User = a user inside a tenant. A tenant/Or-
ganization can have multiple, and even,
thousands of users. Users Users Users

1 Multi tenant architecture benefits:


The adoption of a Multi tenant archi- repositories you would have per cus-
tecture approach will bring extensive tomer? At least 3-4 branches per cus-
valuable benefits for your SaaS appli- tomer and this would be a lot of over-
cation. head, misaligned code releases. Even
worse, visualize the process of deplo-
Let’s dive into the next contributions: ying your code to the entire farm of
tenants; it is extremely complicated.
a) A Reduction of server Infras- This is unviable and time-consuming.
tructure costs utilizing a Multi With a multi tenant SaaS architec-
tenant architecture strategy. Ins- ture, you avoid this type of conflict,
tead of creating a SaaS applica- where you’ll have one codebase
tion/environment per customer, you (source of trust), and a code reposi-
include one application environment tory with a few branches (dev/tes-
for all your customers. This enables t/prod). By following below practices,
your AWS hosting costs to be dramati- –with a single command
cally reduced from hundreds of ser- (one-click-deployment)–, you will quic-
vers to a single environment. kly perform the deployment process
in a few seconds.
b) One single source of trust.Let’s
say again you have a customer using
your SaaS, imagine how many code

www.clickittech.com info@clickittech.com +52 (844) 141-8485


page. 4

c) Cost reductions of Development and time to market. Having a single


codebase, SaaS application environment, multi tenant database architecture,
centralized storage and APIs, and following the 12-factor methodology; all this will
allow you to reduce development labor costs, time to market, and operational
efficiencies.

Quickly Scale Your Business with


a DevOps Team Let’s Start!

1.1 What is a Multi Tenant Architecture?


First of all, you need to understand
what a single tenant architecture and Multi-tenant architecture is an

a Multi tenant architecture is. Single ecosystem or model, in which a single

Tenant vs Multi Tenant: SaaS Architectu- environment can serve multiple

re. tenants utilizing a scalable, available,


and resilient architecture. The underl-

A single-tenant architecture (siloed ying infrastructure is completely

model) is a single architecture per shared, logically isolated, and with

organization where the application fully centralized services. The mul-

has its own infrastructure, hardware, ti-tenant architecture evolves accor-

and software ecosystem. Let’s say you ding to the organization or subdo-

have ten organizations; in this case, main (organization.saas.com) that is

you would need to create ten standa- logged into the SaaS application; and

lone environments, and your SaaS is totally transparent to the end-user.

application or company will function


as a single tenant architecture. Addi- Bear in mind that in this paper, we

tionally, it implies more costs, more will discuss two Multi tenant architec-

maintenance, and a level of difficulty ture models, one for the application

to update across the environments. layer and one for the database layer.

www.clickittech.com info@clickittech.com +52 (844) 141-8485


page. 5

Single Tenant vs Multi tenant

Single-Tenant Multi-Tenant
SaaS application SaaS application
DB
Org1 Org2
Org1 Org2
App App

DB DB VS Org3 Org4

Cluster
Docker Docker Docker

App App App

Load Balancer

Org1 Org2 Org3

www.clickittech.com info@clickittech.com +52 (844) 141-8485


page. 6

2 SaaS Technology stack for an


Architecture on AWS
To build a multi tenant architecture, open-source community. The latest
you need the correct AWS web stack, trends to build SaaS applications are
including OS, language, libraries, and Python + React + AWS. Another “va-
services to AWS technologies. This is riant” is Node.js + React + AWS, but in
just the first step towards creating a the end, the common denominators
next-generation multi tenant architec- are AWS and React. Probably, if you
ture. are a financial company, ML/AI, with
complex Algorithms or backend work,
In case you haven’t chosen your web go for Python. On the other hand, if
stack, hereafter, I’ll suggest you the you are using modern technologies
ideal AWS SaaS stack. Even though we like real-time chats, mini feeds, strea-
will surface a few other multi tenant ming, etc.; then go for Node.js. There
architecture best practices. This arti- is a market in the banking sector that
cle will be primarily oriented to this is leveraging Java, but that’s for esta-
AWS SaaS web stack. blished enterprises. Any new SaaS
application better goes with the men-
Let’s dive into our SaaS Technology tioned web stack. Again, this is just
Stack on AWS: what I’ve noticed as a trend, and what
the community is demanding.
Programming language: It doesn’t
really matter which language plat- Note: This data comes from a survey
form you select. What is vital is that that we performed a few months ago for
Financial Services and SaaS companies.
your application can scale, able to
utilize Multi tenant architecture best
practices, cloud-native principles, and
a well-known language by the

www.clickittech.com info@clickittech.com +52 (844) 141-8485


page. 7

Ideal languages:

Python Node Js Java React Go

Cloud Provider: As a team of DevOps Microservices: If you are planning to


experts, I’ve noticed a cloud variation leverage the cloud, you must leverage
in the last two years, and which cloud-native principles. One of these
corresponds to these percentages: principles is to incorporate microser-
70% of our DevOps implementations vices with Docker. Make sure that
are based on AWS, 25% with Azure, your SaaS application is under micro-
and 5% go to GCP and Digital ocean. services, which brings multiple bene-
Each year the trend is similar, with the fits, including flexibility and standardi-
exception that Azure is gradually zation, easier to troubleshoot, pro-
growing with the years. Those are not blems isolation, and portability. Just
only my words but also ideas suppor- like the cloud, Docker and microservi-
ted by multiple DevOps Partners. So, I ces have transformed the IT ecosys-
strongly recommend deploying your tem and will stay for a long while.
SaaS application under AWS. It has a
Container orchestration platform:
number of benefits; every day there is
This is a complicated and abstract
a new service available for you, and a
decision; there are three options in
new feature that facilitates your deve-
AWS to manage, orchestrate, and
lopment and deployment.
create a microservice cluster environ-
ment.

Why Build SaaS on AWS?


First of all, did you know that you can’t perform the digital
transformation without the AWS Cloud? Over here, you’ll be
able to find more details about Why Build SaaS on AWS.

www.clickittech.com info@clickittech.com +52 (844) 141-8485


page. 8

Amazon ECS. It is the and worker nodes. (Recommended


natural Amazon contai- for large SaaS apps and a sophistica-
Amazon Elastic
Container Service ner orchestration ted DevOps and web development
system in the AWS ecosystem. Team).
(Highly recommended for startups,
small SaaS, and medium SaaS). Which is the best? What shall you
use? In this blog, I explain the main
differences, pros, and cons of Kuber-
Amazon Fargate. netes vs Amazon ECS; which is the best
Almost Serverless, price, container orchestration? Or watch
AWS Fargate
and management is per our video below.
task. Minimal operational effort vs.
ECS. There are some studies con- Kubernetes vs Amazon
ducted by our DevOps team; in ECS: Best Container
terms of performance. Fargate can Orchestration
be slower than ECS, so for this parti- Watch here

cular case, I would recommend


Looking for a hint? In the end, we chose
Amazon ECS, instead of Fargate. Amazon ECS

Another thought is that if your team


is pure developers and not planning Database. The inherent database will
to hire a DevOps engineer, perhaps be PostgreSQL with Amazon RDS.
Fargate is the way to go. However, I strongly recommend that
if you have a senior development
Amazon EKS. It is a team, and are projecting a high-traffic
managed service that for your SaaS application, –or even
Amazon
EKS makes Kubernetes on hundreds of tenants–, you’d better
AWS easy to manage. Use Amazon architect your database with Mongo-
EKS instead of deploying a Kuber- DB. In addition to this, utilize the best
netes cluster on an EC2 instance, practices that will be mentioned
set up the Kubernetes networking, below about Multi tenant Database.

www.clickittech.com info@clickittech.com +52 (844) 141-8485


page. 9

In this case, I would go for Amazon ming your SaaS application, don’t
RDS with PostgreSQL or DynamoDB forget to include GraphQL!
(Mongodb).
Note: I didn’t include this service in the
AWS stack architecture diagram, because
it is implemented in multiple ways, and it
If you are projecting a would require an in-depth explanation on
high-traffic for your SaaS this topic.

application, you’d better


Automation. You need a mechanism
architect your database
to trigger or launch new tenants/or-
with MongoDB
ganizations and attach it to your multi
tenant SaaS architecture. Let’s say
you have a new client that just subs-
GraphQL or Amazon AppSync. Is a cribed to your SaaS application, how
query language and an alternative to do you include this new organization
a RESTful API for your database servi- inside your environment, database,
ces. This new and modern ecosystem and business logic? You need an
is adopted as a middleman among automated process to launch new
the client and the database server. It tenants; this is called Infrastructure
allows you to retrieve database data as Code (IaC). This script/procedure
faster, mitigate the over-fetching in should live within a git/bitbucket
databases, retrieve the accurate data repository, one of the fundamental
needed from the GraphQL Schema, DevOps principles. A strong argument
and mainly the speed of development to leverage Automation and IaC is
by iterating more quickly than a REST- that you need a mechanism to auto-
ful service. Adopting a monolithic mate your SaaS application for your
backend application into a Multi code deployments. In the same lines,
tenant microservice architecture is automate the provisioning of new
the perfect time to leverage GraphQL Infrastructure for your Dev/Test envi-
or AppSync. Hence, when transfor- ronments.

www.clickittech.com info@clickittech.com +52 (844) 141-8485


page. 10

Infrastructure. As Code and Automa- RabbitMQ, or Celery. What I suggest


tion Tools here is to utilize the service that
It doesn’t matter which one to use, requires you less operation, in this
they are both highly known by the case, is Amazon SQS. There are multi-
DevOps community, as well as useful ple times that you need asynchro-
and do the same job. I don’t have a nous communication. From delaying
winner, they are both good. or scheduling a task, to increasing
reliability and persistence with critical
web transactions, decoupling your
Terraform (from Hashi- monolithic or micro-service applica-
corp). A popular Cloud-ag- tion, and, most importantly: using a
Terraform

nostic tool. Used widely for Queue System to communicate


all DevOps communities. Easier to Event-driven Serverless applications
find DevOps with this skill. (Amazon Lambda functions).

Caching System. AWS ElastiCache is


Amazon CloudFormation.
a caching and data storage system
Easier to integrate with
AWS
that is fully scalable, available, and
CloudFormation
Amazon Web services,
managed. It aims to improve the
AWS built-in Automation tool. Whene-
application performance of distribu-
ver there is a new Amazon technology
ted cache data and in-memory data
just released, the compatibility with
structure stores. It’s an in-memory
AWS and CloudFormation is released
key-value store for Memcached and
sooner than Terraform. Trust on an
Redis engines. With a few clicks, you
AWS CloudFormation expert to automa-
can run this AWS component entirely
te and release in a secure manner.
self-managed. It is essential to include
Message Queue System (MQS) a caching system for your SaaS appli-
The common MQS are Amazon SQS, cation.

Release Products Faster with


our Nearshore DevOps Team Let’s Start!

www.clickittech.com info@clickittech.com +52 (844) 141-8485


page. 11

Amazon CloudFront CDN and of Amazon S3, we will include AWS


Amazon S3 for your static content CloudFront to cache the entire static
All static content, including images, content and reduce bandwidth costs.
media and HTML, will be hosted on Integrating this pair of elements is
Amazon S3, –the cloud storage with vital.
infinite storage and elasticity. In front

2.1 SaaS Multi tenant web stack architecture


and diagram on AWS (Amazon Web Services).

www.clickittech.com info@clickittech.com +52 (844) 141-8485


page. 12

3 Which Multi tenant architecture suits


better for your SaaS Application on AWS?
It is paramount to decide which multi real SaaS application.
tenant architecture you’ll incorporate
in your SaaS application from the As a side note, we will discuss two
application and database layer. We types of multi tenant architecture:
will explore the two layers needed to Application layer Multi-tenancy and
enable your application to act as a Database layer Multi-tenancy.

4 Types of Multi tenant SaaS


architectures: Application Layer
4.1 SaaS Monolithic architecture for Multi tenant SaaS applications

Probably, if you haven’t seen this


Cons:
article before, –or if you have already
Waste of AWS resources.
developed and architected your own
SaaS application–, I’m sure you have Not very fault-tolerant per service.

fallen into this approach. The monoli- If the app tier goes down, the
thic components include EC2 instan- whole backend is down.
ces in the web tier, app tier, and When deploying your code, you
Amazon RDS with MySQL for your
have to deploy all your services
Database. The monolithic architectu-
within the app-tier, and not just
re is not a bad approach, with the
with a specific isolated service.
exception that you are wasting
resources massively in the mentioned Not flexible to maintain.
tiers. At least around 50% and 70% of Slows time to market.
your CPU/RAM usage is wasted due HIPAA and PCI compliance cons-
to the nature of the monolithic
traints.
(cloud) architecture.

www.clickittech.com info@clickittech.com +52 (844) 141-8485


page. 13

Pros:
Easy-to-build approach.

Minimal configuration.

Multi tenant database.

Multi tenant architecture example:


Monolithic diagram

www.clickittech.com info@clickittech.com +52 (844) 141-8485


page. 14

4.2 4.2. Microservices Multi tenant


Architecture for SaaS with containers
and Amazon ECS.
Microservices are a recommended ECS as the container orchestrator;
type of architecture since they provide they can be the perfect match. Mainly,
a balance between modernization and consider that Amazon ECS gives fewer
maximum use of available cloud efforts to configure your cluster and
resources (EC2 instances and compute more NoOps for your DevOps team.
units). As well as it introduces a

By 2022, 90% of all new apps will feature microservices archi-


tectures that improve the ability to design, debug, update, and
leverage third-party code; 35% of all production apps will be
cloud-native.
- Forbes, 2019

decomposed system with more granu- With a talented team, the best Mul-
lar services (microservices). We won’t ti-tenant architecture approach would
touch much about the Microservice be this use case scenario. Along the
benefits since it’s widely expressed same lines, it covers the SaaS software
in the community. However, I’ll and architecture’s main attributes,
recommend you to utilize the formula including agility, innovation, repeata-
of Multi-tenant architecture + AWS bility, reduced cycle time, cost efficien-
Services + microservices + Amazon cy, and manageability.

The perfect match


Multi tenant architecture + AWS Services + microservices + Amazon ECS
(as the container orchestrator).

www.clickittech.com info@clickittech.com +52 (844) 141-8485


page. 15

Multi tenant Architecture example:


for SaaS with microservices.

User Devices Internet

AWS Cloud

Cloudfront Distribution
VPC

Public subnet Public subnet

Application S3 Media
Load Balancer

Private subnet

EC2 instance contents EC2 instance contents


Organization1 Organization N

Microservice Microservice Microservice Microservice


ECR
Microservice Microservice
A B C A B C
ECS Cluster Docker Registry

Private subnet

Postgre SQL Postgre SQL


Organization 1 Organization N

Pros: Cons:
Adds a key DevOps principle: A decent grade of complexity to
Loosely coupled architecture. create the microservices archi-
Easier to deploy new code to pro- tecture and the ECS clustering.
duction. Amacon ECS natively lives in the
Helps perform smaller deploy- AWS cloud; and you can't port
ment per microservice. Better this service into another cloud
agility. provider due to it is a proprietary
Pure and real distributed service. service from AWS.
Repeatability and manageability.
A much better level of resources
utilization than monolithic.

www.clickittech.com info@clickittech.com +52 (844) 141-8485


page. 16

4.3 SaaS Kubernetes: Multi-tenant


architecture with Kubernetes
(Amazon EKS)
You might be wondering… what about and avoid creating noise to the other
Kubernetes or Amazon Kubernetes tenants. Another point to consider is
Service (EKS)? Well, Kubernetes is ano- that if you would like to isolate your
ther alternative of microservice archi- namespaces, you need to include Ku-
tecture which adds an extra layer of bernetes Network policies because by
complexity in the SaaS equation. default the networking is open, and
However, you can overcome this com- can communicate across namespaces
plexity by leveraging Amazon EKS (The and containers.
Managed Kubernetes service from
Amazon), which is a de facto service by Here is a comparison of Amazon ECS vs
the Kubernetes community. Kubernetes. You can also visit our you-
tube channel and watch a video that
So, how would a Kubernetes Multi-te- compares and declares which is the
nant architecture look like? The inte- Best Container. Alternatively, –if you
resting part of this component from have a SaaS enterprise–, I’ll recom-
the rest of the architectures is that it mend better to control your microser-
provides the use of namespaces. This vice via Amazon EKS or Kubernetes
attribute aids to isolate every tenant since it allows you to have more gra-
and its own environment within the nular changes.
corresponding Kubernetes cluster. In
this sense, you don’t have to create
different clusters per each tenant (you
could, but for another approach). Also,
by using ResourceQuota you can limit
the resources used per namespace

www.clickittech.com info@clickittech.com +52 (844) 141-8485


page. 17

Kubernetes Multi-tenant SaaS


Architecture diagram

User Devices Internet


AWS Cloud
Pros:
VPC Same pros as the microservices
Public subnet Public subnet
architecture with Amazon ECS
Exceptional in-depth custom
Application
SaaS configuration.
Load Balancer
Used more by enterprise SaaS
Private subnet companies.
EC2 Master Node
Control Plane

Cons:
Amazon
Kubernetes The classic, a higher learning
Service Ingress Controller
NGINIX curve vs Amazon ECS.
A Re-architecture of your SaaS
Private subnet
application.
EC2 Worker Node EC2 Worker Node

Containers Containers

Namespace Organization 1 Namespace Organization N

Private subnet

DB DB
Organization 1 Organization N

* A simple Multi-tenant architecture with


Kubernetes and siloed by Kubernetes
Namespaces.

www.clickittech.com info@clickittech.com +52 (844) 141-8485


page. 18

4.4 Serverless Multi tenant SaaS


Architecture
The dream of any AWS architect is to
No need to worry about scalabili-
create a Multi tenant SaaS architecture
ty, downtime, or availability. It is
with a Serverless approach. That’s a all built-in in the serverless
dream that can come true as a DevOps ecosystem.
or SaaS architect, but it especially adds Serverless simplifies much more
the deployments per function.
a fair amount of complexity as a tra-
deoff. Additionally, it requires a reaso-
nable amount of time of collaboration
Cons:
with your dev team, extensive applica-
You need to refactor the entire
tion of code changes, and a transfor- application into a Serverless
mative Serverless mindset. Given said ecosystem
that, anyhow in a few years, it will be It adds much more complexity
than previous architectures.
the ultimate solution, and all de-
pends on the talent, capabilities,
and use case. A Serverless SaaS architecture enables
applications to obtain more agility,
Serverless is disrupting the IT stack, and resilience, and fewer development
you still on-premises? Go through this efforts, a truly NoOps ecosystem.
paper I created a few months ago
which shows more details about the At a high level, what are the new parts
serverless ecosystem. of this next-generation serverless
SaaS architecture? Every call becomes
Pros: an isolated tenant call, either going to
Truly based on consumption by
a logical service (Lambda function) or
milisecs/secs.
going to the database data coming
End-to-end serverless applica-
tion. from the Amazon API Gateway as an
A microservice per each event entry point in the serverless SaaS
call. application. Now that you have decou-

www.clickittech.com info@clickittech.com +52 (844) 141-8485


page. 19

pled every logical service, the authenti- an STS token with these aspects. Parti-
cation and authorization module cularly API Gateway will route all
needs to be handled by a third-party tenant functions to the correct
service like Amazon Cognito, which will lambda functions matching the STS
be the one to identify the tenant, user, Token.
tier, IAM tenant role, and bringing back

Multi tenant architecture example:


For SaaS with Serverless.

Organization 1 Internet Organization N

AWS Cloud

Cloudfront Distribution
( Cache/ CDN)

Cognito Api Gateway


( Request Authorizer) ( Endpont Proxy)

Lamba Application

Node js Python Ruby


Lambda Call Lambda Call
Tenant1 Tenant N

Login User Product Cart

Storage

Postgre SQL S3
DB Media/Reports

www.clickittech.com info@clickittech.com +52 (844) 141-8485


page. 20

5 Types of Multi tenant SaaS Architecture:


Database layer
The multi-tenancy concept comes with As a next step, you need to evaluate
different architecture layers. We have what Multi tenant database architec-
already advocated the multi-tenancy ture to pursue with tables, schemas,
application layer and its variants. Now, or a siloed database.
it is time to explore multi-tenancy in
the Database layer, which is another The following multi-tenant databa-
aspect to discover. Paradoxically, the se architectures can be distingui-
easiest and cost-effective Multi-tenant shed as:
database architecture is the pure and
real database multitenancy.

5.1 Single database: A table per tenant


(pure multi-tenancy and pooled model).
This database architecture is the tenant can overuse compute and ram
common and the default solution by resources from another. Lastly, every
DevOps or software architects. It is table name has its own tenantID,
very cost-effective when having a which is very straightforward to
small startup or with a few dozen orga- design and architect.
nizations. It consists of leveraging a Alternative single-tenant database
table per each organization within a architecture: a Shared table in a
database schema. There are specific single schema in a single schema in a
trade-offs for this architecture, inclu- single database. Perfect for Dynamo-
ding the sacrifice of data isolation, DB. (We didn’t cover this approach -
noise among tenants, and performan- FYI)
ce degradation -meaning that one

www.clickittech.com info@clickittech.com +52 (844) 141-8485


page. 21

Pros: Cons:
Lowest cost per tenant. Hard to troubleshoot a single-te-
The easiest architecture to scale nant per table.
your database. (However, you Hard to backup and restore a
have always a limit). single tenant per table.
Great approach for hundreds/- Reaching the single database
thousands of tenants. limits, it becomes extremely
difficult to control.
Low tenant isolation.

5.2 Single Database: A schema per


tenant (bridge model).
This Multi-tenant database approach in your database performance.
is still very cost-effective and more Hence, it is recommended to split the
secure than the pure tenancy (DB database into two (add the second
pooled model), since you are with a database as a replica). However, the
single database, with the exception of best database tool for this approach
the database schema isolation per is PostgreSQL, which supports multi-
tenant. If you are concerned about ple schemas without much complexi-
data partitioning, this solution is ty. And lastly, this strategy of –a
slightly better than the previous one schema per tenant– shares resources,
(Single/pooled DB, a table per tenant). compute, and storage across all its
Similarly, it is simple to manage tenants. As a result, it provokes noisy
across multiple schemas in your appli- tenants that utilize more resources
cation code configuration. One impor- than expected.
tant distinction to notice is that with
more than 100 schemas or tenants
within a database, it can provoke a lag

www.clickittech.com info@clickittech.com +52 (844) 141-8485


page. 22

Pros: Cons:
Low development complexity. It doesn’t comply with PCI/HI-
This pattern is best used for a PAA/Fedramp regulations.
few dozens of schemas. However, if you dont need them,
More secure vs single database who cares?
(A table per tenant). Can get slower by the fact that
You can customize specific sche- loading a specific schema can be
mas per tenant. A Different ver- an expensive operation.
sion per schema. Medium tenant isolation.
Scales horizontally. Updating a database structure
would need an update to all
schemas.

5.3 A database server (instance) per tenant


Siloed model (expensive, but the best
for isolation and security compliance).
This technique is significantly more model, or at least find a workaround
costly than the rest of multi-tenant with the correct IAM roles, the best
database architectures, but it com- container orchestration –either Kuber-
plies with security regulations; the netes or Amazon ECS namespaces–, a
best for performance, scalability, and VPC per tenant and encryption
data isolation. This pattern uses one everywhere.
database server per tenant, it means
if the SaaS app has 100 tenants, the- Cons:
refore there will be 100 database
Low development complexity.
servers, extremely costly.
High tenant and data isolation.
When PCI, HIPAA or SOC2 is needed, Widely used and accepted by the
it is vital to utilize a database siloed customer.

Architect your SaaS App with AWS Let’s Start!

www.clickittech.com info@clickittech.com +52 (844) 141-8485


page. 23

Cons:
DynamoDB (great option).
Highest costs per tenant.
Complex to manage N database Dynamo DB

servers (hard management).


Hard to scale to more than 100
servers?? Amazon RDS with
Scales vertically. Mysql
Amazon RDS
with MySQL

Tools for a Multi tenant Database GraphQL. As described


architecture: previously, use it in front of
GraphQL any of these databases to
increase speed on data
retrieval, speed on development, and
Amazon RDS with alternative to RESTful API, which helps
PostgreSQL (best option). to relieve requests from the backed
Amazon RDS
with PostgreSQL
servers to the client.

6 Application Code Changes.


Once you have selected your Multi meworks are able to capture the URL
tenant strategy in every layer, let’s or subdomain that is coming from the
start considering what is needed to request. The ability to obtain this
change in the code level, in terms of information (subdomain) at runtime
code changes. If you have decided to is critical to handling dynamic subdo-
adopt Django (from Python) for your mains for your Multi-tenant architec-
SaaS application. Then you need a ture. We won’t cover in-depth what
few tweak changes to align your lines of codes we need to include in
current application with your Multi-te- your Django application –or in any
nant architecture from the database other framework–, but at least I’ll let
and application layer. Fortunately, you know what items should be con-
web application languages and fra- sidered in this section.

www.clickittech.com info@clickittech.com +52 (844) 141-8485


page. 24

7 In a nutshell for Python Django:


1 Add an app called tenant.py, a class for tenantAwareModel with multiple
pool classes.

2 How to identify tenants? You need to give each tenant a subdomain; to


do so, you need to modify a few DNS changes, Nginx/Apache tweaks, and
add a utility method (utils.py). Now, whenever you have a request, you can
use this method to get the tenant.

3 Determine how to extract the tenant utilizing the host header (subdo-
main).

4 Admin isolation.

Note: Previous code suggestions can change depending on the architecture.

8 Wildcard DNS Subdomain – URL


based SaaS application.
Basically, every organization must multi-tenant application, and this
have its own subdomain, and they are DNS change will facilitate the identifi-
quite useful for identifying organiza- cation, authentication, and authoriza-
tions. Per tenant, it is a unique dedica- tion of every tenant. However, ano-
ted space, environment, and custom ther workaround is called path-based
application (at least logically); for per tenant, which is not recommen-
example, ‘org1.saas.com’, ‘org2.saas.- ded, for example, ‘app.saas.-
com’, and so on. This URL structure com/org1/…’, ‘app.saas.com/org2…’,
will dynamically provision your SaaS and so on.

www.clickittech.com info@clickittech.com +52 (844) 141-8485


page. 25

So, the following is required in this


particular section: Similarly, a CNAME record labeled
A wildcard record should be in (*) pointing to your ‘app.saas.com’
place in your DNS management or ‘saas.com/login’. An asterisk (*)
records. means a wildcard to your app
domain.
This wildcard subdomain redi-
rects all routes to your Multi-te- As a final step, another (A) record
nant architecture (either to the pointing your ‘app.saas.com’
load balancer, application server domain to your amazon ECS clus-
or cluster end-point). ter, ALB, or IP.

DNS Records entries:

*. saas. co m CN AME ‘a p p.sa a s.co m


app. saas.com A 1.2.3.4 OR a p p.sa a s.co m A ( a lia s)
b al an cer.us- ea st-1.elb.a m a zo n aws.co m

Note: An (A) Alias record is when you are utilizing an ALB/ELB (Load Balancer)
from AWS.

9 Web Server Setup - Nginx configuration


to support Multi tenant SaaS applications.
Let’s move down to your web server, up a wildcard vhost for your Nginx
specifically Nginx. In this stage, you web server. Make sure it is an alias
will need to configure your Nginx.conf (ServerAlias) and a catch-all wildcard
and server blocks (virtual hosts). Set site. You don’t have to create a sub-

www.clickittech.com info@clickittech.com +52 (844) 141-8485


page. 26

domain VirtualHost in Nginx per single wildcard VirtualHost for all


tenant; instead, you need to set up a your tenants. Naturally, the wildcard
single wildcard VirtualHost for all your pattern will match your subdomains
tenants. Naturally, the wildcard pat- and route accordingly to the correct
tern will match your subdomains and and unique patch of your SaaS app
route accordingly to the correct and document root.
unique patch of your SaaS app docu-
ment root.Let’s move down to your SSL Certificates. Just don’t forget to
web server, specifically Nginx. In this deal with the certificates under your
stage, you will need to configure your tenant subdomains. You would need
Nginx.conf and server blocks (virtual to add them either in the Cloudfront
hosts). Set up a wildcard vhost for CDN, Load balancer, or in your web
your Nginx web server. Make sure it is server.
an alias (ServerAlias) and a catch-all
wildcard site. You don’t have to create Note: This solution can be accom-
plished using Apache web server.
a subdomain VirtualHost in Nginx per
tenant; instead, you need to set up a

10 Follow the 12-factor methodology


framework or die trying!
Following the 12-factor methodology Willing to know more about the
represents the pure DevOps and 12-factor methodology? This article
cloud-native principles, including deeply explains how to adopt the
immutable infrastructure, dev/test 12-factor methodology for any SaaS
and prod parity with Docker, CI/CD application on AWS.
principles, stateless SaaS application,
and more.

www.clickittech.com info@clickittech.com +52 (844) 141-8485


page. 27

11 What are the Multi-tenant SaaS


architecture best practices?

11.1. SaaS architecture Best practices for your


multi tenant application:

How is your SaaS application going to scale?

You should consider a strategy on how to scale your SaaS application.


Here is a good Scaling strategy to follow:

1 Amazon AutoScaling, either with ec2 instances or microservices.

2 Database replication with Amazon RDS, Amazon Aurora or DynamoDB.

3 Application Load Balancer.

4 Including a CloudFront CDN for your static content.

5 Amazon S3 for all your static/media content.

6 Caching system including Redis/Memcached or its equivalent in the


AWS cloud – Amazon ElastiCache.

7 Multi-availability zone set up for redundancy and availability.

More details here on how to scale a SaaS Application.

Build a Multitenant Architecture


for SaaS Let’s Start!

www.clickittech.com info@clickittech.com +52 (844) 141-8485


page. 28

Code Deployments with CI/CD DevOps Automation: Automate your


(application updates across tenant new tenant creation process
apps). Another crucial aspect to con- How are you creating new tenants
sider is how to deploy your code per each subscription? Identify the
releases across tenants and your process of launching new tenants into
multiple environments (dev, test, and your SaaS environment. You need to
prod). You will need a Continuous trigger a script to launch or attach the
Integration and Continuous Delivery new Multi tenant environment to
(CI/CD) process to streamline your your existing Multi-tenant architectu-
code releases across all environments re, meaning to automate the setup of
and tenants. If you follow-up on my new tenants. Consider that it can be
previous best practices, it won’t be after your customer gets registered in
difficult. The CI/CD practice is another your onboarding page, or you need to
world that your DevOps team needs trigger the script manually.
to get familiar with, but with a team
like us on ClickIT. CI/CD is just one of Tools for the automation:

the five principles of DevOps practi- Terraform (Recommended)


ces, it is pretty lean for us to adopt it
Amazon CloudFormation
into your SaaS application. Ready to
(Trust on an AWS CloudForma-
go?
tion certified team).
What tools to embrace CI/CD?
Ansible.
CI/CD tools: Jenkins, CircleCi, or AWS
Code pipelines (along with Codebuild Note: Ensure you utilize Infrastructure As
Code principles in this aspect.
and CodeDeploy).

My advice
If you are looking for a sophisticated DevOps team and widely known tool, go for
Jenkins; otherwise, go for CircleCI. If you want to keep leveraging AWS technologies
exclusively… Then go for AWS Code pipelines. But, if you’re looking for compliance,
banks, or regulated environments, Go for Gitlab.

www.clickittech.com info@clickittech.com +52 (844) 141-8485


page. 29

How your architecture will be isola- Just think, you have 99 tenants, com-
ted from other tenants: Siloed com- pute/database load is almost to the
pute and siloed storage
limits, do you have a ready environ-
Just identify the next: Every layer of
ment to support the new tenants?
the SaaS application needs to be iso-
What about the databases? You have
lated. The customer workflow is tou-
a particular customer that wants an
ching multiple layers, pages, backend,
isolated Tenant environment for its
networking, front-end, storage, and
SaaS application. How would you
more bits – How is your isolation stra-
support an extra Tenant environ-
tegy?
ment, separated from the rest of the
multi-tenant architecture? Would you
Take in mind the next aspects: do it? What are the implications? Just
consider a scenario for this aspect.
IAM Roles per function or
micro services.
Tenant clean-up.

Amazon S3 security policies. What are you doing with the tenants
that are idle or not used anymore?
VPC isolation.
Perhaps a clean-up process for any

Amazon ECS / Kubernetes tenant that has been inactive for a


Namespace isolation. prolonged period, or remove unused
resources/tenants by hand, but you
Database isolation (tenant per
need a process or automation script.
table/schema/silo database)

Tenant compute capacity – Have


you considered how many SaaS
tenants can it support per environ-
ment?

www.clickittech.com info@clickittech.com +52 (844) 141-8485


page. 30

12 Conclusions
Multi tenant architecture and SaaS nearshore collaboration model (which
applications under AWS… What a topic adds another layer of cost-savings).
that we just discovered! Now you un-
derstand the whole Multi tenant SaaS If you ever need a hand on how to
architecture cycle from end-to-end, architect your SaaS application, execu-
including server configuration, code, te the whole AWS/DevOps projects
and what architecture pursues per and follow these principles, or just
every IT layer. As you can notice, there hire a DevOps engineer to fulfill your
is no global solution for this ecosys- DevOps needs, just contact us. We
tem. There are multiple variants per help enterprises run successfully their
each IT layer, either all fully multi-te- DevOps embracement. Read more
nant, partially tenant or just silo about the voice of our customers.
tenants. It falls more on what you
need, budget, complexity, and the ClickIT is an AWS Select Partner with
expertise of your DevOps team. multiple AWS Certifications. Every
engineer on ClickIT loads more than
I strongly recommend going for micro- 10 DevOps projects based on SaaS
services (ECS/EKS), partially multi architectures and cloud-native appli-
tenant SaaS in the app, and database cations including PHP Laravel, React,
layer. As well, include cloud-native Angular, NodeJS, Python, Go, Ruby,
principles, and finally, adopt the mul- and Java. In the DevOps space, we
ti-tenant architecture best practices work with any cloud provider (Azure,
and considerations described in this AWS, Digital Ocean, and Google
article. That being said, brainstorm Cloud), with any CI/CD, including Jen-
your SaaS architecture firstly by thin- kins, CircleCI, bitbucket, and more.
king on how to gain agility, cost-effi-
ciency, IT labor costs, and leveraging a In regard, Automation with Terraform

www.clickittech.com info@clickittech.com +52 (844) 141-8485


page. 31

and CloudFormation is our best requirement should be included in


choice. And even better, most of our your processes. In case you’re looking
AWS/DevOps projects are following to learn more about DevOps practices
PCI, HIPAA, and SOC2 regulations. If and SaaS applications, don’t hesitate
you are a fintech, healthcare, or SaaS and visit our YouTube Channel to
company, well, you know this type of learn more through videos.

Implement the migration of


Contact Us!
your host in the AWS

About ClickIT
ClickIT is an experienced Cloud and DevOps Nearshore Solution Provider for 10
years. Our competencies are Financial Services, Healthcare, MarTech, Ecommerce,
Big Data & Analytics and our Experience comes with startups and mid-large enter-
prises. We are AWS and GCP certified partners with an experience of helping more
than 200 product and service-centric companies based out of the US with their
cloud migration and DevOps initiatives.

Select Consulting Partner

AWS CloudFormation Select Partner

AWS Solutions Architect Associate

AWS Developer Associate

www.clickittech.com info@clickittech.com +52 (844) 141-8485

You might also like