What Is Keycloak?
A complete, zero-jargon walkthrough of the open-source tool that lets companies stop building their own login systems from scratch — how it works, what it protects you from, and when it's the right choice.
Nearly every serious application needs to answer two questions before it does anything else: “who are you?” and “what are you allowed to do here?” Building a secure, correct answer to those two questions from scratch — handling passwords safely, supporting “Login with Google,” issuing tokens, managing password resets, enforcing multi-factor authentication — is a surprisingly large and easy-to-get-wrong engineering project. Keycloak is a free, open-source tool created specifically to solve this problem once, correctly, so that individual teams never have to build it themselves. This guide explains what Keycloak actually is, what happens inside it when a user logs in, why large organizations rely on it instead of writing their own authentication code, and where it fits next to alternatives like Auth0 and AWS Cognito.
ACore Concepts
Keycloak sits inside a category of software called Identity and Access Management, or IAM. Understanding that category first makes Keycloak itself far easier to grasp.
Keycloak is an open-source Identity and Access Management server, originally created by Red Hat, whose entire job is to sit between your users and your applications and handle everything related to logging in, staying logged in, and proving who someone is. Instead of every single application on a company’s network having its own separate login page, its own separate password database, and its own separate rules about session timeouts, all of those applications instead trust one central Keycloak server to handle authentication for all of them.
Imagine a large office building with dozens of separate companies renting floors. Instead of every company installing its own front-door lock and issuing its own separate keycards, the building has one central security desk at the entrance. You show your ID once at that desk, get one badge, and that single badge then works on every floor you’re allowed into. Keycloak is that central security desk for software applications — log in once, and every connected application trusts the badge it gives you.
This “log in once, trusted everywhere” behavior has a specific name: Single Sign-On, or SSO. It’s the headline feature most people associate with Keycloak, but Keycloak actually handles a much broader set of responsibilities under one roof.
Authentication
Verifying a user’s identity through passwords, one-time codes, biometrics, or social logins like Google and GitHub.
Authorization
Deciding what an already-authenticated user is allowed to do, using roles and fine-grained permission policies.
Federation
Connecting to an existing user directory — like a corporate LDAP server or Active Directory — instead of forcing a fresh migration of every user account.
Standard Protocols
Speaking industry-standard languages — OpenID Connect, OAuth 2.0, and SAML — so it can plug into almost any modern application without custom integration code.
If you’ve ever clicked “Continue with Google” or been redirected briefly to a separate login page before landing back inside an app, you’ve experienced the pattern Keycloak is built to provide — even if that specific site wasn’t using Keycloak itself.
BInternal Working
To understand how Keycloak works internally, you need three building blocks it’s organized around: realms, clients, and tokens.
A realm is Keycloak’s top-level container — think of it as a completely isolated “world” with its own users, its own login rules, and its own settings. A single Keycloak server can host many realms at once; a company might run one realm for its internal employees and a completely separate realm for external customers, with zero overlap between the two.
A client is any application registered inside a realm that wants Keycloak to handle its logins — a website, a mobile app, or a backend API. Each client gets its own identifier and its own rules about what it’s allowed to request from Keycloak.
A token is the actual proof of identity Keycloak hands out after a successful login. Rather than an application repeatedly asking Keycloak “is this user still logged in?” for every single action, the user’s browser or app holds onto a signed token and presents it directly to applications, which can verify it themselves almost instantly, without even needing to contact Keycloak again for most checks.
sequenceDiagram
participant U as User Browser
participant A as Your Application
participant K as Keycloak Server
U->>A: Try to access protected page
A->>U: Redirect to Keycloak login
U->>K: Enter username & password
K->>K: Verify credentials, check MFA
K->>U: Redirect back with Authorization Code
U->>A: Deliver Authorization Code
A->>K: Exchange code for Access Token + ID Token
K->>A: Return signed tokens
A->>U: Grant access, set session
Fig 1 — The standard OpenID Connect “Authorization Code” login flow that Keycloak implements.
This exchange follows an industry-standard protocol called OpenID Connect (built on top of an older standard called OAuth 2.0). Keycloak didn’t invent this flow — it’s a widely agreed-upon standard used by Google, Microsoft, and virtually every major identity provider — Keycloak’s value is that it implements this entire standard correctly, securely, and for free, so your team doesn’t have to.
Keycloak does not store or see your application’s business data. It only manages identity — who a user is and what roles they hold. Your actual application still owns and protects its own database and business logic.
CData Flow & Lifecycle
A user’s relationship with Keycloak isn’t a single moment — it’s a session with a beginning, an active middle, and an eventual end.
Redirect to Login
A user tries to open a protected application; the application redirects their browser to Keycloak’s hosted login page rather than showing its own login form.
Credential Verification
Keycloak checks the submitted username and password (and any additional multi-factor step) against its own database or a connected external directory.
Token Issuance
On success, Keycloak issues a signed Access Token (proving identity and permissions to APIs) and an ID Token (describing who the user is), both digitally signed so any application can verify they’re genuine without calling Keycloak back.
Active Session
The user moves between any application trusting this same Keycloak realm without logging in again, since each application accepts the same token, until it expires.
Token Refresh or Logout
A short-lived Access Token is periodically renewed using a longer-lived Refresh Token, until the user explicitly logs out or their full session expires, at which point Keycloak revokes access across every connected application simultaneously.
Walking Through a Real Example: Logging Into an Internal Company Portal
An employee opens their company’s HR portal. The portal redirects them to Keycloak. Because they already logged into their email earlier that morning through the same Keycloak realm, Keycloak recognizes an existing valid session and skips the login form entirely, silently issuing a fresh token and redirecting them straight into the HR portal — the employee never even sees a login screen for the second application.
DAdvantages, Disadvantages & Trade-offs
Choosing Keycloak means choosing to self-host and self-manage your identity layer, rather than paying a third party to fully manage it for you — a trade-off worth examining honestly.
Advantages
- Completely free and open-source, with no per-user licensing fees, unlike most commercial identity providers.
- Implements well-tested, industry-standard security protocols instead of custom, unaudited login code.
- Supports Single Sign-On across any number of applications from one central realm.
- Can federate with existing corporate directories (LDAP, Active Directory) instead of forcing a full user migration.
- Highly configurable: custom themes, custom authentication flows, and extensions for unusual requirements.
Disadvantages
- You are responsible for hosting, patching, scaling, and backing up the Keycloak server yourself — it is not a managed cloud service by default.
- The initial learning curve around realms, clients, roles, and protocol concepts can be steep for a first-time team.
- High-availability production setups require careful database and clustering configuration to avoid Keycloak becoming a single point of failure.
- Upgrades between major Keycloak versions have historically required careful planning due to underlying architecture changes.
This is fundamentally the same “build vs. buy” trade-off seen throughout infrastructure decisions. A fully managed commercial service like Auth0 or AWS Cognito removes the operational burden of running servers, at the cost of ongoing fees that scale with your user count. Keycloak removes the licensing fees entirely, in exchange for your team taking on the operational responsibility of running it well.
Many teams run Keycloak inside Kubernetes with a managed database behind it, treating it like any other critical piece of infrastructure — with monitoring, backups, and a clear upgrade process — rather than a “set it and forget it” tool.
EDesign Patterns & Anti-Patterns
Teams that get real value from Keycloak tend to structure their realms and clients deliberately, rather than treating every setting as a default to leave untouched.
Separate Realms by Trust Boundary
Keep internal employees and external customers in entirely separate realms, so a security issue or policy change in one never accidentally affects the other.
Role-Based Access Mapped to Tokens
Encode a user’s roles directly into their issued token, so downstream applications can make authorization decisions instantly without an extra lookup call.
Short-Lived Access Tokens
Keep Access Tokens valid for only a few minutes, relying on Refresh Tokens for longer sessions, so a stolen token becomes useless quickly.
Directory Federation Over Migration
Connect Keycloak to an existing LDAP or Active Directory rather than copying every user record into Keycloak’s own database, keeping one authoritative source of truth.
The Problem
Running a single Keycloak instance with no clustering, no database replication, and no monitoring in front of a production application that many users depend on daily.
Why It Hurts
Because Keycloak sits directly in the login path of every connected application, a Keycloak outage doesn’t just affect one app — it can lock users out of every application relying on that realm simultaneously.
Better Approach
Run Keycloak in a clustered, highly-available configuration with a properly backed-up database, and monitor it with the same seriousness applied to any other critical piece of production infrastructure.
The Problem
Granting every client application the broad “admin” client scope by default, rather than deliberately scoping down what each individual application is allowed to request.
Why It Hurts
If any single application is compromised, an overly broad scope means the attacker can potentially impersonate users or access far more than that one application should ever have needed.
Better Approach
Apply least-privilege scoping per client, granting each application only the specific roles and permissions it genuinely requires to function.
FBest Practices & Common Mistakes
Beyond the architectural decisions, a handful of operational habits determine whether a Keycloak deployment stays secure and reliable over time.
One frequent mistake among teams new to Keycloak is underestimating how much planning realm and client structure deserves before going live. Because migrating users or restructuring realms after a system is already in production use is disruptive and risky, it pays to design the realm layout — how many realms, what roles exist, how federation with an existing directory will work — carefully during initial setup, rather than treating it as something to fix later.
A second common mistake is customizing Keycloak’s core authentication flows so heavily and so undocumented that future upgrades become nearly impossible without breaking something. Keycloak supports deep customization deliberately, but teams that keep clear documentation of every custom flow, theme, and extension they’ve added avoid painful surprises when it’s time to upgrade to a newer version.
GReal-World & Industry Examples
Keycloak has become one of the most widely deployed open-source identity servers in the world, used across very different kinds of organizations.
Enterprise Internal Tools
Large companies commonly deploy Keycloak to unify login across dozens of internal tools — HR systems, ticketing platforms, internal dashboards — so employees experience one login instead of a different password for every tool.
Government and Public Sector Platforms
Because Keycloak is fully open-source and self-hostable, government agencies with strict data-residency and auditability requirements frequently prefer it over commercial, cloud-hosted identity providers where user data would leave their own infrastructure.
SaaS Products Needing Multi-Tenant Login
Software companies serving many different customer organizations often use Keycloak’s realm structure to isolate each customer’s users cleanly, while still sharing one underlying identity platform.
Kubernetes-Native Platforms
Keycloak is frequently paired with API gateways and service meshes in Kubernetes environments, acting as the OpenID Connect provider that secures access to internal microservices.
What unites these very different examples is the same underlying motivation: none of these organizations wanted to build and continuously maintain their own authentication system from scratch, and each valued having full control over where their identity data actually lives.
HFrequently Asked Questions
ISummary and Key Takeaways
Key Takeaways
- Keycloak is an open-source Identity and Access Management server that handles authentication and authorization so individual applications don’t have to build their own login systems.
- Realms, clients, and tokens are the core building blocks: realms isolate user populations, clients represent connected applications, and signed tokens carry proof of identity.
- It implements standard protocols — OpenID Connect, OAuth 2.0, and SAML — rather than a proprietary approach, allowing broad compatibility across languages and frameworks.
- Single Sign-On is the headline benefit: log in once, and every connected application trusts the same signed token without a separate login.
- Free, but not effortless: Keycloak eliminates licensing costs but requires your own team to host, patch, scale, and secure the server responsibly.
- Federation avoids painful migrations: Keycloak can connect to an existing LDAP or Active Directory instead of requiring every user account to be recreated.
- It’s a serious alternative to commercial identity providers like Auth0 or AWS Cognito, particularly for organizations that value full control over where identity data is hosted.