Course contentsShow
← Node.js
Lesson 553 of 1,9806. Networking and HTTP FundamentalsFree lesson

Multi-tenant Server Partitioning

Route incoming requests to different service instances based on hostnames or custom request headers.

In this lesson, you will learn how to direct incoming HTTP traffic to specific application instances based on the source of the request, a practice known as multi-tenant server partitioning.

The Concept of Partitioning

When you run a single Node.js application that serves multiple clients (tenants), you might find that one tenant’s heavy traffic affects the performance of another. Instead of scaling the entire server, "multi-tenant partitioning" allows you to isolate traffic by routing it to different "shards" or instances. By inspecting the incoming Host header or a custom header (like x-tenant-id), your entry-point server can act as a traffic controller to ensure requests are handled by the correct environment.

Routing Logic

Imagine you are running a SaaS platform. When a request hits your main Node.js gateway, you analyze the headers before passing the request along. You can use the Host header to determine the destination:

const http = require('http');

const server = http.createServer((req, res) => {
  const host = req.headers['host'];

  if (host === 'tenant-a.myapp.com') {
    // Route to the internal cluster assigned to Tenant A
    proxyTo(req, res, 'http://10.0.0.1:3000');
  } else if (host === 'tenant-b.myapp.com') {
    // Route to the internal cluster assigned to Tenant B
    proxyTo(req, res, 'http://10.0.0.2:3000');
  } else {
    res.statusCode = 404;
    res.end('Tenant not found');
  }
});

Why Partition?

By partitioning, you gain three major advantages:

  1. Resource Isolation: A spike in traffic from Tenant A only impacts the infrastructure assigned to them, leaving Tenant B unaffected.
  2. Security Compliance: You can deploy specific tenants to instances with different security configurations or physically separate databases.
  3. Deployment Granularity: You can update the software version for one specific tenant without touching the rest of your user base.

This pattern leverages your knowledge of header inspection to act as a lightweight traffic director, effectively separating concerns at the network entry point before the application logic even begins.

Key Takeaway: Multi-tenant server partitioning uses request headers like Host to dynamically route traffic to isolated service instances, ensuring performance and security boundaries between your users.