ERPClimb logoERPClimb
Multitenancy and extensions
SAP Cloud Application Programming Modeladvanced 5 min read

Multitenancy and extensions — SAP Guided Tutorial

Free sample lesson · open to everyone

Key takeaway

Turning a CAP app into a SaaS application: the MTX sidecar, tenant subscription and per-tenant HDI containers, tenant isolation in code, upgrading all tenants on each release, and letting customers extend the model safely.

In this lessonShow sections

Visual overview

Multitenancy and extensions — learning sheetFlow: Subscribe: SaaS registry → MTX: tenant container → Request: cds.context.tenant → Upgrade: all tenants. Key ideas: HOW SUBSCRIPTION WORKS; TENANT ISOLATION; UPGRADES; EXTENSIBILITY. Watch out: Module-level caches without tenant keys leak data between customers.ERPClimb.com / CAP / LESSON 11/12 / LEARNING SHEETMultitenancy and extensions1 · HOW IT FLOWS1SubscribeSaaS registry2MTXtenant container3Requestcds.context.tenant4Upgradeall tenants2 · KEY IDEAS EXPLAINEDHow Subscription WorksThe provider deploys once; consumer subaccountssubscribe in the BTP cockpit.The SaaS registry calls the subscription callback;MTX provisions the tenant's HDI container anddeploys the model.Tenant IsolationNever store tenant data in module-level variablesor shared caches without the tenant ID in the key.Background jobs must set the tenant contextexplicitly (`cds.tx({ tenant }, ...)`).UpgradesAfter deploying a new version, run the tenantupgrade (via MTX APIs or the sidecar's upgradejob) for all tenants.Monitor failures per tenant; one broken tenantmust not block the others.Extensibility`cds add extensibility` lets customers add fieldsand entities to defined extension points.Restrict what can be extended (e.g. allowedentities, limits on new fields) in theextensibility config.3 · REMEMBER · WATCH OUTRemember`cds add multitenancy` adds the MTX sidecar (@sap/cds-mtxs) thathandles subscription, tenant databases and upgrades.When a customer subscribes, the SaaS Provisioning service calls theapp; MTX creates a separate HDI container for that tenant via Service…Each request carries the tenant from the token; CAP routes DB accessto that tenant's container (cds.context.tenant).!Watch outModule-level caches without tenantkeys leak data between customers.4 · IN A REAL PROJECTA SaaS app cached product prices in a module-level Map keyed by product ID. Customer B briefly saw customerA's prices after A viewed the same product IDs. Keying the cache by tenant and adding a two-tenant regressiontest fixed the leak; a security review then found and fixed the same pattern in a background job.Original ERPClimb learning sheet • Verify against your SAP release and edition

The situation: a real project scenario

A SaaS app cached product prices in a module-level Map keyed by product ID. Customer B briefly saw customer A's prices after A viewed the same product IDs. Keying the cache by tenant and adding a two-tenant regression test fixed the leak; a security review then found and fixed the same pattern in a background job.

Explanation

Turning a CAP app into a multitenant SaaS application means moving from "one app for one company" to "one app deployed once, serving many customers," and the mechanics of that are handled by the MTX sidecar, added with cds add multitenancy.

When a customer subscribes through the BTP cockpit, the SaaS Provisioning service calls back into your app, and MTX provisions a dedicated HDI container for that tenant via the Service Manager and deploys the data model into it. Unsubscribing reverses this, deleting or offboarding the tenant's data according to whatever retention design you've chosen.

At request time, the tenant is carried in the token, and CAP uses that (available as cds.context.tenant) to route database access to the correct tenant's container automatically.

The place multitenancy most often breaks in practice is isolation outside the database itself.

A cache or any other state held in a module-level variable, shared across all requests regardless of tenant, will leak one customer's data to another unless the tenant ID is part of the cache key — the fix is as simple as keying the cache by ${cds.context.tenant}:${productID} instead of just the product ID.

Background jobs are just as exposed: because they don't arrive with an incoming request's token, they need to set the tenant context explicitly, for example await cds.tx({ tenant: t }, tx => tx.run(...)), or they'll either fail or, worse, silently operate against the wrong tenant.

Destinations and identity configuration are also tenant-specific and should be looked up per tenant rather than cached once at application startup.

Every release also has to be rolled out to every subscribed tenant, not just deployed once centrally — after deploying a new version, the tenant upgrade (through the MTX APIs or the sidecar's upgrade job) needs to run for all tenants, and a schema change has to work correctly against every tenant's data, including tenants that have added their own extensions. It's worth monitoring upgrade failures per tenant specifically, since one tenant's broken upgrade should never be allowed to block the rest from getting the new release.

Extensibility is the flip side of multitenancy: cds add extensibility defines extension points where customers can add their own fields or entities, but those extension points and their limits (which entities can be extended, how many new fields are allowed) need to be explicit in your configuration, both so core logic isn't broken by an unexpected extension and so a customer's extensions reliably survive your next upgrade.

To work through this hands-on: add multitenancy and run locally with two mocked tenants using the with-mtx profile; subscribe both tenants and write data as each, confirming neither can see the other's rows; deliberately introduce a module-level cache bug, observe the leak, and fix it by keying on tenant; then deploy a schema change and run the upgrade across both tenants. By the end you should be able to prove, with two real tenants, that data, caches, destinations, and background jobs are all properly isolated.

Code example

ABAP Code
// Tenant-aware cache keyconst cache = new Map();function getPrice(productID) {  const key = `${cds.context.tenant}:${productID}`;  return cache.get(key);}// Background job: always set tenant explicitlyawait cds.tx({ tenant: t }, tx => tx.run(SELECT.from('Orders').where({ status: 'OPEN' })));

Watch out

Common mistakes

  • Caching tenant data globally without the tenant ID.
  • Running scheduled jobs without setting the tenant context.
  • Forgetting to upgrade tenants after deploying a new model.
  • Allowing unrestricted customer extensions that break core logic.

Do this

Best practices

  • Treat tenant isolation as a security requirement with automated two-tenant tests.
  • Automate tenant upgrades in the release pipeline with per-tenant reporting.
  • Define explicit extension points and limits for customer extensions.

In the interview

Interview angle

Multitenancy questions focus on isolation and operations: how subscription provisions a tenant container, how CAP routes requests by tenant, where isolation commonly breaks (caches, jobs, destinations), and how you upgrade all tenants safely.

Practice this lesson

Original educational case studies for SAP consultants, not official SAP certification exam questions. Verify product edition and implementation details against the current SAP documentation.

Quick check

0 of 4 questions attempted

Question 1 of 4

In a CAP multitenant app, where is each customer's data stored by default?

Practise this in an interview

Answer in your own words and get instant feedback, the way an interviewer would judge it.

1Explain what happens when a customer subscribes to a CAP-based SaaS app on BTP.

2How do you test and guarantee tenant isolation in a CAP SaaS app?

Was this lesson helpful?

Keep exploring

Check before use: lesson examples are educational, not a guarantee of correctness for your SAP edition or release. Sample code has not been executed on your SAP system. Confirm API availability, syntax, authorizations and business impact in an approved development or test environment.