0

I have issue with generating global unique request id based on HttpServletRequest. I need to make sure the generated id is unique in Application scope. Java UUID Or Random does not guarantee what I want although the chance for duplicate is very low.

Anyone has any ideas? Thanks,

LHA
  • 9,398
  • 8
  • 46
  • 85

2 Answers2

1

If you have to be absolutely sure you don`t make a duplicate you could use an auto increment database id, but given the incredibly low chances of duplicates using UUID i dont think it justifies the cost.

  • Thanks. I think we can use combination of db sequence and random since I want the id is secured. – LHA Nov 22 '13 at 15:59
1

Random definitely doesn't guarantee uniqueness, but UUID definitely does that. So your concern makes no sense. Even if you've hardware capable of serving a billion HTTP requests per second, you'd only after 100 years reach 50% chance on a duplicate.

If you have really really a hard head in this, just check in synchronized block if the application scope contains already such a key and if so, then just generate another one. And so on. This way you can even use Random.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thank you for your answer. I will go with Random and checking the ID (Key) if it existing. – LHA Nov 22 '13 at 16:13