I am reading a system design about TinyURL and it involves generating a unique hash ID for shortened URLs. It suggests two approaches for generating keys:
- On-demand: use hash function which takes input URL to generate a key
- Pre-creation: pre-create a pool of keys. Create two tables - one with used keys and one with un-used keys
My questions are then:
- Why would we need two tables for storing used and unused keys? Can't we just have a flag in the table that indicates whether a given key is being used or not?
- It suggests that as soon as we can load unused keys in application server memory, we can move them to used key table. In this way, multiple servers can't get the same key. But, I am not sure how this would prevent multiple servers from reading the same row. What if multiple servers try to concurrently read the same key?