News for hackers. Stuff that matters.
Re:Mistakes Beginners Make When Working with Databases by Jweb_Guru on Wednesday June 08, 2016 @12:15PM (#11863290)
UUIDs don't avoid all id collision issues, and it's far from final. They'll prevent you from using BRIN indexes effectively, for example. Timestamp plus unique server id is an arguably much better approach; on top of everything else, timestamps are actually useful, and they fit in 8 bytes.
Re:Mistakes Beginners Make When Working with Databases by JoeAltmaier on Wednesday June 08, 2016 @12:18PM (#11863310)
Denial isn't the same as argument. The definition of a UUID pretty much means they are. Enough bits and the chances of collision are less than that of a superintelligence spontaneously evolving in your morning coffee and hacking your computer. And its past time amateurs stopped thinking they've come up with the perfect UUID (timestamp + server id! Perfect! Until I set my clock back, or repurpose the server) and just use the vetted solutions available.
Re:Mistakes Beginners Make When Working with Databases by treebeard901 on Wednesday June 08, 2016 @01:24PM (#11863894)
In my experience this is only true if the UUID is generated by the database system in a controlled manner. It is not uncommon to have a unique identifier be used as a key in a database that comes from an external source. Consider an environment where you have a database that stores data for a monitoring system. So you decide to use the machine ID (a UUID) as a key. In your design you fail to take into account sys admins that do not clone a VM the correct way, so you wind up with two different machines with the same supposedly unique identifier. Sure, if you have a unique constraint on the key, you will find out about it pretty quickly, but it is just one example.
Re:Mistakes Beginners Make When Working with Databases by Jweb_Guru on Wednesday June 08, 2016 @12:23PM (#11863369)
Timestamp + server id can be uniquely guaranteed per server without coordination, so yes, it works fine :P Also, modern computers have monotonic clocks available and can use things like PTP.
Re:Mistakes Beginners Make When Working with Databases by IanCal on Thursday June 09, 2016 @06:57AM (#11868576)
Well you need to ensure that two servers never re-use the same ID so they can't come up with their own ID and there must be some form of co-ordination there. You also must ensure that you never enter two things into the database from the same server within one tick of the clock. Monotonic doesn't mean you'll never get the same number twice, just that it always changes in the same direction (in this case, the timestamp never gets lower, but nothing stops you getting the same number twice).
Re:Mistakes Beginners Make When Working with Databases by Jweb_Guru on Thursday June 09, 2016 @11:40AM (#11870111)
I'm aware. Step 1 is generally still considered under the "eventual consistency" umbrella (fork-join specifically). Step 2 doesn't really matter because all you actually need is monotonicity from the clock; locally it's really easy to guarantee uniqueness (and remember, it's fine to delay transactions as long as they commit eventually, which is one reason CAP is kind of imprecisely stated; you're only unavailable if you never respond to a request, when in practice it's really trying to convey latency requirement).
Re:Mistakes Beginners Make When Working with Databases by mercer on Wednesday June 08, 2016 @01:16PM (#11863829)
Could you elaborate on that? I use UUID's at the moment, trusting that they'll be unique, but in part because I don't properly understand them, I feel a little anxiety about the chance of a collision. I sort of assumed they already used timestamps as a basis already, but apparently they do not. Is it just a matter of it being highly improbably of generating the same UUID twice? I'm assuming yes on gut feeling, but I highly distrust that body part these days.

(Edit: I realize I could go look this up myself, but 1) I like explaining things so perhaps you do too, and 2) I'm hoping perhaps at the very least you can point me to a good resource to properly understand this topic)

Re:Mistakes Beginners Make When Working with Databases by lordnacho on Wednesday June 08, 2016 @01:34PM (#11863971)
If you write a sufficiently long series of random digits, it becomes increasingly likely that your number is the only use of that number anywhere in the universe. That's the crux of it.

I'm on a train now, but I think there's also a timestamp element to it somewhere, making it less likely that a restart of the random number generator will cause you to have a collision.

Re:Mistakes Beginners Make When Working with Databases by mikeash on Wednesday June 08, 2016 @12:39PM (#11863510)
Version 1 UUIDs are server ID (MAC address) plus timestamp. Your overall proposal is already included in "UUIDs." They don't fit into 8 bytes, though, so if you want to squeeze them down you'd have to come up with your own scheme.

If you do use this scheme, be careful that your timestamp is sufficiently granular that it's not possible to generate two identical IDs in quick succession....

Re:Mistakes Beginners Make When Working with Databases by Jweb_Guru on Wednesday June 08, 2016 @01:00PM (#11863704)
Granted re: UUIDs. There are definitely tradeoffs here (time precision / inserts per second [in practice you'll just end up throttling or massaging the timestamp if you do have insufficient granularity] vs. number of supported partitions vs. ability to efficiently route a path to the key vs. fast reservation ability vs. key size, etc., etc.). I was referring to the notion of a global 128-bit pseudorandom number as the final solution to everything... things are rarely that straightforward.
Re:Mistakes Beginners Make When Working with Databases by TheCoelacanth on Wednesday June 08, 2016 @03:11PM (#11864759)
Of the five UUID versions, only version 4 is pseudorandom. The other four versions are completely non-random.