473,405 Members | 2,344 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,405 developers and data experts.

Java UUID generation – Performance impact



Java developers tend to use ‘java.util.UUID#randomUUID()’ API, to generate a UUID (Universally Unique Identifier) number (i.e., ‘b8bbcbed-ca07-490c-8711-5118ee0af2f9’). Under certain circumstances, using this API can affect your application’s availability. Let’s discuss this API in this post with a real-world example.

How does *‘java.util.UUID#randomUUID()’ API works?
java.util.UUID#randomUUID() API internally uses ‘entropy‘ in the operating system to generate a unique number. What does ‘entropy’ mean? Linux kernel uses certain techniques like user’s mouse movements, variance in the hardware fan noise, variance in the noise of the device drivers … to generate random numbers. When there is a lack of ‘entropy’ in the operating system then random number generation will slow down. When there is a slowdown, application threads which are calling this ‘java.util.UUID#randomUUID()’ API call will be put in a BLOCKED state, and they wouldn’t be able to progress further.

If your application uses ‘java.util.UUID#randomUUID()’ API in a critical code path and there is a lack of entropy in the operating system, then multiple threads can enter into this BLOCKED state bringing your entire application to a grinding halt.

Real world application – 50 threads BLOCKED in java.util.UUID#randomUUID() API
Here is a real-world thread dump report of an application that was suffering from this problem. If you haven’t clicked on the hyperlink in the previous sentence, we request you do so. It would give the better context of the problem. (Note: in the thread dump report, we have changed the package name to ‘buggycompany’ to hide the identity of the application).

In the thread dump report, you can notice that there are 102 threads in total. In these 102 threads 50 threads are in the BLOCKED state due to ‘java.util.UUID#randomUUID()’ API call. Below is the stack trace of one of that 50 threads:


"[ACTIVE] ExecuteThread: '1' for queue: 'weblogic.kernel.Default (self-tuning)'" waiting for lock
java.security.SecureRandom@20a56b2b BLOCKED

java.security.SecureRandom.nextBytes(SecureRandom. java:433)
java.util.UUID.randomUUID(UUID.java:159)
com.buggycompany.jtm.bp.<init>(bp.java:185)
com.buggycompany.jtm.a4.f(a4.java:94)
com.buggycompany.agent.trace.RootTracer.topCompone ntMethodBbuggycompanyin(RootTracer.java:439)
weblogicx.servlet.gzip.filter.GZIPFilter.doFilter( GZIPFilter.java)
weblogic.servlet.internal.FilterChainImpl.doFilter (FilterChainImpl.java:56)
weblogic.servlet.internal.WebAppServletContext$Ser vletInvocationAction.wrapRun(WebAppServletContext. java:3730)
weblogic.servlet.internal.WebAppServletContext$Ser vletInvocationAction.run(WebAppServletContext.java :3696)
weblogic.security.acl.internal.AuthenticatedSubjec t.doAs(AuthenticatedSubject.java:321)
weblogic.security.service.SecurityManager.runAs(Se curityManager.java:120)
weblogic.servlet.internal.WebAppServletContext.sec uredExecute(WebAppServletContext.java:2273)
weblogic.servlet.internal.WebAppServletContext.exe cute(WebAppServletContext.java:2179)
weblogic.servlet.internal.ServletRequestImpl.run(S ervletRequestImpl.java:1490)
weblogic.work.ExecuteThread.execute(ExecuteThread. java:256)
weblogic.work.ExecuteThread.run(ExecuteThread.java :221)


Fig: Stack trace of a thread stuck while making ‘java.util.UUID#randomUUID()’ API call

You can notice that the thread got into a BLOCKED state when invoking ‘java.util.UUID#randomUUID()’ due to a lack of ‘entropy’ and unable to progress forward. Due to that 50 threads got stuck. Thus it was making the application unresponsive.

Checking entropy status in Linux
To check the availability of entropy in Linux execute the below command:


cat /proc/sys/kernel/random/entropy_avail


If you see the value to be less than 1000, then it indicates there is a lack of entropy. It may lead to BLOCKED threads in the applications.

Potential Solutions
If this problem surfaces in your application, the following are the potential solutions to address them:

1. RHEL
This problem has been resolved in RHEL 7 and above versions. If you can upgrade to RHEL 7 or above version, please do so.

If you are running on the older version of RHEL, you can follow the recommendations given here to fix this problem

2. Install Haveged in Linux
If your application is running in Linux, then you consider installing the ‘haveged’ library. The ‘haveged project‘ is meant to provide an easy-to-use, unpredictable random number generator based upon an adaptation of the HAVEGE algorithm. Here is the ‘Haveged’ project GIT repository page Here is how you can install it:

On Debian based platforms (Debian, Ubuntu):


sudo apt-get install rng-tools
sudo update-rc.d haveged defaults


On Redhat platforms (RHEL, Fedora, CentOS):


sudo yum install rng-tools
sudo chkconfig haveged on


3. Use /dev/urandom instead of /dev/random
Unix-like operating systems come up with special file ‘/dev/random’ that serve as pseudorandom number generators. Java uses this file to generate random numbers. You can configure it to use ‘/dev/urandom’ instead of ‘/dev/random’.

‘/dev/urandom’ is another special file that is capable of generating random numbers. However, it has the downside of reduced security due to less randomness. You can achieve it by passing the following JVM argument to your application during startup:


-Djava.security.egd=file:/dev/./urandom


Video
https://youtu.be/8Q_injWDm9M
Sep 3 '22 #1
0 7084

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Andrew James | last post by:
Hi, I've been looking around on Google for the answer to this question, and it's beginning to really bug me. I'm making some design decisions for some code I'm writing, and I'm wondering whether...
9
by: Aaron Anodide | last post by:
Hello, I am using the following template class as a shorthand for zero-ing memory: template<class T> class ZeroMem : public T { public: ZeroMem(void)
3
by: groups | last post by:
Hi all, I've recently ported a rather large C application to run multithreaded. A few functions have seriously deteriorated in performance, in particular when accessing a rather large global...
7
by: Magnus | last post by:
Im using the new binding features of Visual Studio 2005. I have done the steps to create a bound data source, and selected all 40 tables from the database. The wizard generated the necessary code...
6
by: Rich | last post by:
Hi, I know HOW to run these side by side. But what is the performance impact??? Microsoft make a big deal about how it is supported, but provide no guidelines as to potential performace or...
5
by: lister | last post by:
Hi all, I have a fairly diverse range of data that I want to cache in the session rather than pulling it from the database on every page refresh. The problem is is that it seems that PHP...
14
by: Sugandh Jain | last post by:
Hi, The warning from Microsoft.Performance Code Analysis check that, its not required to initialize numeric variables to zero, boolean to false and object to null is a good one because CLR does...
0
by: amollokhande1 | last post by:
Hi, I would like to know whether is any performance impact on sql server while using N as prefix in the sql query. For instance if have used following query to insert the data in fields of CHAR,...
3
by: CraCKerO | last post by:
Hello, I have a question about blob performance in sqlServer 2008 I have a table that contains about 64 columns from which two columns are blob (image), my question is, will these blob columns...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.