Implementing Multitenancy in SAP CAP Applications

Multitenancy is a core requirement for SaaS applications running on SAP BTP. The SAP Cloud Application Programming Model (CAP) provides built-in features to develop scalable, tenant-aware systems with isolated data and centralized application logic.

This guide explains how multitenancy works in CAP, how tenants are provisioned, and how data containers are created dynamically.

What Is Multitenancy in CAP?

Multitenancy allows a single CAP application to serve multiple customers (tenants) while isolating:

  • Data
  • Configurations
  • Authorization policies
  • Service bindings

With CAP, the application code is shared, but each tenant gets its own:

  • HDI schema
  • Database container
  • Service instance bindings

This makes CAP ideal for enterprise SaaS offerings on BTP.

How CAP Enables Multitenancy

CAP relies on SAP BTP services to implement multitenancy:

  • SaaS Provisioning Service (for onboarding/offboarding tenants)
  • XSUAA (for authentication & tenant isolation)
  • HDI containers (for tenant-specific schemas)
  • Service Manager (automated resource provisioning)

The CAP runtime automatically:

  • Detects tenant context
  • Routes requests
  • Applies tenant-specific database bindings
  • Ensures isolated persistence

Enabling Multitenancy in CAP

Step 1: Enable Multitenancy in package.json

{
  "cds": {
    "requires": {
      "multitenancy": true
    }
  }
}

Step 2: Use SaaS Provisioning Service in package.json

"cds": {
  "requires": {
    "saas-registry": {
      "kind": "saas-registry"
    }
  }
}

This activates SaaS onboarding/offboarding APIs.

Step 3: Bootstrap CAP with Multitenancy

const cds = require('@sap/cds');
cds.on('bootstrap', app => {
  // custom logic (optional)
});
module.exports = cds.server;

CAP automatically registers provisioning handlers.

Tenant Provisioning Workflow

Once multitenancy is enabled, SAP BTP handles:

  1. Tenant subscribes to SaaS app
  2. CAP receives the onboard event
  3. New HDI container is created
  4. Database artifacts deployed to tenant-specific schema
  5. Connections and credentials stored in Service Manager
  6. Tenant is activated

Example Provisioning Handler

module.exports = cds.service.impl(function () {
  this.on('tenantProvision', async req => {
    console.log('Provisioning tenant:', req.tenant);
  });
});

But in most cases, CAP handles provisioning automatically with no custom code required.

Tenant Isolation Using HDI Containers

Each tenant gets a dedicated HDI container:

  • Separate tables
  • Independent schemas
  • Cleaner upgrades
  • Full isolation

Example HDI configuration:

"cds": {
  "requires": {
    "db": {
      "kind": "hana",
      "multiTenant": true
    }
  }
}

CAP automatically switches database connections per tenant.

Routing Tenant Requests

CAP distinguishes tenants using:

  • JWT tokens
  • Subdomain in the URL
  • XSUAA issuer claim

Examples:

tenant1.myapp.cfapps.eu10.hana.ondemand.com
tenant2.myapp.cfapps.eu10.hana.ondemand.com

CAP maps the incoming domain → tenant → schema.

Testing Multitenancy Locally

CAP provides a local multitenancy simulator.

Enable it:

cds watch --multi-tenant

Simulate tenants:

cds deploy --to sqlite:tenant1.db
cds deploy --to sqlite:tenant2.db

Switch tenants:

cds serve --tenant tenant1
cds serve --tenant tenant2

Useful for validating routing & service logic.

Deployment on SAP BTP

For deployment, ensure the mta.yaml includes:

  • XSUAA
  • SaaS Registry
  • HDI containers
  • Service Manager

Example section:

- name: myapp-saas-registry
  type: org.cloudfoundry.managed-service
  parameters:
    service: saas-registry
    service-plan: application

Once deployed:

  • Tenants subscribe via BTP cockpit
  • CAP provisions resources
  • App is ready for use

Custom Logic During Provisioning

You can extend provisioning using CAP events:

  • subscribe
  • unsubscribe
  • upgrade

Example:

cds.on('subscribe', async req => {
  console.log(`Tenant ${req.tenant} subscribed.`);
});

Useful for:

  • Creating default master data
  • Sending onboarding emails
  • Logging events

Handling Tenant-Specific Configurations

You can store tenant-level configuration:

  • In the HDI tenant DB
  • In a central admin DB
  • Via feature toggles
  • Remote configuration services

Example query:

const settings = await SELECT.one.from('Config').where({ tenant: req.tenant });

Best Practices

  • Keep business logic tenant-agnostic
  • Use feature flags to enable tenant-specific functionality
  • Avoid hardcoding tenant IDs
  • Minimize dependencies between tenants
  • Log tenant actions for audit purposes
  • Automate DB migrations during upgrades
  • Monitor provisioning errors

Conclusion

SAP CAP makes multitenancy development clean, scalable, and enterprise-ready. With built-in provisioning, tenant routing, and automated schema management via HDI containers, you can focus on building robust SaaS applications instead of complex infrastructure.

Whether you’re onboarding hundreds of tenants or customizing tenant-specific features, CAP provides everything needed to run a secure, isolated, multitenant SaaS solution on SAP BTP.

you may be interested in this blog here:-

Don’t Fear the Update: Navigating the Challenges of how to implement sap note

Five Top Technology Investment Drivers for 2024

How many dollars worth of RSU does Salesforce typically offer an MTS (experienced hire) on joining?

Integration cloud system to HANA Cloud Platform using Cloud Connector

₹25,000.00

SAP SD S4 HANA

SAP SD (Sales and Distribution) is a module in the SAP ERP (Enterprise Resource Planning) system that handles all aspects of sales and distribution processes. S4 HANA is the latest version of SAP’s ERP suite, built on the SAP HANA in-memory database platform. It provides real-time data processing capabilities, improved…
₹25,000.00

SAP HR HCM

SAP Human Capital Management (SAP HCM)  is an important module in SAP. It is also known as SAP Human Resource Management System (SAP HRMS) or SAP Human Resource (HR). SAP HR software allows you to automate record-keeping processes. It is an ideal framework for the HR department to take advantage…
₹25,000.00

Salesforce Administrator Training

I am text block. Click edit button to change this text. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.
₹25,000.00

Salesforce Developer Training

Salesforce Developer Training Overview Salesforce Developer training advances your skills and knowledge in building custom applications on the Salesforce platform using the programming capabilities of Apex code and the Visualforce UI framework. It covers all the fundamentals of application development through real-time projects and utilizes cases to help you clear…
₹25,000.00

SAP EWM

SAP EWM stands for Extended Warehouse Management. It is a best-of-breed WMS Warehouse Management System product offered by SAP. It was first released in 2007 as a part of SAP SCM meaning Supply Chain Management suite, but in subsequent releases, it was offered as a stand-alone product. The latest version…
₹25,000.00

Oracle PL-SQL Training Program

Oracle PL-SQL is actually the number one database. The demand in market is growing equally with the value of the database. It has become necessary for the Oracle PL-SQL certification to get the right job. eLearning Solutions is one of the renowned institutes for Oracle PL-SQL in Pune. We believe…
₹25,000.00

Pega Training Courses in Pune- Get Certified Now

Course details for Pega Training in Pune Elearning solution is the best PEGA training institute in Pune. PEGA is one of the Business Process Management tool (BPM), its development is based on Java and OOP concepts. The PAGA technology is mainly used to improve business purposes and cost reduction. PEGA…
₹27,000.00

SAP PP (Production Planning) Training Institute

SAP PP Training Institute in Pune SAP PP training (Production Planning) is one of the largest functional modules in SAP. This module mainly deals with the production process like capacity planning, Master production scheduling, Material requirement planning shop floor, etc. The PP module of SAP takes care of the Master…
X
WhatsApp WhatsApp us
Call Now Button