Quick revision
The short answer
Slow LDAP logins usually come from three costs adding up: a new TLS connection for every search, expensive nested-group lookups, and a directory server that is far away or busy. We pool the service-account connections, replace recursive group searches with a narrower search base or the tokenGroups attribute, use a nearby replica or the global catalog, and cache authorities briefly after login. 'Size limit exceeded' comes from the server's search limits, so we page the results or narrow the filter, and in the long run consider an OIDC identity provider federated with the directory.
What to remember
In this answer, we split a slow LDAP login into its steps and measured them, then pooled the service-account LDAPS connections, made nested group lookups cheaper with a narrower search base or tokenGroups, fixed 'size limit exceeded' with paging and narrower filters instead of higher server limits, moved to a nearby domain controller or the global catalog, and cached authorities briefly. In the long run an OIDC identity provider federated with the directory removes LDAP from the login path.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Slow LDAP logins usually come from three costs adding up: a new TLS connection for every search, expensive nested-group lookups, and a directory server that is far away or busy. We pool the service-account connections, replace recursive group searches with a narrower search base or the tokenGroups attribute, use a nearby replica or the global catalog, and cache authorities briefly after login. 'Size limit exceeded' comes from the server's search limits, so we page the results or narrow the filter, and in the long run consider an OIDC identity provider federated with the directory.
Why this question is asked
An LDAP login that takes 8 to 15 seconds at peak is a classic production problem in companies with large Active Directory forests. It rarely has one cause; it is several slow steps that only become painful under load. Interviewers ask this debugging question to see whether we measure before changing anything and whether we understand what a Spring Security LDAP login actually does on the wire. In this answer, we will break a login into its steps, measure them, then fix connections, group lookups, size limits and directory distance, and finish with caching and the longer-term option.
1What One LDAP Login Actually Does
A typical Spring Security LDAP login does more than check a password. It opens a connection with the service account and searches for the user's entry, opens another connection and binds as the user to check the password, and then searches for the user's groups, often several times if groups are nested inside other groups. Each connection to LDAPS also needs a TLS handshake. Every step waits on the network and on the directory server.
At quiet times each step takes tens of milliseconds and nobody notices. At peak, the directory is busy, handshakes queue up and recursive group searches scan large parts of the tree, and the same login takes seconds.
- Open connection, TLS handshakeservice account, often a new one each time
- Search for the user's entryby
sAMAccountName - Open a second connection, bind as the userpassword check
- Search for groups, recursivelythe usual hot spot
- Size limit hit?YesError: size limit exceededNoBuild authorities and finish
5 steps in order. Play them, or step through them one at a time.
2Measuring Before Changing Anything
We first find out which step is slow. Spring LDAP and Spring Security log the searches at DEBUG level, but timings are easier with a small wrapper or a Micrometer timer around the authenticator and the authorities populator. A test login for a user with few groups and one for a user with hundreds of nested groups, at quiet and peak times, usually tells the whole story.
In this example most of the time goes into the group search, and the two TLS handshakes add almost a second. That ordering is typical, and it tells us where to start.
3Fix 1: Reuse Connections Instead of Opening New Ones
If every search opens a new LDAPS connection, every login pays for TCP and TLS setup, which at peak can take hundreds of milliseconds per handshake. We enable pooling for the service-account connections. With Spring LDAP's LdapContextSource we can turn on the JNDI connection pool, but note one detail: the JNDI pool does not pool TLS connections unless we tell it to, through the com.sun.jndi.ldap.connect.pool.protocol system property set to include ssl. Alternatively, Spring LDAP's PoolingContextSource gives a separately configurable pool.
Only the service-account connections are pooled. The bind as the user must use a fresh connection, because after the bind that connection is authenticated as the user, and Spring LDAP opens authenticated contexts without pooling for that reason.
4Fix 2: Make Nested Group Lookups Cheaper
Active Directory groups can contain groups. To find every group a user belongs to, applications often use the LDAP_MATCHING_RULE_IN_CHAIN filter, whose matching rule id is 1.2.840.113556.1.4.1941, or run their own recursive searches. Both make the directory walk the group graph, which is expensive for users in hundreds of groups and for searches whose base is the whole domain.
We can also narrow the problem. If the application only cares about 20 groups under OU=AppGroups, we search only that base, or we check membership of those 20 groups rather than computing every group the user is in. Most users belong to hundreds of groups the application never looks at; fetching them all is wasted work.
5Fix 3: Handle 'Size Limit Exceeded' Properly
'Size limit exceeded' is LDAP result code 4. It means a search matched more entries than the server allows in one response; Active Directory's default MaxPageSize is 1,000 entries. Users with many groups, or a group search with a broad filter, hit this limit, and the login fails or loses authorities.
Raising the server limit is the wrong fix: it makes every expensive search more expensive. Instead we request results in pages with the paged results control, which Spring LDAP supports with PagedResultsDirContextProcessor, or, better, narrow the filter and search base so that the search returns only the groups the application needs. A client-side count limit set too low in our own configuration produces the same error, so we check that too.
6Fix 4: Talk to a Nearby Directory Server
A login in Frankfurt that talks to a domain controller in Dallas pays about 100 milliseconds per round trip, and an LDAP login involves many round trips. We point the application at a domain controller in its own site, through AD's site-aware DNS records or a local load-balanced name. For forests with several domains, the global catalog, on ports 3268 or 3269 for TLS, holds a partial copy of every object in the forest and can answer user and group searches without following referrals across domains. Referral chasing to unreachable servers is itself a common source of multi-second timeouts, so we also check the referral setting and connection timeouts.
7Fix 5: Cache Authorities and Consider the Longer Term
Group membership rarely changes from one minute to the next, so we can cache a user's authorities for a few minutes after login. Repeated logins, for example from several devices, then skip the group search. The cache must be short, because removing someone from a group should take effect quickly.
The longer-term fix is to stop querying the directory on every login. An OIDC identity provider federated with Active Directory authenticates the user once for all applications and puts the relevant groups into the token. The application reads them from the token without any LDAP traffic, and gains MFA and single sign-on on the way.
8Follow-up questions
Why not raise the timeout and move on?
Because the timeout hides the problem while users still wait 15 seconds, and under heavier load the logins pile up and exhaust threads. The cause is avoidable work, and removing it fixes both speed and capacity.
Is it safe to pool LDAPS connections?
Yes for the service account, as long as the pool validates connections and has sensible limits. Pooled connections authenticated as end users would be a problem, which is why user binds are never pooled.
9Conclusion
In this answer, we split a slow LDAP login into its steps and measured them, then pooled the service-account LDAPS connections, made nested group lookups cheaper with a narrower search base or tokenGroups, fixed 'size limit exceeded' with paging and narrower filters instead of higher server limits, moved to a nearby domain controller or the global catalog, and cached authorities briefly. In the long run an OIDC identity provider federated with the directory removes LDAP from the login path.
Open with
I would first measure each step of a login, because an LDAP login is several steps: connect and TLS handshake with the service account, search for the user, a second connection to bind as the user, and then group searches, often recursive.
Then, if they want more
I would time them for a user with few groups and one with many nested groups, at peak. Usually the nested group lookup dominates, with TLS handshakes second. For connections, I would pool the service-account connections; with JNDI pooling on LdapContextSource that needs the pool protocol property to include ssl, otherwise LDAPS is not pooled. User binds stay unpooled.
For groups, LDAP_MATCHING_RULE_IN_CHAIN searches from the domain root are expensive, so I would narrow the search base to the application's groups, check only the groups we care about, or read the user's tokenGroups attribute, which the domain controller computes.
Size limit exceeded is result code 4 from the server's limit, around 1,000 entries in AD by default, so I would page the results or narrow the filter rather than raise the limit. I would also make sure we use a domain controller in the same site or the global catalog, and check referral and timeout settings.
Then I would cache authorities for a few minutes, and longer term move to an OIDC provider federated with AD so groups arrive in the token.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Security reference
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Logins through LDAP take 8 to 15 seconds at peak, and some users with many nested groups...": https://scrivio-interview-library.netlify.app/spring-boot/authentication/73fb7c2ffb/