DEV Community

Discussion on: URL shortener with Elixir and Phoenix

hlappa profile image
Aleksi Holappa Author

Thanks! And you are totally right! It would just fail and user will be greeted with an error message. If that happens, it is like winning in a lottery 10 times in a row. If you calculate how many permutations 8-char/digit string can have with upper- and lowercase letters, symbols, numbers etc. The number of total permutations would be enormous.

I think the current solution is viable option how to do things for this exact case. In the create function we could check if the custom id exists already in DB, but it will slow down the creation of the new shortened url.

Good point anyway! :)

rhamorim profile image
Roberto Amorim

I think checking is not necessary; you can just proceed with the insert, and, if any unique violation occurs, generate a new key and try inserting again, repeat until it works. That way you make sure it works while not incurring in any penalty on the "happy" path.

Thread Thread
hlappa profile image
Aleksi Holappa Author • Edited on

Yes! That would be even better! I’ll update the article at some point to handle it like this. 👍