Keycloak
74 строки · 5.1 Кб
1<#import "/templates/guide.adoc" as tmpl>
2<#import "/templates/links.adoc" as links>
3
4<@tmpl.guide
5title="Concepts for configuring thread pools"
6summary="Understand these concepts to avoid resource exhaustion and congestion"
7tileVisible="false" >
8
9
10This section is intended when you want to understand the considerations and best practices on how to configure thread pools connection pools for {project_name}.
11For a configuration where this is applied, visit <@links.ha id="deploy-keycloak-kubernetes" />.
12
13== Concepts
14
15=== Quarkus executor pool
16
17{project_name} requests, as well as blocking probes, are handled by an executor pool. Depending on the available CPU cores, it has a maximum size of 200 or more threads.
18Threads are created as needed, and will end when no longer needed, so the system will scale up and down automatically.
19{project_name} allows configuring the maximum thread pool size by the link:{links_server_all-config_url}?q=http-pool-max-threads[`http-pool-max-threads`] configuration option. See <@links.ha id="deploy-keycloak-kubernetes" /> for an example.
20
21When running on Kubernetes, adjust the number of worker threads to avoid creating more load than what the CPU limit allows for the Pod to avoid throttling, which would lead to congestion.
22When running on physical machines, adjust the number of worker threads to avoid creating more load than the node can handle to avoid congestion.
23Congestion would result in longer response times and an increased memory usage, and eventually an unstable system.
24
25Ideally, you should start with a low limit of threads and adjust it accordingly to the target throughput and response time.
26When the load and the number of threads increases, the database connections can also become a bottleneck.
27Once a request cannot acquire a database connection within 5 seconds, it will fail with a message in the log like `Unable to acquire JDBC Connection`.
28The caller will receive a response with a 5xx HTTP status code indicating a server side error.
29
30If you increase the number of database connections and the number of threads too much, the system will be congested under a high load with requests queueing up, which leads to a bad performance.
31The number of database connections is configured via the link:{links_server_all-config_url}?q=db-pool[`Database` settings `db-pool-initial-size`, `db-pool-min-size` and `db-pool-max-size`] respectively.
32Low numbers ensure fast response times for all clients, even if there is an occasionally failing request when there is a load spike.
33
34=== JGroups connection pool
35
36The combined number of executor threads in all {project_name} nodes in the cluster should not exceed the number of threads available in JGroups thread pool to avoid the error `org.jgroups.util.ThreadPool: thread pool is full`.
37To see the error the first time it happens, the system property `jgroups.thread_dumps_threshold` needs to be set to `1`, as otherwise the message appears only after 10000 threads have been rejected.
38
39--
40include::partials/threads/executor-jgroups-thread-calculation.adoc[]
41--
42
43Use the metrics `vendor_jgroups_tcp_get_thread_pool_size` to monitor the total JGroup threads in the pool and `vendor_jgroups_tcp_get_thread_pool_size_active` for the threads active in the pool.
44This is useful to monitor that limiting the Quarkus thread pool size keeps the number of active JGroup threads below the maximum JGroup thread pool size.
45
46[#load-shedding]
47=== Load Shedding
48
49By default, {project_name} will queue all incoming requests infinitely, even if the request processing stalls.
50This will use additional memory in the Pod, can exhaust resources in the load balancers, and the requests will eventually time out on the client side without the client knowing if the request has been processed.
51To limit the number of queued requests in {project_name}, set an additional Quarkus configuration option.
52
53Configure `http-max-queued-requests` to specify a maximum queue length to allow for effective load shedding once this queue size is exceeded.
54Assuming a {project_name} Pod processes around 200 requests per second, a queue of 1000 would lead to maximum waiting times of around 5 seconds.
55
56When this setting is active, requests that exceed the number of queued requests will return with an HTTP 503 error.
57{project_name} logs the error message in its log.
58
59[#probes]
60=== Probes
61
62{project_name}'s liveness probe is non-blocking to avoid a restart of a Pod under a high load.
63
64// Developer's note: See KeycloakReadyHealthCheck for the details of the blocking/non-blocking behavior
65The overall health probe and the readiness can probe in some cases block to check the connection to the database, so they might fail under a high load.
66Due to this, a Pod can become non-ready under a high load.
67
68=== OS Resources
69
70In order for Java to create threads, when running on Linux it needs to have file handles available.
71Therefore, the number of open files (as retrieved as `ulimit -n` on Linux) need to provide head-space for {project_name} to increase the number of threads needed.
72Each thread will also consume memory, and the container memory limits need to be set to a value that allows for this or the Pod will be killed by Kubernetes.
73
74</@tmpl.guide>
75