Realm-per-Tenant Architecture in Keycloak: One Server, Many Locked Rooms
When a single Keycloak deployment needs to serve many separate customers without their users, roles, or logins ever mixing, Realm-per-Tenant is the most direct way to draw that line. Here's exactly what it is, how it works internally, and where it starts to strain.
Picture a large office building with a single reception desk downstairs but a separate, independently keyed floor for every company renting space in it. Company A’s employee badge opens only Company A’s floor — it doesn’t even know Company B’s floor exists. The building owner (the Keycloak server) still runs the elevators and the electricity for everyone, but each floor manages its own staff list, its own visitor policy, and its own door locks. Realm-per-Tenant architecture in Keycloak is this office building, and each “realm” is one of those independently keyed floors.
1What Is Realm-per-Tenant Architecture?
To understand this pattern, we first need a clear picture of what a “realm” and a “tenant” each mean on their own.
What is a realm?
A realm is Keycloak’s built-in isolation boundary. Every realm manages its own users, credentials, roles, groups, clients, and login screens, completely separately from every other realm on the same server. Two realms never share a user database, even if two people in different realms happen to use the exact same email address — Keycloak treats them as entirely unrelated identities.
What is a tenant?
A tenant is a business concept, not a Keycloak concept — it simply means one distinct customer, organization, or client using a shared platform. A software-as-a-service product that serves hundreds of companies, where each company’s data and users must never mix with another’s, has hundreds of tenants.
Putting the two together
Realm-per-Tenant architecture is the design decision to give each tenant its own dedicated Keycloak realm. Tenant “Acme Corp” gets the acme realm, tenant “Globex Inc” gets the globex realm, and so on — all running on the same physical Keycloak deployment, but with Keycloak’s native isolation doing the heavy lifting of keeping them apart.
This is like a bank that gives every business customer their own separate safe deposit box inside the same vault room. The vault (Keycloak server) is shared infrastructure, but nobody can open a box that isn’t theirs, and the bank never has to build a whole new vault every time a new customer signs up.
Realm-per-Tenant is one of two common patterns for multi-tenancy in Keycloak — the other being a single shared realm with one Keycloak client per tenant. This post focuses specifically on the realm-based approach and where it shines or strains.
2Architecture & Components
A realm-per-tenant deployment is built from a handful of moving parts that repeat, once per tenant, across the same Keycloak instance.
Tenant Realm
One realm per customer — for example acme — holding that tenant’s users, groups, roles, and clients.
Master Realm
Keycloak’s built-in top-level administrative realm, used only for managing the server and creating other realms — never for hosting application users.
Per-Realm Theme & Branding
Each tenant’s realm can have its own login page look, logo, and color scheme, independent of every other tenant.
Per-Realm Identity Providers
A tenant that wants to let its users log in via their own corporate Active Directory or Google Workspace configures that connection only inside its own realm.
How the application routes requests to the right realm
Because every realm has its own distinct login and token endpoints, the calling application — or a gateway in front of it — needs to know which realm a given request belongs to before it can authenticate. This is commonly resolved from a subdomain (acme.myapp.com), a custom domain mapped to a specific tenant, or a path segment, and then used to pick the correct realm’s OpenID Connect configuration for that request.
flowchart LR
A[Request: acme.myapp.com] --> B[Tenant Router]
C[Request: globex.myapp.com] --> B
B -->|acme| D[Realm: acme]
B -->|globex| E[Realm: globex]
D --> F[(Acme Users, Roles, Clients)]
E --> G[(Globex Users, Roles, Clients)]
The tenant router acts like a hotel concierge who hears a guest’s room number and points them straight to the correct floor. The concierge never checks anyone’s ID for a floor they didn’t ask for — the routing decision and the identity check are two separate, sequential steps.
3Internal Working: What Isolation Actually Covers
“Isolation” is a word that gets used loosely — Keycloak’s realm boundary is specific about exactly what it separates.
| Dimension | Isolated Per Realm? |
|---|---|
| User accounts and credentials | Yes — entirely separate user stores |
| Roles and groups | Yes — a role in one realm has no meaning in another |
| Clients (applications) | Yes — a client registered in one realm cannot be used from another |
| Authentication flows and MFA policy | Yes — each realm configures its own login and MFA requirements |
| Identity provider connections (LDAP, SAML, social login) | Yes — configured independently per realm |
| Sessions and single sign-on | Yes — SSO only applies within one realm; it never silently spans realms |
| Underlying database and Keycloak server process | No — this remains a single shared deployment |
What this means for a single human being
If the same person is genuinely a member of two different tenants — say, a consultant who works with both Acme and Globex — Keycloak treats them as two completely separate identities, one per realm, even if both accounts use the same email address. There is no automatic linking between them. Any experience where “the same person” needs to move between tenant realms has to be deliberately built, typically using realm-to-realm identity brokering.
Realm isolation does not mean the same physical Keycloak server can’t serve requests for many realms extremely quickly, one after another. It means the data and configuration inside each realm cannot see or reference another realm’s data — the shared server process is simply the plumbing underneath.
4Data Flow & Lifecycle of a Tenant Realm
Onboarding a new tenant in a realm-per-tenant system follows a repeatable sequence — and automating it well is what separates a smooth-scaling platform from a painful one.
New Tenant Signs Up
A business event — a signup form, a sales handoff — triggers the need for a brand-new isolated identity space.
Realm Provisioned Programmatically
A new realm is created via Keycloak’s Admin REST API or a realm import template, typically pre-populated with standard roles, default clients, and baseline authentication flows.
Tenant-Specific Configuration Applied
Branding, custom domain, identity provider connections, and any tenant-specific policy overrides are layered on top of the baseline template.
Routing Updated
The tenant’s subdomain or path is registered in the routing layer so incoming requests resolve to the newly created realm.
Ongoing Operation & Eventual Offboarding
The realm is monitored and maintained like any other, and if the tenant relationship ends, the realm can be exported for records and then deleted — cleanly removing every user, role, and client that belonged to it.
Because realm creation is fully scriptable through the Admin REST API and realm export/import files, most production platforms treat “spin up a new tenant” as a one-command, automated operation rather than a manual console task.
5Advantages and Trade-offs
Advantages
- The strongest form of tenant isolation Keycloak offers, since users, roles, and clients literally cannot cross realm boundaries.
- Each tenant can have fully independent branding, login pages, MFA requirements, and identity provider connections.
- A tenant’s admin can be scoped entirely to their own realm, so they can manage their own users without any visibility into another tenant.
- Easy to reason about during a security review: “does this realm contain only this tenant’s data?” is a simple, direct question to answer.
- A single tenant’s realm can be exported, backed up, or deleted independently without touching any other tenant.
Disadvantages / Trade-offs
- A user who legitimately belongs to more than one tenant ends up with entirely separate identities, requiring extra work (such as identity brokering) to create a smooth experience.
- Realm creation, upgrades, and configuration changes multiply by the number of tenants, increasing operational overhead as the tenant count grows.
- Very high realm counts — commonly cited in the hundreds to low thousands — can introduce measurable performance overhead on a single Keycloak deployment.
- Cross-tenant reporting or analytics require pulling data out of many separate realms rather than querying one shared store.
6Security Considerations
The master realm should never host application users
Keycloak’s built-in master realm exists purely to administer the server itself and to create and manage other realms — it is not meant to hold application end users or application clients. Mixing tenant users into the master realm undermines the entire isolation model the architecture is built around.
Granting a tenant’s own administrator broad realm-management permissions instead of scoping them strictly to their own realm. A tenant admin account should never be able to see, let alone modify, another tenant’s realm.
Identity brokering between realms needs deliberate design
When the same human legitimately needs access to more than one tenant realm, the natural solution is to configure one realm as an identity provider for another, letting a user “sign in with their other realm’s account.” Left at Keycloak’s defaults, this can present the user with a confusing choice of login method every time, so production systems typically add custom logic to keep this transition invisible and predictable — without ever weakening the underlying separation between the two realms’ data.
Automating realm provisioning safely
Because new realms are usually created programmatically, the automation itself becomes a security-sensitive component. The credentials or service account used to call Keycloak’s Admin REST API for realm creation should be tightly scoped and audited, since it effectively has the power to create a brand-new isolated tenant space on demand.
Automated realm provisioning is like giving a hotel’s booking system the power to cut new room keys automatically. That’s efficient and necessary at scale — but the system that cuts the keys needs to be trusted and monitored just as carefully as the front desk staff would be.
7Monitoring, Logging & Metrics
Per-realm event visibility
Keycloak records login events, failed attempts, and admin actions scoped to the realm in which they occurred, which naturally supports giving each tenant’s own administrator visibility into only their own realm’s activity — a useful property for both operational monitoring and customer-facing audit trails.
Watching the realm count itself as a metric
Since Keycloak’s own performance characteristics can shift as the number of realms on a single deployment grows into the hundreds or beyond, tracking total realm count over time — alongside login latency and admin console responsiveness — gives an early warning before the platform approaches limits that would require splitting tenants across multiple Keycloak deployments.
Standard infrastructure monitoring tools such as Prometheus and Grafana can be pointed at Keycloak’s built-in metrics to track login activity, authentication latency, and potential brute-force patterns across all tenant realms from one dashboard, even though the realms themselves remain logically isolated.
Monitoring automated provisioning
Because new tenants are typically onboarded through an automated pipeline calling the Admin REST API, that pipeline’s own success and failure rate deserves its own monitoring — a silently failing tenant-provisioning job is a business-impacting incident, not just a technical one.
8Design Patterns & Anti-Patterns
Template Realm Pattern
A “golden” realm export is maintained as a template, containing baseline roles, default clients, and standard authentication flows. Every new tenant realm is created by importing this template and then layering tenant-specific overrides on top, keeping new tenants consistent from day one.
Tenant Router Layer
A dedicated routing component — separate from Keycloak itself — resolves an incoming request’s subdomain, custom domain, or path to the correct realm before any authentication call is made, keeping tenant-resolution logic in one place instead of scattered across the application.
Problem
Manually creating and configuring each new tenant’s realm by hand through the admin console.
Why It’s Harmful
Manual setup is slow, inconsistent between tenants, and error-prone at exactly the moment a platform is trying to grow its customer base quickly.
Correct Approach
Automate realm creation through the Admin REST API or realm import/export files, driven from the same signup workflow that onboards the tenant’s account.
Problem
Choosing Realm-per-Tenant for a platform that expects tens of thousands of small tenants, without validating Keycloak’s realm-count performance behavior at that scale first.
Why It’s Harmful
The strong isolation this pattern provides comes with real overhead as realm counts climb into the high hundreds and beyond, and discovering this only after committing to the architecture is expensive to reverse.
Correct Approach
Load-test realm creation and authentication throughput at a realistic projected realm count before committing, or consider the alternative single-realm, client-per-tenant pattern for very high tenant volumes with lighter isolation needs.
9Best Practices & Common Mistakes
Keep the master realm administrative only
Never host application end users or application clients inside the built-in master realm.
Script every realm-lifecycle step
Creation, configuration, export, and deletion should all be automatable, not click-through console tasks.
Design for the shared-user edge case early
If any user might plausibly need access to more than one tenant, plan identity brokering before it becomes an urgent support ticket.
Watch realm count against known scaling limits
Treat total realm count as a capacity metric, not just a business metric.
Common mistakes to avoid
Beyond the two anti-patterns above, teams frequently underestimate how much tenant-specific configuration drift accumulates over time — a template realm updated for new tenants doesn’t retroactively update existing ones, so a process for propagating shared policy changes (like a new password requirement) across every existing tenant realm needs to exist from the start. It’s also easy to forget that SSO does not span realms by default; assuming a logged-in session in one tenant realm will be recognized in another is a frequent source of confused bug reports.
10Real-World & Industry Examples
B2B SaaS Platforms
Business software sold to other companies commonly uses one realm per customer organization, since each customer expects its own branded login page, its own administrators, and a guarantee that its user list is never visible to another customer on the same platform.
Managed Identity Hosting Providers
Companies that offer “Keycloak as a service” to their own customers frequently isolate each paying customer into a dedicated realm on a shared underlying cluster, giving customers the experience of their own private identity server without the cost of a fully separate deployment.
.NET and Java Enterprise Platforms
Enterprise applications built on frameworks like ASP.NET or Spring frequently pair per-tenant Keycloak realms with application-level tenant scoping — such as request-scoped services that inject the current tenant ID and partitioned caches — combining Keycloak’s identity isolation with the application’s own data isolation.
| Scenario | Why Realm-per-Tenant Fits |
|---|---|
| B2B software with distinct company branding | Each tenant needs its own login theme and identity provider connections |
| Regulated industries needing strict data separation | Realm boundaries provide an auditable, structural isolation guarantee |
| Platforms offering delegated tenant administration | A tenant admin can be scoped entirely to their own realm |
11Frequently Asked Questions
No. The other common pattern keeps every tenant in one shared realm and uses a separate Keycloak client per tenant, relying on role-based access control for isolation instead of realm boundaries. It trades some isolation strength for significantly better performance at very high tenant counts.
Yes, but Keycloak treats them as two completely separate identities with independent credentials and lifecycles, even if the email address is identical. Linking them into a smooth single experience requires deliberate identity-brokering configuration.
There’s no hard limit, but real-world reports and documentation note measurable performance drops once a deployment reaches several hundred realms, though this has improved across newer Keycloak versions. Load-testing at your expected scale is the only reliable way to know your own limit.
No. The master realm is reserved for administering the Keycloak server and other realms. Application users and clients should always live in their own dedicated realms.
No. Single sign-on in Keycloak applies within one realm only. A user authenticated in one tenant’s realm is not automatically recognized in another tenant’s realm without explicit identity brokering set up between them.
12Summary and Key Takeaways
Realm-per-Tenant architecture gives every customer on a shared Keycloak deployment their own fully isolated identity space — separate users, roles, clients, login flows, and identity provider connections — while still running on one underlying server. It is the strongest isolation option Keycloak offers for multi-tenancy, and the easiest to reason about during a security review, but it trades that strength for real operational overhead: realm provisioning has to be automated, shared users across tenants need deliberate brokering, and very large tenant counts can strain a single Keycloak deployment’s performance.
Key Takeaways
- A realm is Keycloak’s native isolation boundary — users, roles, clients, and login flows never cross realm lines.
- Realm-per-Tenant maps one tenant to one realm — giving each customer a fully independent identity space on shared infrastructure.
- The master realm is administrative only — it should never host application end users or clients.
- A shared user becomes two identities — the same person in two tenant realms needs deliberate identity brokering to feel like one account.
- Automation is essential at scale — realm creation, configuration, and offboarding should be scripted through the Admin REST API, not done by hand.
- Realm count is a real performance variable — very high tenant counts can strain a single deployment, and should be load-tested rather than assumed.
- SSO stops at the realm boundary — a session in one tenant’s realm is never automatically recognized in another’s.