Realm Role vs. Client Role in Keycloak

Realm Role vs. Client Role in Keycloak

The complete beginner-to-production guide to choosing the right scope for a permission — and why getting this wrong quietly costs teams for years.

Picture a large office building again, but this time look at two very different kinds of access passes handed to a new employee. The first is a badge sticker that says “Building Staff” — it works at literally every door in the building, from the lobby to the roof deck, because building security itself issued it and every door reads the same universal sticker. The second is a small keycard that only unlocks the server room on the third floor — issued and understood solely by the IT department, meaningless to any other door in the building. Both are “permissions,” but they live at completely different scopes. Keycloak draws exactly this same line between a Realm Role and a Client Role, and confusing the two is one of the most common design mistakes teams make when they first start modeling permissions.

This guide puts the two side by side: what each one is, how they’re structured differently, how they travel into a token differently, and a clear, repeatable way to decide which one a new permission should actually be. No prior identity or security background is assumed — every term is explained the moment it appears.

1Core Concepts

Before comparing them side by side, let’s define each one cleanly on its own terms.

Realm Role: The Building-Wide Sticker

A Realm Role is a permission label defined at the top level of a Realm — Keycloak’s isolated universe of users and settings — and it is automatically recognized by every single Client (application) registered inside that Realm, with no extra setup required per application. Names like “employee,” “verified-customer,” or “premium-subscriber” are typical examples: broad, organization-wide facts about a user.

Client Role: The Server-Room Keycard

A Client Role is a permission label defined inside one specific Client, and it is meaningful only to that one application. A name like “billing-api:can-issue-refunds” means nothing at all to any other application in the Realm — only the billing application itself knows to look for and interpret it.

i
The One-Sentence Test

Ask: “Would this permission still make sense if I removed one specific application from the picture entirely?” If yes — it’s a Realm Role. If the permission becomes meaningless without that one specific application in mind — it’s a Client Role. This single question resolves the vast majority of real-world scoping decisions.

Everyday Analogy

“Registered Guest” printed on a hotel’s universal key card works at the gym, the pool, and the lobby lounge — a Realm Role, recognized everywhere on the property because the hotel itself issued it. “Suite 402 Access” only works on one specific door — a Client Role, meaningful only to that one room, issued and understood only in that narrow context.

2Side-by-Side Comparison

Seeing every dimension of difference laid out together makes the distinction concrete rather than abstract.
DimensionRealm RoleClient Role
Defined atThe Realm levelInside one specific Client
Visible toEvery current and future Client in the RealmOnly the one Client it belongs to
Typical naming“employee,” “verified-customer”“billing-api:can-issue-refunds”
Token locationrealm_access.rolesresource_access.<client-id>.roles
Best forBroad organizational factsFine-grained, application-specific permissions
Change impactPotentially wide — every connected app can see itNarrow — contained to one application
graph TD
    Realm["Realm"]
    Realm --> RR["Realm Role: employee"]
    Realm --> ClientA["Client: billing-api"]
    Realm --> ClientB["Client: hr-portal"]
    ClientA --> CRA["Client Role: can-issue-refunds"]
    ClientB --> CRB["Client Role: can-approve-leave"]
    User["User: sara"] --> RR
    User --> CRA
    RR -.->|"seen by"| ClientA
    RR -.->|"seen by"| ClientB
    CRA -.->|"seen only by"| ClientA
    
Fig 2.1 — A Realm Role visible to every Client at once, versus a Client Role confined to just one

3Where Each One Lives Inside a Token

The clearest technical difference between the two shows up the moment you look inside an actual issued token.

When Keycloak issues a token, it doesn’t just dump every Role a user has into one flat list. It carefully separates the two scopes into two distinct locations, so that any application reading the token can immediately tell which Roles apply broadly and which apply only to a specific Client:

Realm Scope

realm_access.roles

A single, shared list containing every Realm Role the user currently has — the same list, unchanged, regardless of which Client requested the token.

Client Scope

resource_access.<client-id>.roles

A separate list, nested under that specific Client’s own identifier, containing only the Client Roles belonging to that one application.

!
Common Trap

An application that only ever checks realm_access.roles will never see any of its own Client Roles — they live in a completely separate part of the token. This is a frequent source of “why isn’t my permission check working?” confusion for teams new to Keycloak.

4Internal Working

Let’s trace how both types of Roles are resolved together the moment a user logs in.
1

User Authenticates

The user logs in successfully, independent of which Roles they hold.

2

Effective Roles Computed, Both Scopes

Keycloak resolves the user’s full set of Realm Roles (direct, via Groups, via composites) and, separately, their Client Roles for every Client relevant to this login.

3

Token Assembled in Two Sections

Realm Roles are placed into the shared realm_access.roles claim; Client Roles are placed into their own nested resource_access section, keyed by each relevant Client’s ID.

4

Application Reads the Right Section

A well-built application checks both sections as appropriate — realm_access.roles for broad facts, and its own entry under resource_access for permissions specific to itself.

5Composite Roles Across Both Scopes

Composition is where the two scopes actually meet — and where the real design decisions get interesting.

A composite Role — one that automatically bundles other Roles when assigned — can mix scopes freely. A composite Realm Role called “department-head” might bundle in a broad Realm Role (“manager”) alongside several narrow Client Roles pulled from three different applications a department head needs access to. This is genuinely powerful: a single assignment step grants a thoughtfully chosen mix of organization-wide and application-specific access at once.

Everyday Analogy

A “New Department Head Starter Pack” is a Realm Role that, when handed over, automatically includes the universal “Manager” sticker (a Realm Role) plus a spa-access keycard and a parking-garage keycard (two Client Roles from two different departments) — one handover, three separate access grants, spanning both scopes at once.

!
Blast Radius Warning

Because a composite Realm Role can reach into Client Roles belonging to applications a Realm-level administrator may not personally manage, changing that composite can silently ripple permissions into unrelated applications. Review composite Realm Roles with special care before editing them.

6Advantages, Disadvantages & Trade-offs

Each scope brings a distinct set of trade-offs, and understanding both sides is what makes the choice deliberate rather than accidental.

Realm Role Trade-offs

  • Gain: defined once, automatically understood by every current and future Client — no per-application setup
  • Gain: ideal for facts that genuinely span the whole organization
  • Cost: visible everywhere, so a poorly scoped one can leak meaning into applications where it was never intended to apply

Client Role Trade-offs

  • Gain: tightly scoped — changes and reviews stay contained to one application
  • Gain: keeps an application’s own permission model self-contained and easy to audit in isolation
  • Cost: must be set up separately for each application that needs its own permissions, with no automatic sharing across Clients

7Security

Getting the scope wrong isn’t just an organizational tidiness issue — it has real security consequences.
ADR-008: Wrong-Scoped Permission Anti-pattern Warning
Problem

A team needing a narrow permission for just one application — say, “can-approve-invoices” for a billing app — defines it as a Realm Role out of convenience, rather than scoping it as a Client Role on that one Client.

Why It Fails

Every other application in the Realm now also sees this Role name inside every user’s token, even though it’s meaningless to them. This raises the risk that a different, unrelated application’s code mistakenly checks for or misinterprets that Role name, granting access it should never have had.

Correct Approach

Apply the one-sentence test from Chapter 1 consistently: scope a permission as a Client Role unless it would clearly still make sense with any single application removed from the picture.

Auditing Is Easier When Scope Matches Meaning

A security review asking “what can this user do inside the billing application specifically?” is far faster to answer when billing-specific permissions live cleanly as Client Roles on that one Client, rather than being scattered across a mix of Realm Roles and Client Roles with overlapping, ambiguous meaning.

8Monitoring, Logging & Metrics

Because the two scopes surface differently in tokens, monitoring and log analysis need to account for that split.
Audit

Scope-Aware Access Reviews

A useful access review separately lists “which users have this Realm Role” and “which users have this Client Role on this application,” rather than blending both into one undifferentiated report.

Debugging

Token Claim Inspection

When an application’s permission check unexpectedly fails, inspecting whether the expected Role actually appears in realm_access versus the correct resource_access.<client-id> section is often the fastest way to spot a scoping mistake.

Hygiene

Cross-Scope Duplication Checks

Periodically checking for Realm Roles and Client Roles with confusingly similar names helps catch cases where a permission may have been defined redundantly in both scopes.

9Best Practices & Common Mistakes

A short list of consistent habits keeps the two scopes clean and predictable, even as an organization’s Keycloak Realm grows.

Best Practices

  • Apply the one-sentence scoping test from Chapter 1 consistently, every time a new permission is created
  • Reserve Realm Roles for facts genuinely meaningful across the whole organization
  • Namespace Client Role names clearly with their Client (like “billing-api:refund-approve”) so their scope is obvious at a glance
  • Review composite Realm Roles that reach into Client Roles with extra scrutiny, given their wider blast radius

Common Mistakes

  • Defaulting to Realm Roles for every new permission simply because they’re the first option teams learn about
  • Checking only realm_access.roles in application code and never looking at the Client-scoped section, missing Client Role checks entirely
  • Letting the same underlying permission get redundantly defined once as a Realm Role and again as a Client Role
  • Nesting composite Roles many layers deep across both scopes until nobody can trace the full effective access anymore

10Real-World & Industry Examples

Well-run organizations tend to reach for the same clean split between the two scopes.

Enterprise Employee Directory

A company might use Realm Roles like “employee” and “manager” (organization-wide facts) alongside Client Roles like “expense-app:can-approve” and “hr-portal:can-view-salaries” (permissions meaningful only inside one specific tool) — a textbook clean split between the two scopes.

SaaS Products with Modular Add-Ons

A software-as-a-service platform frequently uses a Realm Role like “premium-subscriber” to reflect account-wide subscription status, while individual product modules (analytics, reporting, integrations) each define their own Client Roles for the specific actions available within that one module.

Multi-Application Government Portals

A government agency running many separate citizen-facing applications behind one identity provider often uses Realm Roles for broad citizen status (like “verified-identity”) while each individual application defines its own Client Roles for the specific services it uniquely offers.

11Frequently Asked Questions

Q1What is the single clearest way to decide between the two?
Ask whether the permission would still make sense with one specific application removed from the picture entirely. If yes, it’s a Realm Role. If it becomes meaningless without that one application in mind, it’s a Client Role.
Q2Do both types of Roles show up in the same place inside a token?
No — Realm Roles appear in the shared realm_access.roles claim, while Client Roles appear separately, nested under resource_access.<client-id>.roles for each relevant Client.
Q3Can a composite Role mix both Realm Roles and Client Roles?
Yes — a composite Realm Role can bundle in Client Roles from one or more applications, letting a single broad assignment grant a deliberate mix of organization-wide and application-specific access at once.
Q4Why would my application’s Client Role check not be working?
A very common cause is checking the wrong location in the token — Client Roles live under resource_access.<client-id>.roles, not inside realm_access.roles, which only contains Realm Roles.
Q5Is it ever appropriate to define the same permission as both a Realm Role and a Client Role?
Generally no — this creates redundant, confusing overlap. Pick the correct scope once, using the one-sentence test, rather than defining the same underlying permission twice.
Q6Which scope has a bigger security blast radius if misconfigured?
Realm Roles, since they’re automatically visible to every current and future Client in the Realm. A Client Role’s impact stays contained to the one application it belongs to.

12Summary and Key Takeaways

“A Realm Role is the sticker every door already knows how to read. A Client Role is the keycard only one door was ever built to accept.”

Key Takeaways

  • A Realm Role is defined at the Realm level and automatically recognized by every Client — use it for broad, organization-wide facts.
  • A Client Role is defined inside one specific Client and only meaningful there — use it for fine-grained, application-specific permissions.
  • The one-sentence test — “would this still make sense without one specific application?” — resolves the vast majority of scoping decisions.
  • The two scopes live in different token locations: realm_access.roles for Realm Roles, resource_access.<client-id>.roles for Client Roles — a frequent source of “why isn’t my check working?” bugs.
  • Composite Roles can mix both scopes, but composites reaching into Client Roles from other applications carry a wider blast radius when changed.
  • Getting the scope wrong has real security consequences — an over-broadly scoped permission can leak meaning into applications that were never meant to see it.
  • Consistent naming and disciplined scoping keep a Keycloak Realm’s permission model auditable and maintainable, even at scale.