Multitenancy and extensions — SAP Guided Tutorial
Free sample lesson · open to everyone
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 sectionsHide sections
Visual overview
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
// 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?
Question 2 of 4
A background job processes open orders. What must it do in a multitenant app?
Question 3 of 4
After deploying a new version with model changes, what is still required?
Question 4 of 4
Which code pattern is a tenant data leak risk?
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.