Java
Tomcat
SecureRandom
Startup Time
Performance Optimization

Tomcat takes too much time to start - Java SecureRandom

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Introduction

When deploying Java-based applications, one of the common concerns that arise with Apache Tomcat is the extended time it takes to start. A frequent underlying cause for this issue is the SecureRandom class in Java, which can cause significant delays, especially in systems that rely on the default entropy source.

This article aims to provide a comprehensive explanation of how SecureRandom can impact Tomcat startup times, and explore potential solutions that can be implemented to mitigate such delays.

Understanding SecureRandom

SecureRandom is a class in Java's java.security package that provides a cryptographically strong random number generator (RNG). It plays a critical role in generating secure random keys for encryption and other security purposes.

How SecureRandom Works

SecureRandom typically relies on system entropy sources for randomness. On UNIX-like systems, it often uses /dev/random or /dev/urandom. The distinction is crucial:

  • /dev/random blocks if there is not enough entropy to ensure randomness. This can lead to delays if the system has low entropy.
  • /dev/urandom does not block even when entropy is low, instead reusing its internal pool of random numbers.

Impact on Tomcat

Tomcat may use SecureRandom either directly or indirectly during startup for session ID generation, SSL context initialization, or other cryptographic operations. If SecureRandom defaults to a blocking entropy source like /dev/random, startup time can increase noticeably, particularly on headless or virtualized environments lacking substantial user interaction to generate entropy.

Solutions to Reduce Startup Time

Several strategies can be employed to alleviate startup delays caused by SecureRandom:

  1. Switch entropy source
    Configure the Java Security properties file ($JAVA_HOME/lib/security/java.security) to use /dev/urandom instead of /dev/random. Modify the setting as follows:
    • Using the haveged daemon to increase entropy.
    • Incorporating hardware RNGs if available.
    • Ninja-level: improvising with entropy-generating scripts or background tasks.
  • Security Trade-offs: Switching to /dev/urandom might engender the rare risk of lower initial randomness if the system boots with extremely low entropy. However, for most applications, this trade-off is acceptable given the non-blocking nature and subsequent rapid entropy replenishment.
  • Environment Monitoring: Regularly monitor and assess changes in system entropy to adjust configurations dynamically, whenever necessary, to maintain optimal performance.
  • Application Profiling: Profile your application during startup to identify sections that utilize SecureRandom and evaluate whether less secure, but faster, RNGs can be pragmatically substituted.

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.