Realm Sizing and Sharding Strategy in Keycloak

Realm Sizing and Sharding Strategy in Keycloak

A complete, beginner-friendly guide to how big a Keycloak realm can safely grow, when to split identity data across multiple realms, and how to plan for scale before it becomes a crisis.

Imagine a single filing cabinet meant to hold every employee record for a small startup. It works fine at first, but as the company grows into thousands, then millions, of employees across dozens of departments, that one cabinet becomes impossible to search quickly, painful to reorganize, and terrifying to maintain — a single drawer jamming could disrupt access to everyone’s records at once. Eventually, someone has to decide: do we get a bigger, better-organized cabinet, or do we split records across several cabinets by department or region? This exact dilemma — one big container versus several smaller, well-planned ones — is what Realm Sizing and Sharding Strategy addresses for Keycloak.

1What Is Realm Sizing and Sharding?

Let’s establish clear definitions before exploring the decision-making involved.

In Keycloak, the open-source Identity and Access Management (IAM) server built by Red Hat, a realm is the top-level container for a set of users, clients, roles, and configuration — essentially a self-contained security domain. Realm sizing refers to understanding and planning for how much data (users, sessions, clients) a single realm can comfortably hold while continuing to perform well and remain operable. Sharding refers to the strategy of deliberately splitting what could have been one enormous realm’s worth of data across multiple realms (or, more broadly, multiple Keycloak deployments) to keep each individual piece manageable.

Simple Analogy

Think of a massive public library trying to decide how to organize millions of books. One option is a single enormous room with every book shelved together — technically possible, but finding anything becomes slow, and any disruption in that one room (a burst pipe, a fire alarm) affects the entire collection at once. The alternative is splitting the collection into multiple branch libraries, each serving a specific neighborhood or subject area, so problems in one branch don’t affect the others and each branch stays a manageable size. Realm sharding is choosing to run multiple “branch libraries” (realms) instead of one impossibly large central one.

It’s important to understand that Keycloak doesn’t have a single, universal “maximum realm size” number that applies to every deployment — the practical ceiling depends heavily on factors like the underlying database’s performance, the caching and clustering configuration, the number of concurrent active sessions, and the hardware resources allocated to the deployment. Realm sizing is therefore less about memorizing a fixed limit and more about understanding which factors actually constrain a realm’s growth, so an organization can plan proactively rather than discovering the limit painfully in production.

Term

Realm

Keycloak’s top-level isolation boundary — a self-contained set of users, clients, roles, and configuration.

Term

Sharding

Deliberately dividing a large dataset (in this case, identity data) across multiple smaller containers instead of one large one.

Term

Vertical Scaling

Making a single realm’s underlying infrastructure (database, servers) more powerful to handle more load.

Term

Horizontal Scaling

Adding more instances or splitting data across more units (like more realms or more database shards) rather than making one unit bigger.

i
Why This Matters

Getting realm sizing wrong in either direction has real costs: an over-large, unsplit realm risks performance degradation and a larger “blast radius” if something goes wrong, while premature or unnecessary sharding adds operational complexity — more realms to manage, more configuration to keep consistent — without a corresponding benefit.

2Why Realm Sizing Becomes a Problem at Scale

Let’s examine exactly which pressures cause a growing realm to strain.

As a realm accumulates more users, more active sessions, and more clients, several distinct subsystems inside Keycloak come under increasing pressure, and they don’t all strain at the same rate or for the same reason.

Pressure Point

Database Size and Query Performance

A relational database holding millions of user records and their associated data can experience slower queries — especially searches, exports, and bulk administrative operations — as the tables grow.

Pressure Point

Active Session Volume

Every concurrently logged-in user consumes session state, held in the distributed caching layer, which must be sized to comfortably hold peak concurrent session counts.

Pressure Point

Administrative Console Responsiveness

Searching, filtering, and browsing a realm with an extremely large number of users or clients through the Admin Console can become noticeably slower as the dataset grows.

Pressure Point

Blast Radius of Incidents

A configuration mistake, a bad deployment, or an unexpected load spike affecting one realm impacts every user and application relying on that realm simultaneously — a larger realm means a larger group affected by any single incident.

None of these pressures appear suddenly at some universal threshold — they build gradually, and different organizations will notice strain at very different scales depending on their specific database technology, hardware, caching configuration, and usage patterns (a realm with a modest user count but extremely high login frequency can strain differently than a realm with many users who rarely log in). This is precisely why realm sizing requires ongoing measurement and planning rather than a one-time decision made at initial setup.

“The right realm size isn’t a fixed number — it’s the point where your specific infrastructure, usage pattern, and operational tolerance for risk intersect.”

3Architecture & Components Affected by Sizing

Let’s map out which parts of Keycloak’s architecture are most directly shaped by realm sizing decisions.

Component

Relational Database

Stores all persistent realm data. Its schema design, indexing, and hardware capacity are the foundation of how large a realm can grow before queries slow down.

Component

Distributed Cache/Session Grid

Holds active session and runtime state in memory across the Keycloak cluster, sized according to peak concurrent session volume.

Component

Keycloak Server Cluster

The set of running Keycloak instances processing requests for the realm, which can be scaled horizontally (more instances) to handle more concurrent traffic.

Component

User Federation Providers

External identity sources (like LDAP directories) linked to a realm, whose own performance and scale characteristics also factor into overall realm sizing.

When an organization decides that sharding is the right strategy, it typically chooses one or more dimensions along which to split its identity data into separate realms. Common sharding dimensions include splitting by business unit, by geographic region, by product line, or by customer/tenant (particularly relevant for B2B SaaS providers, where the Organizations feature and realm sharding can sometimes be complementary strategies at different scales).

Sharding by Business Unit

A large enterprise with genuinely independent divisions (each with its own IT policies and applications) might split realms along those existing organizational boundaries, keeping each division’s identity footprint separately manageable.

Sharding by Region

A global organization might split realms by geography, both to keep each realm’s size manageable and to align with data residency or latency considerations for users in different parts of the world.

Sharding by Product Line

A company running multiple distinct products might use a separate realm per product, keeping each product’s user base and configuration independent, and limiting the blast radius of any one product’s identity issues.

graph TD
    A[Single Massive Realm] -->|Sharding Decision| B[Realm: Region - EU]
    A -->|Sharding Decision| C[Realm: Region - APAC]
    A -->|Sharding Decision| D[Realm: Region - Americas]
    B --> E[Own DB Load, Own Cache, Own Blast Radius]
    C --> F[Own DB Load, Own Cache, Own Blast Radius]
    D --> G[Own DB Load, Own Cache, Own Blast Radius]
        
FIG 1 — Splitting one large realm into multiple smaller, regionally sharded realms.

4The Sizing & Sharding Decision Flow

Let’s walk through the process a team typically follows to decide whether and how to shard.

1

Measure Current Realm Metrics

Gather concrete numbers: total user count, peak concurrent sessions, database size and query latency trends, and Admin Console responsiveness under real usage.

2

Project Future Growth

Estimate how these metrics will change over the next relevant planning horizon, based on business growth plans, expected new customer or employee onboarding, and historical growth trends.

3

Identify the Binding Constraint

Determine which specific pressure point (database performance, session cache capacity, administrative usability, blast-radius risk tolerance) is likely to be hit first as growth continues.

4

Evaluate Vertical Scaling First

Consider whether simply increasing database and server capacity (a bigger cabinet) can comfortably accommodate projected growth without the added complexity of sharding.

5

Choose a Sharding Dimension (If Needed)

If vertical scaling alone won’t suffice, or if organizational or compliance reasons favor separation anyway, select the most natural dimension for splitting — region, business unit, product, or tenant.

6

Plan the Migration

Design how existing users, clients, and configuration will be moved from the original realm into the new, sharded realms, including how applications will be updated to point at the correct realm for each user.

7

Execute and Monitor

Carry out the migration carefully, monitoring each new realm’s metrics closely afterward to confirm the sharding decision actually relieved the original pressure points.

sequenceDiagram
    participant Team as Platform Team
    participant KC as Keycloak
    participant DB as Database
    Team->>KC: Measure user count, sessions, latency
    Team->>DB: Analyze query performance trends
    Team->>Team: Project growth, identify binding constraint
    Team->>Team: Evaluate vertical scaling feasibility
    alt Vertical scaling sufficient
        Team->>KC: Increase capacity, monitor
    else Sharding needed
        Team->>Team: Choose sharding dimension
        Team->>KC: Plan and execute realm split/migration
        Team->>KC: Monitor new realms post-migration
    end
        
FIG 2 — The decision process for choosing between scaling up and sharding out.
!
Common Misconception

Sharding is not automatically the “more advanced” or “better” choice compared to vertical scaling. Many organizations comfortably run a single, well-tuned, appropriately-scaled realm for a very long time — sharding introduces real complexity and should be adopted because it solves a genuine, measured problem, not simply because it feels like the more sophisticated approach.

5Internal Working: What Actually Limits a Realm’s Size

Let’s dig into the specific technical mechanisms that determine practical realm capacity.

Keycloak stores nearly all persistent realm data — users, credentials, roles, clients, and configuration — in a relational database. The performance characteristics of that database, including how well its tables are indexed and how efficiently common queries (like searching for a user by username or email) execute as row counts grow, is one of the most fundamental factors shaping practical realm size.

Simple Analogy

Think of a database index like the index at the back of a textbook. A well-organized index lets you jump straight to the page you need regardless of how thick the book is. A book with no index, or a poorly organized one, forces you to flip through page after page — and that search gets progressively more painful the thicker the book grows. A realm’s database performance under growth depends heavily on whether its “index” (both literal database indexes and the overall schema and query design) stays efficient as the number of “pages” (rows) increases.

Beyond the database, Keycloak’s distributed caching layer (which manages active sessions, authentication session state, and various runtime caches) has its own capacity considerations, primarily driven by available memory across the cluster. A realm with an extremely high number of concurrently active sessions needs proportionally more cache capacity, independent of how many total (not concurrently active) user accounts exist in that realm.

Limiting Factor

Total User Count

Affects database storage size and the performance of bulk or search-oriented administrative operations more than everyday login performance.

Limiting Factor

Concurrent Active Sessions

Directly affects memory usage in the distributed cache layer and is often a more immediate scaling pressure than raw total user count.

Limiting Factor

Client Count

A large number of registered applications (clients) within one realm can add overhead to certain administrative and token-issuance operations that need to consider client-specific configuration.

Limiting Factor

Login Throughput

The rate of new authentication requests per second stresses the Keycloak server cluster’s processing capacity and the database’s write throughput, somewhat independently of total stored user count.

i
Key Insight

“Realm size” is really a shorthand for several distinct metrics — total users, concurrent sessions, login throughput, and client count — that don’t necessarily grow together in lockstep. A thorough sizing exercise measures each of these separately rather than treating “realm size” as one single number.

6Data Flow & Lifecycle of a Sharding Migration

Let’s trace what actually happens to data during a realm-splitting migration.

1

Assessment and Grouping

Existing users, clients, and configuration in the original realm are categorized according to the chosen sharding dimension — for example, tagging each user with the region their new realm should belong to.

2

New Realm Provisioning

Each target realm is created with the appropriate configuration — clients, roles, themes, and security policies — mirroring or adapting what existed in the original realm.

3

User and Data Export

User accounts and their associated data belonging to each group are exported from the original realm, typically using Keycloak’s export tooling or the Admin REST API.

4

Import Into Target Realms

The exported data is imported into the corresponding new realm, with careful attention to preserving credentials, role mappings, and any custom attributes.

5

Application Reconfiguration

Applications previously pointing at the single original realm are updated to direct each user (or each deployment/tenant) to their correct new realm.

6

Validation and Cutover

Both old and new realms may run in parallel briefly for validation before traffic is fully cut over to the new, sharded realms.

7

Decommissioning the Original Realm

Once migration is confirmed successful and stable, the original monolithic realm is retired.

!
Migration Reality

Realm-splitting migrations are rarely instantaneous, single-step operations. They typically involve a carefully staged process with validation checkpoints, precisely because identity data errors (a lost credential, a broken role mapping) directly and immediately affect real users’ ability to log in.

These two strategies address the same underlying pressure in fundamentally different ways.

AspectVertical ScalingSharding
ApproachMake the single realm’s infrastructure more powerfulSplit data across multiple, smaller realms
Operational complexityLower — one realm to manage and monitorHigher — multiple realms, configuration consistency, and cross-realm coordination
Blast radiusRemains large — one realm still serves everyoneReduced — an incident in one shard affects only that shard’s users
Migration effortMinimal — largely an infrastructure upgradeSignificant — requires careful data migration and application updates
Best fitGrowth that a stronger database/cluster can comfortably absorbGrowth patterns aligned with natural organizational, regional, or tenant boundaries

A useful mental model is that vertical scaling buys time and simplicity, while sharding buys long-term structural headroom and reduced blast radius at the cost of ongoing operational complexity. Many organizations reasonably choose to scale vertically for as long as it remains sufficient, reserving sharding for the point where either technical limits or organizational/compliance reasons make a genuinely compelling case for it.

i
Practical Guidance

Sharding decisions are easiest to get right when they align with boundaries that already exist for other reasons — separate business units, separate regions with different compliance requirements, or separate customer tenants — rather than being an arbitrary technical split imposed purely for performance reasons.

8Advantages, Disadvantages & Trade-offs

Sharding is a powerful tool, but every tool has costs worth weighing honestly.

Advantages of Sharding

  • Keeps each individual realm’s data volume within a well-understood, comfortable operating range.
  • Reduces the blast radius of any single incident, misconfiguration, or outage.
  • Can align naturally with organizational, regional, or compliance boundaries that already exist for other reasons.
  • Allows different shards to be scaled, tuned, or even upgraded independently of one another.

Disadvantages of Sharding

  • Multiplies the number of realms an administrative team must configure, monitor, and keep consistent.
  • Cross-realm operations (like a report spanning all users organization-wide) become more complex to build and maintain.
  • Migration itself is a significant, risk-bearing project that must be executed carefully.
  • Applications may need updates to correctly route users to their appropriate realm.

The right balance point depends heavily on organizational context. A company already structured into clearly separate divisions or regions may find sharding along those lines relatively natural and low-friction. A company with a single, unified user base and no compelling organizational boundary to split along may find that investing in strong vertical scaling and careful database tuning delivers most of the benefit with far less operational overhead.

9Security Considerations

Sizing and sharding decisions carry security implications that deserve deliberate attention.

Consideration

Consistent Security Policy Across Shards

When splitting into multiple realms, ensure security-relevant settings — password policies, multi-factor authentication requirements, session timeout values — remain consistent, unless a deliberate, documented reason justifies a difference for a specific shard.

Consideration

Migration Data Integrity

A realm-splitting migration that mishandles credentials, role mappings, or federated identity links could silently create incorrect access — either granting too much or accidentally locking out legitimate users.

Consideration

Reduced Blast Radius as a Security Benefit

Beyond pure performance, sharding limits how many users are affected by a security incident, compromised credential, or misconfiguration confined to a single realm.

Consideration

Cross-Shard Audit Visibility

Splitting into multiple realms can make organization-wide security auditing and reporting more complex, requiring deliberate tooling to aggregate visibility across all shards.

ANTI-PATTERN-01 Avoid
Problem

Sharding realms for performance reasons without also establishing a plan for how organization-wide security auditing, compliance reporting, and policy consistency will be maintained across all the resulting shards.

Why It’s Harmful

Without deliberate cross-shard visibility, security drift between realms can go unnoticed — one shard might quietly fall behind on a security policy update, creating an inconsistent, harder-to-audit security posture across the organization.

Correct Approach

Treat cross-shard configuration consistency and unified audit visibility as first-class requirements of any sharding project, not an afterthought to be figured out later.

!
Common Mistake

Rushing a realm migration under performance pressure without thorough testing of credential and role-mapping integrity, risking subtle access problems that only surface after real users start hitting the new realm in production.

10Monitoring, Logging & Metrics

Good sizing decisions depend entirely on good measurement — let’s cover what to track.

Metric

Database Query Latency Trends

Tracking how key query types (user search, login lookups) perform over time as data volume grows is one of the earliest warning signs of an approaching sizing ceiling.

Metric

Peak Concurrent Sessions

Monitoring the highest simultaneous session count (not just total registered users) helps size the distributed cache layer appropriately.

Metric

Admin Console and API Responsiveness

Regularly checking how administrative operations perform under real data volume catches usability degradation before it becomes a significant operational burden.

Metric

Growth Rate Over Time

Tracking user count, session volume, and client count growth rates (not just their current absolute values) enables meaningful forward projection for capacity planning.

Users
Total realm population tracked over time
Sessions
Peak concurrent load, the real cache-sizing driver
Latency
Query and login performance trend, the earliest warning sign

Capacity Planning Reviews

Mature Keycloak operations teams schedule periodic capacity planning reviews — comparing current growth trends against known infrastructure limits — well before those limits are expected to be reached, giving enough lead time to plan and execute either a vertical scaling upgrade or a sharding project deliberately, rather than reactively under pressure.

11Design Patterns & Anti-Patterns

A handful of recurring patterns show up across teams that have navigated realm growth successfully.

Pattern: Shard Along Existing Boundaries

Choosing a sharding dimension that already exists organizationally (business unit, region, product line) tends to produce cleaner, easier-to-reason-about splits than an arbitrary technical division invented purely to balance load.

Pattern: Incremental Migration with Dual Running

Running the original and new realms in parallel during a migration, gradually shifting traffic and validating correctness at each step, reduces the risk of a single, high-stakes cutover event.

Pattern: Configuration Templating Across Shards

Maintaining realm configuration as version-controlled, reusable templates (rather than manually configuring each realm individually) helps keep security policy and settings consistent across many sharded realms.

ANTI-PATTERN-02 Avoid
Problem

Waiting until a realm is already experiencing severe, user-visible performance problems before beginning to plan a sizing or sharding response.

Why It’s Harmful

Sizing and sharding projects — especially migrations — take meaningful time to plan and execute safely. Starting only once problems are already acute forces rushed decisions under pressure, increasing the risk of migration errors and extending the period of degraded user experience.

Correct Approach

Establish proactive capacity monitoring and periodic review cycles well before any specific metric approaches a known limit, so sizing or sharding decisions can be made calmly and executed carefully, on a timeline the team controls.

12Best Practices & Common Mistakes

A concise checklist for teams planning ahead for realm growth.

Best Practice

Measure Multiple Metrics Separately

Track total users, concurrent sessions, client count, and login throughput independently rather than treating “realm size” as one number.

Best Practice

Plan Capacity Reviews on a Schedule

Don’t wait for a crisis — review growth trends against known limits on a regular, proactive cadence.

Best Practice

Exhaust Vertical Scaling First (Where Reasonable)

Confirm that a stronger database and cluster genuinely can’t absorb projected growth before committing to the added complexity of sharding.

Best Practice

Choose Sharding Dimensions That Already Exist

Align splits with real organizational, regional, or tenant boundaries rather than inventing an artificial technical division.

Best Practice

Template Realm Configuration

Manage realm settings as reusable, version-controlled templates to keep multiple shards consistent over time.

Best Practice

Migrate Incrementally, With Validation

Favor staged migrations with checkpoints over single, high-stakes cutover events.

!
Common Mistake

Confusing total registered user count with the metric that actually matters for a given pressure point. A realm with millions of rarely-active accounts may face very different constraints than a realm with far fewer users who are almost constantly logged in — sizing decisions should be grounded in the specific metric under real strain.

13Real-World & Industry Examples

Realm sizing and sharding decisions show up across a wide range of organizations that grow into needing them.

Global Enterprises with Regional Divisions

Large multinational companies often shard realms by geography, aligning naturally with data residency requirements and regional IT autonomy, while also keeping each region’s realm at a comfortable operational size.

SaaS Platforms Scaling Past Early-Stage Simplicity

A growing SaaS provider that started with one realm for all customers may eventually shard by customer segment or region as its user base scales into the millions, to keep database and session performance predictable.

Conglomerates with Distinct Business Units

Corporate groups spanning genuinely separate business lines often maintain separate realms per business unit from early on, both for organizational clarity and to naturally cap the growth of any single realm.

Educational Institutions with Multiple Campuses

Universities or school systems with many semi-autonomous campuses sometimes shard realms by campus or department, aligning identity boundaries with existing administrative structures.

Across these examples, successful sharding decisions tend to follow organizational logic that already exists, rather than being purely reactive technical fixes — reinforcing that the best sharding strategy is usually the one that makes sense on a whiteboard even before performance metrics are considered.

14Frequently Asked Questions

Common questions teams ask when first thinking about realm sizing and sharding.

Q1Is there a fixed maximum number of users a Keycloak realm can hold?

There is no single universal number — practical capacity depends on the underlying database, hardware, caching configuration, and usage patterns. Sizing should be based on measuring your specific deployment’s behavior rather than assuming a fixed industry-wide limit.

Q2Should every organization eventually shard its realms?

No. Many organizations run a single, well-tuned, appropriately-scaled realm indefinitely without ever needing to shard. Sharding is a response to specific, measured pressure points or organizational needs, not an inevitable milestone every deployment must reach.

Q3Does the Organizations feature replace the need for realm sharding?

Not entirely — they solve related but distinct problems at different scales. Organizations helps manage many B2B tenants cleanly within one realm; realm sharding addresses situations where a realm itself (regardless of how tenants are organized within it) has grown too large for a single deployment to handle comfortably.

Q4What’s the biggest risk during a realm-splitting migration?

Data integrity issues — particularly around credentials, role mappings, and federated identity links — pose the greatest risk, since errors here directly and immediately affect real users’ ability to authenticate correctly.

Q5Can total user count and concurrent session count strain a realm differently?

Yes. Total user count primarily affects database size and search/administrative performance, while concurrent session count primarily affects the distributed cache layer’s memory usage. A realm can have very high numbers on one metric and comfortable numbers on the other.

Q6How early should capacity planning for realm sizing begin?

Ideally, well before any specific metric approaches a known limit — establishing regular monitoring and periodic capacity reviews from early in a deployment’s life gives the team enough lead time to plan a calm, well-tested response rather than a rushed one.

15Summary and Key Takeaways

Realm Sizing and Sharding Strategy is fundamentally about recognizing that a Keycloak realm’s practical capacity is shaped by several distinct, independently measurable pressures — database performance, concurrent session volume, administrative usability, and incident blast radius — rather than one fixed limit. Organizations facing genuine growth-driven strain have two broad responses available: scaling the existing realm’s infrastructure vertically, or deliberately sharding identity data across multiple realms along a natural organizational boundary. Neither choice is universally correct; the right answer depends on measured data, projected growth, and how well a given sharding dimension aligns with the organization’s existing structure.

Key Takeaways

  • There is no universal maximum realm size — practical capacity depends on database performance, caching configuration, and hardware, and must be measured for each specific deployment.
  • Different metrics strain a realm in different ways — total users, concurrent sessions, client count, and login throughput should be tracked and reasoned about separately.
  • Vertical scaling and sharding are complementary strategies, not competitors — most organizations should exhaust reasonable vertical scaling before committing to the added complexity of sharding.
  • The best sharding dimensions align with existing organizational boundaries — region, business unit, or product line — rather than being purely arbitrary technical splits.
  • Sharding reduces blast radius but increases operational overhead — more realms to configure, monitor, and keep consistent.
  • Realm migrations carry real risk to credential and role-mapping integrity and should proceed incrementally, with validation checkpoints, not as a single high-stakes cutover.
  • Proactive, scheduled capacity planning — rather than reactive scrambling once problems are already visible — is what allows sizing decisions to be made calmly and executed safely.