For my web application, I am expecting a peak load of about 1000 concurrent requests per second for a period of 5-10 minutes every business hour of the day. My web application needs to generate a 9 digit unique hexadecimal number, and send it as a response and also persist the same in the database.
As a solution, I am trying to pre-generate a Set of 5000 identifiers and keep it ready. During the peak load, I want my Servlet to simply keep popping out the generated the identifiers and use them in responses. When the generated Set count falls below 2500, I want to regenerate another 2500 unique identifiers and populate the same Set and keep it ready again.
Can someone please suggest a way to implement this?
If you persist it in a database anyway, I'd suggest to use a database mechanism for generating the IDs, such as a sequence or an autogenerated ID. You wouldn't finish handling the request till it's committed anyway, would you?
(I'm not sure about other databases, but Oracle's sequences can actually be configured to cache values exactly in the way you're describing. They are pretty fast.)
subject: Accessing pre-generated identifiers from a Servlet