Quick revision
The short answer
Some SecureRandom configurations read /dev/random, and on older Linux kernels /dev/random blocks until the kernel has gathered enough entropy, which on a fresh VM or container can take minutes. The usual trigger is SecureRandom.getInstanceStrong(), which on Linux selects the blocking NativePRNGBlocking algorithm, or code that calls generateSeed(). The fix is to use new SecureRandom() for tokens, reuse one thread-safe instance, check securerandom.source and the algorithm in java.security, feed entropy on old kernels, and only then consider -Djava.security.egd=file:/dev/urandom, measuring contention before changing anything for throughput.
What to remember
In this answer, we saw that the hang comes from reading /dev/random on older Linux kernels with little entropy, usually through getInstanceStrong(), which selects NativePRNGBlocking, or through generateSeed(). We confirm it with a thread dump, then fix it by using new SecureRandom() created once, improving entropy on old hosts, and only where justified changing the seed source with java.security.egd. The slower load test is contention on a shared instance, which we measure first and address with DRBG or a few instances, never with a weaker generator.
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.
Some SecureRandom configurations read /dev/random, and on older Linux kernels /dev/random blocks until the kernel has gathered enough entropy, which on a fresh VM or container can take minutes. The usual trigger is SecureRandom.getInstanceStrong(), which on Linux selects the blocking NativePRNGBlocking algorithm, or code that calls generateSeed(). The fix is to use new SecureRandom() for tokens, reuse one thread-safe instance, check securerandom.source and the algorithm in java.security, feed entropy on old kernels, and only then consider -Djava.security.egd=file:/dev/urandom, measuring contention before changing anything for throughput.
Why this question is asked
A service that starts in ten seconds on old hosts and hangs for minutes on new ones looks like a networking or disk problem, so teams often lose days before they look at random numbers. Interviewers ask this because it tests whether we understand where Java's secure randomness actually comes from, and whether we can fix a performance problem without weakening security. In this answer, we will recap how the operating system provides entropy, see which Java calls can block and why, confirm the diagnosis with a thread dump, go through the fixes in order of preference, and then look at the separate throughput issue.
1A Quick Recap of Entropy on Linux
Entropy here means unpredictable data the kernel collects from hardware events such as interrupt timings, disk activity and, on modern CPUs, a hardware random number instruction. The kernel mixes this into its own cryptographic generator, and exposes it through two device files, /dev/random and /dev/urandom, plus the getrandom system call.
Historically the two files differed. /dev/urandom never blocked. /dev/random kept an estimate of available entropy and blocked readers when the estimate ran low. On a freshly booted virtual machine or a container host with little device activity, that estimate could stay low for a long time. Since Linux 5.6 the distinction is mostly gone: once the kernel generator has been initialised early in boot, both files return data without blocking. So this problem appears mainly on older kernels, which many enterprise distributions still ship, or very early in boot.
2Which Java Calls Can Block
Java's SecureRandom is configured in the java.security file of the JDK. Two properties matter here. securerandom.source names the seed source, and is file:/dev/random by default. securerandom.strongAlgorithms lists what getInstanceStrong() returns, and on Linux it starts with NativePRNGBlocking.
7 messages in order. Play them, or step through them one at a time.
On Linux 5.6 and later the read returns at once after boot; on older kernels it can wait.
The default new SecureRandom() is NativePRNG, whose nextBytes reads /dev/urandom and does not block. NativePRNGBlocking reads /dev/random for everything, so on an old kernel it can stall. generateSeed() on NativePRNG also reads /dev/random. Some libraries call these during startup: a common example is a servlet container creating its session id generator, which logs a warning that creating the SecureRandom instance took many seconds. Another is a library that asks for the strong instance because strong sounds safer.
So the new hosts likely combine an older kernel or a minimal VM image with little entropy, and a code path that uses the blocking source. The old hosts had been running long enough, or had more device activity, that the pool was full.
3Confirming the Diagnosis
Before changing anything, we prove it. A thread dump taken during the hang with jcmd <pid> Thread.print, or jstack, shows the main thread inside sun.security.provider.NativePRNG or SeedGenerator, waiting in a FileInputStream read. On the host, cat /proc/sys/kernel/random/entropy_avail shows the kernel's estimate on older kernels, and uname -r tells us the kernel version. If the stack shows a different place, we look elsewhere; this is a diagnosis, not a guess.
4Fixing the Startup Hang
The fixes go from most to least preferred. First, use new SecureRandom() for tokens, session ids and salts. It is cryptographically strong, and the kernel's non-blocking generator is the recommended source for these. getInstanceStrong() is meant for long-term keys, and even there, on modern kernels, it adds nothing but the risk of blocking.
Second, fix the host where we can. Newer kernels remove the problem. On older ones, a hardware generator exposed to the VM, such as virtio-rng, or a daemon like rng-tools, keeps the pool supplied. Third, as a targeted JVM setting, -Djava.security.egd=file:/dev/urandom, or the matching securerandom.source line in java.security, changes the seed source to the non-blocking file. That is safe on Linux once the kernel generator has initialised, which happens early in boot, but it is a global change for every SecureRandom in the process, so we document why we set it and apply it only to services that need it, rather than copying it into every Dockerfile.
Notice that the fix also creates the instance once. Creating a new SecureRandom per request costs seeding work each time, which is wasteful even when it does not block.
5The Separate Throughput Problem
The load test result is a different issue. SecureRandom is thread-safe, so sharing one instance is correct, but the NativePRNG implementation serialises access to its underlying stream, so many threads calling nextBytes at once queue behind each other. Measured symptoms are threads in a BLOCKED or waiting state inside SecureRandom in a profiler, or flat throughput as threads increase.
Before changing anything we measure: a short JMH benchmark or the load test with a profiler attached tells us whether random generation is really the bottleneck, or whether it is encoding, hashing or the database write. If it is, we can try the DRBG algorithm, SecureRandom.getInstance("DRBG"), and compare, or use a small pool of instances, for example one per thread through a ThreadLocal, so threads stop sharing one lock. What we do not do is switch to java.util.Random or ThreadLocalRandom for speed: that turns a performance problem into a security vulnerability.
6Follow-up questions
Is file:/dev/./urandom still needed?
That odd path was a workaround for very old JDKs, where the special handling of exactly file:/dev/urandom made the SHA1PRNG implementation still read /dev/random. Modern JDKs do not need it. If it appears in old startup scripts, it is a sign the setting was copied without being understood, and we can review whether it is still needed at all.
Does this happen on macOS or Windows?
Not in the same way. macOS /dev/random does not block after boot, and on Windows the default SecureRandom uses the operating system's crypto API. That is one reason it works on developer laptops and fails on new Linux servers.
How many bytes should a token have?
At least 16 bytes, 128 bits, for anything an attacker might try to guess; 32 bytes is a common choice and costs nothing noticeable. Encoded with URL-safe Base64 without padding, 32 bytes become 43 characters.
7Conclusion
In this answer, we saw that the hang comes from reading /dev/random on older Linux kernels with little entropy, usually through getInstanceStrong(), which selects NativePRNGBlocking, or through generateSeed(). We confirm it with a thread dump, then fix it by using new SecureRandom() created once, improving entropy on old hosts, and only where justified changing the seed source with java.security.egd. The slower load test is contention on a shared instance, which we measure first and address with DRBG or a few instances, never with a weaker generator.
Open with
My first suspect would be SecureRandom blocking on /dev/random. On older Linux kernels, before about 5.6, /dev/random blocks when the kernel's entropy estimate is low, and fresh VMs or containers often have very little entropy at boot.
Then, if they want more
Java reads it in certain configurations: securerandom.source in java.security defaults to /dev/random, and getInstanceStrong on Linux returns NativePRNGBlocking, which reads /dev/random for everything. Some libraries, or a servlet container creating its session id generator, hit this during startup.
I would confirm with a thread dump during the hang, which shows the thread inside NativePRNG reading the file, and check the kernel version and entropy_avail. The fix is to use plain new SecureRandom(), which is NativePRNG reading /dev/urandom and is cryptographically strong, and to create it once and reuse it, since it is thread-safe.
On old hosts I would add a hardware entropy source or upgrade the kernel, and only if needed set java.security.egd to file:/dev/urandom for that service, documenting why, because it is a process-wide change. For the slow token generation, I would measure first, for example with a profiler or JMH, because a shared instance serialises callers.
If it really is the bottleneck, I would try the DRBG algorithm or a few instances per thread, but never switch to Random or ThreadLocalRandom for tokens.
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
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
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 "After moving to new Linux hosts, a service's startup sometimes hangs for minutes inside...": https://scrivio-interview-library.netlify.app/java/types-and-values/84323f6f90/