You should always add a UNIQUE to the database column. This will create an implicit index to improve the search for this column, and it will ensure that none of the two records ever have the same value. Thus, in the worst case, you will get a database exception, not a security violation.
In addition, depending on how often you need to create unique tokens, I find that in most cases it is great to use the database search during generation. If your column is indexed correctly, this will be a fairly quick query. Most databases are very scalable horizontally, so if you are creating the next Facebook, this is again an option. In addition, you will probably need to complete a request to verify the uniqueness of E-Mail.
Finally, if you are really concerned about performance, you can always pre-create one million unique tokens and save them in a separate database table for quick use. Just configure the procedure to periodically check its use and, if necessary, insert more entries into it. However, as pointed out by @MacroMan in the comments, this can have security implications if someone gets access to the list of pre-generated tokens, so this practice should be avoided.
source share