Sarathlal N

Handle tenant specific subdomains for SaaS application

Consider that we have a cloud SaaS solution and all our customers use our product at

With custom subdomains, Customer A can use the product at, and similarly, Customer B can use the product at

Pros of this method

  1. Subdomains can provide tenants better isolation with better security control for cookies, cross-origin resources sharing, etc.
  2. With the help of DNS and Load Balancer configuration, subdomains can help us to configure tenant-specific hardware if required.

It is better to design our application without subdomains by default, and then bake in custom subdomain functionality as an additional feature/layer. If we integrate subdomains all the way through from the beginning, then it might become very inflexible to change it in the future.

Different methods to achieve the requirement given below.

Using DNS and CNAME

A major drawback of this design is DNS propagation. Every time customers configure a new subdomain, we will add a new CNAME record to the DNS. As DNS propagation is not instantaneous, your customers might have to wait (maybe a few hours) before they can use their newly configured subdomain.

Using Reverse Proxy Server

In this approach, we have a single wildcard DNS entry (* which points to a reverse proxy server (varnish, haproxy, Nginx, etc.), and keeping the entries for routing subdomain to the actual backend servers in this reverse proxy server.

May a reverse proxy server is a bottleneck and can become a single point of failure. But handling subdomain entries in the reverse proxy is much better than managing them in the DNS.

Using Servlet Filter

This is known as the best option among these 3 methods. But I didn’t try & I’m completely zero in this option. So no comments now.

Recent Posts

  1. Solve "ValueError invalid literal for int() with base 10" - Python
  2. Handling dynamic subdomain with Flask - Python
  3. A minimal example about WordPress object cache
  4. Select random element from a list - Python
  5. Write our first Selenium program with Python 3 & Firefox

Your Questions / Comments

If you found this article interesting, found errors, or just want to discuss about it, please get in touch.