What are the costs/benefits of using :binary_id for primary keys instead of :id? (ecto schema)

Hi, I’m looking into switching my resources to use binary_id’s for the key (see https://hexdocs.pm/ecto/Ecto.Schema.html). This is because this would give me unpredictable urls which could be shared even for ‘private’ resources. i.e. they are undiscoverable on the site but public if you know the URL.

The benefits of plain ids I can see are:

  • predictable (easier for testing, user discovery/scraping)
  • shorter urls
  • less initial setup (out-of-the-box behaviour with ecto/phoenix)
  • sense of history from url i.e roughly when the resource was created

The downsides:

  • predictable (easier for testing, user discovery/scraping)!

What do you all think? Are there any other downsides to switching from :id primary keys? (Apart from a small performance cost). And what do you use?

1 Like

There is one more big reason for switching to UUIDs: you can generate them on the client-side. This makes stuff like building single page apps slightly easier if you do it right. Also, building stuff like CQRS/events based system is easier if you can generate IDs of things before they actually hit the databsae.

6 Likes