educative.io

What are the strategies to avoid key duplication and concurrency issues

Please clarify the following,

  1. What strategies are used to avoid key duplication? If we take the first 6 characters of base64, we are increasing the chances of key collision. In the post, it is mentioned that “we can choose some other characters out of the encoding string or swap some characters”. Please elaborate.
  2. What strategies are used for new key insertion to DB and duplicate checking when concurrent requests are being processed? It could be possible that while one request has generated a short key and checks that it is not duplicate but just before insertion, another request could generate the same key and succeed inserting. Is there a global lock employed so that untill a request completes DB insertion other requests wait? but this could slow down the system significantly.