I want to create a unique user name for my users, but this will be not system generated. Users can choose his/her user name.

DB
--

MongoDB

### Ex

Github user name

Current process
---------------

1. sign-up
2. get a verification link on the email
3. go to the link, like `example.com/verify?token=xyz...`

On verify page user can enter a user name, that user name must be unique across the DB.I don't want to keep duplicate user names.

> 
> Many websites have implemented this feature, like Github, GitLab, etc. But, I have explored some web about implementing this feature and I didn't get the desired result.
> 
> 
> 

4. After entering the user name in step 3, the browser will make a `POST` request to the server.
5. Server will then check the DB that if this user name already exists or not, if this already exists then throw an error or process the user name otherwise.

This is working on my local PC, but I am not sure whether this will work when multiple users make a request with the same username within a time interval. Then, in this case, there may be some duplicates in DB.

case
----

There are two users A and B wants to create their user name on the system at the same time.

> 
> both are requesting the same user name
> 
> 
> 

1. A: make the request for the user name `u1`
2. Server: check for A's user name is present or not in DB
3. B: make the request for user name `u1`
4. Server: A's user name is not present, save `u1` as A's user name
5. Server: B's user name is not present, save `u1` as B's user name

You can see, in the end, both users's haveing the same user name.

Is there any way to avoid this??