I want to use this Go package https://github.com/bwmarrin/snowflake to generate primary int64 keys for my tables in Postgresql. If my application server is running on at least two machines how could I prevent duplicate keys from being generated?
So snowflake provides 63 bit integer stored in an int64. According to the documentation you can generate 4096 unique IDs every millisecond, per Node ID. Let's take the default implementation.That is 4096 * 1023 = 40961023 id's per millisecond and if you calculate in one second you can generate billions of unique id across multiple nodes and will be very rare to get conflict.
So i think if you pass a node id in env variable of the server and generate id's based upon that you should be safe.
It also helps to add some prefix to the id based upon the entity or domain so that you get more entropy which will reduce the conflicts even less.