Keycloak Memory Usage
You control keycloak memory usage by sizing the Java heap and tuning session caches. You must also limit the user data that goes into each login token. Teams often run the identity server with default memory limits in production. When thousands of users log in at once, the server runs out of memory. Broad roles and huge group lists make every session object much larger. This causes crashes and blocks engineers from the systems they need to do their work. How keycloak memory usage works under t
Keycloak: the complete guide
Architecture patterns, implementation strategies, and security best practices. Delivered to your inbox.
You control keycloak memory usage by sizing the Java heap and tuning session caches. You must also limit the user data that goes into each login token. Teams often run the identity server with default memory limits in production. When thousands of users log in at once, the server runs out of memory. Broad roles and huge group lists make every session object much larger. This causes crashes and blocks engineers from the systems they need to do their work.
How keycloak memory usage works under the hood
Keycloak runs on the Java Virtual Machine and stores active state in memory. Every time a user logs in, the server creates a new session object. It keeps these sessions in an internal cache so logins stay fast. When you run a cluster, nodes share these cache entries over the network.
Total memory goes beyond the Java heap alone. The process also needs non-heap space for class data and thread stacks. Container limits must leave room for both heap and off-heap memory needs. If the Java heap takes all container memory, the host system kills the process.
Database pools and network buffers also add to the total footprint. Offline tokens stay in the cache if you do not bound their count. Each realm, client, and role mapping sits in memory for quick lookup. Large directory sync jobs pull thousands of user records into memory at once.
How to tune caches and token claims
Start by setting clear upper bounds on your session and user caches. When a cache has no limit, it grows until the server crashes. Eviction rules drop the oldest idle entries and keep memory use flat. Short session timeouts also clear dead sessions out of the heap much faster.
Next, check the size of the tokens your realms create for users. Many teams map every group and role into every single login token. When an engineer belongs to fifty groups, each token becomes very large. The server must build, sign, and cache these big payloads on every request. Trim token mappers so they only send the claims that an app needs.
You should also test garbage collection under real login load before going live. Fast login spikes create a lot of temporary objects in young heap space. If the collector cannot keep up, pause times grow and health checks fail. Load testing shows you the exact heap size needed for peak morning traffic.
Keycloak: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
What identity server tuning solves and what it misses
Sizing the identity server keeps single sign-on fast and stable for your team. It ensures that users can log in and get valid identity tokens. It also stops login spikes from taking down your central auth service.
Still, an identity server only checks who a person is at login time. Once an engineer gets a session, the identity server steps out of the way. It cannot see what queries an engineer runs inside a production database. It cannot stop a bad command from dropping a table inside a shell.
Teams often try to fix this by creating hundreds of tiny roles. That approach bloats the identity cache and hurts memory use even more. Even with fine roles, a logged-in user still holds broad database or server rights. Real safety requires control over each command and query while the session runs. You also need data masking that rewrites emails, phone numbers, and card numbers in results.
Govern statements and responses across databases and APIs
Good config and scoped roles are needed setup. They do not check single queries in flight or mask sensitive fields in returned rows. Across Postgres, MySQL, SQL Server, MongoDB, ClickHouse, Kubernetes APIs, SSH, and internal HTTP endpoints, hoop.dev runs beside each supported service as an open-source proxy, a sidecar for your database or API.
First, hoop.dev guardrails block banned queries before they run. Then hoop.dev data masking rewrites matched sensitive values in responses in memory. The audit trail records every statement and verdict per resource. Furthermore, hoop.dev holds SOC 2 Type II and is open source on GitHub.
Frequently asked questions
Why does my identity container crash even when heap use looks low?
Java uses extra memory outside the heap for threads, classes, and network buffers. Leave a safe gap between the max heap limit and the container memory limit.
Do offline sessions increase keycloak memory usage at startup?
Yes, preloading many offline sessions into cache at boot can spike memory use. Limit how many offline sessions stay in memory and load them on demand.
How do large role mappings affect server memory and speed?
Mapping hundreds of roles into every token makes session objects and caches much larger. Scope your mappers so tokens only carry the roles needed for that specific app.
Related guides
In this post