Keycloak Ops
Isometric pink stepped area chart beside a layered pink and purple cube, with six glowing pink knobs linked on a dotted grid against dark blue.
sizing

Keycloak RAM and Memory Requirements: Usage and Heap Sizing

Size Keycloak RAM and JVM heap from the 1,250 MB pod baseline. Calculate container limits, tune session caches and estimate vCPU from login rates.

By Keycloak Ops Editorial · ·Updated · 5 min read

Start with 1,250 MB of RAM per Keycloak pod for realm caches and 10,000 cached sessions. About 300 MB of that baseline is non-heap memory. With the default maximum heap of 70% of the container limit, this works out to a limit of about 1,360 MB. Keycloak sizing guide

Evidence and limits. The figures and configuration examples are based on the Keycloak 26 documentation and the Kubernetes resource reference. The examples were prepared without running the commands or measuring a deployment. See Keycloak resource requirements for CPU, disk and database hardware for small installs.

Where Keycloak Memory Goes

  • JVM heap. The Keycloak container defaults to an initial heap of 50% of the container memory limit and a maximum heap of 70%. The settings are -XX:InitialRAMPercentage=50 and -XX:MaxRAMPercentage=70. Keycloak container guide
  • Non-heap memory. About 300 MB of the baseline per pod is non-heap memory. Keycloak sizing guide
  • Database. Size the database separately using the login, logout and refresh rates in the load example below. Keycloak sizing guide

The Documented Baseline

Set the pod’s starting memory request to 1,250 MB. To size the limit at the default heap percentage, subtract the estimated non-heap memory from the request and divide by 0.7. In the sizing guide’s example, (1,250 MB − 300 MB) / 0.7 ≈ 1,360 MB. Keycloak sizing guide

Always set a container memory limit because the JVM sizes its heap as a share of the available memory. The container guide recommends a 2 GB limit for smaller production deployments. It specifies at least 750 MB to approximate the former 512 MB maximum heap. Keycloak container guide

How Persistent Sessions Use Heap

Keycloak 26 stores user sessions in the database by default. Keycloak 26.0.0 release notes

With persistent sessions and embedded session caching enabled, Keycloak loads sessions from the database into memory as needed. Each cache entry has a single owner. The default capacity is 10,000 entries per node for each of the four session caches. Keycloak caching guide

Set Heap and Cache Limits

Heap override. Set JAVA_OPTS_KC_HEAP to override the JVM’s heap options. The container guide passes the following option to the container runtime. It sets the maximum heap to 65% of the container memory. Keycloak container guide

-e JAVA_OPTS_KC_HEAP="-XX:MaxHeapFreeRatio=30 -XX:MaxRAMPercentage=65"

Session cache caps. Use the following options to set the per-node entry cap for each embedded session cache when persistent sessions are enabled. Keycloak caching guide

  • --cache-embedded-sessions-max-count
  • --cache-embedded-client-sessions-max-count
  • --cache-embedded-offline-sessions-max-count
  • --cache-embedded-offline-client-sessions-max-count

For example, append --cache-embedded-offline-sessions-max-count=1000 to the Keycloak startup command to cap the offline user session cache at 1,000 entries per node. Keycloak caching guide

Kubernetes requests and limits. This container-level resources block uses the Keycloak sizing guide’s example values for one pod. Keycloak sizing guide · Kubernetes resource reference

resources:
  requests:
    cpu: "3"
    memory: "1250M"
  limits:
    cpu: "7500m"
    memory: "1360M"

Allow headroom for the caches to refill after a restart. Size for startup as well as steady-state use. Keycloak sizing guide

The Caches You Should Size Deliberately

Keycloak groups its caches by how they are shared. Keycloak caching guide

  • Local caches include realms, users, authorization, keys and crl.
  • Replicated cache work shares invalidation messages across nodes.
  • Distributed caches include authenticationSessions, sessions, clientSessions, offlineSessions, offlineClientSessions, loginFailures and actionTokens.

The local realms, users and authorization caches hold up to 10,000 entries each by default. The keys cache holds up to 1,000 entries by default and has a default expiration interval of one hour. Keycloak caching guide

When more than 2,500 distinct clients send requests concurrently and the standard 10,000-entry local caches are in use, Keycloak recommends the following increases to reduce database lookups. Keycloak sizing guide

  • Increase the users cache size by twice the number of concurrent clients.
  • Increase the realms cache size by four times the number of concurrent clients.

Turning Load Into Nodes

These ratios estimate CPU across the Keycloak cluster. The last column gives the highest test rate reported for each request type in the sizing guide. Keycloak sizing guide

Request type Requests per second per vCPU Highest rate tested (requests per second)
Password logins 15 300
Client credential grants 120 2,000
Refresh token requests 120 435

Keycloak recommends adding 150% CPU headroom for spikes, startup and failover. For an Aurora PostgreSQL multi-AZ database, the guide budgets 1,400 write IOPS and 0.35 to 0.7 vCPU for every 100 login, logout and refresh requests per second. Keycloak sizing guide

A Worked Example

Suppose the cluster handles 30 password logins and 200 refresh token requests per second at peak. Apply the published rates as follows. Keycloak sizing guide

  • Keycloak CPU request. 30 / 15 + 200 / 120 ≈ 3.67 vCPU across the cluster.
  • Keycloak CPU with headroom. Multiply by 2.5 to include the extra 150%, for about 9.2 vCPU across the cluster.
  • Memory per pod. Start with a 1,250 MB request. The example at the default heap percentage gives (1,250 MB − 300 MB) / 0.7 ≈ 1,360 MB for the limit.
  • Aurora PostgreSQL load. The 230 login and refresh requests per second give 2.3 × 1,400 = 3,220 write IOPS. Multiplying each endpoint of the CPU range by 2.3 gives about 0.8 to 1.6 vCPU for that request load.

The Keycloak sizing guide’s own three-pod example uses rates of 45 logins, 360 client credential grants and 360 refresh token requests per second for its Keycloak CPU calculation. Each pod gets a 3 vCPU request and a 7.5 vCPU limit. Keycloak sizing guide

To vary the memory inputs, size it for your session count with the calculator, including the container-limit conversion.

What Makes Memory Grow Unexpectedly

Client count. More concurrently active clients can fill the local users and realms caches. Keycloak sizing guide

Session cache caps. Raising the max-count options lets each node cache more session entries. Keycloak caching guide

Restarts. Caches refill when an instance restarts. Keycloak sizing guide

Client scopes and token design are covered in Keycloak realms, clients and token design fundamentals.

What to Monitor

Track heap usage after garbage collection and watch garbage collection pause times. Use cache eviction and miss rates to see whether the users, realms or session caches are under pressure. Keycloak’s documented garbage collection settings can leave memory allocated to the JVM after use. Keycloak container guide

Before You Size, Get the Deployment Shape Right

Resolve Keycloak hostname and proxy errors before using request rates to size a deployment. If the resource estimate affects your choice of identity server, see Keycloak vs authentik vs Authelia.

Sources

  1. Keycloak: Concepts for sizing CPU and memory resources
  2. Keycloak: Running Keycloak in a container
  3. Keycloak: Configuring distributed caches
  4. Keycloak 26.0.0 released
  5. Kubernetes: Resource Management for Pods and Containers

Related