The problem I am trying to solve is to validate if a username is already used before allowing an admin user to create a new user with that username.

Currently I am doing an ajax request to check if the username is taken. This can cause a lag that detracts from the user experience - especially as they are encouraged to create more than 30 usernames at once during the onboarding process.

I wonder if there is a hashing algorithm that allows the backend to construct a string that the frontend can use to check if a new username is in the set.


on the backend

usernames_set = jim, carl, fred
my_hash = generate_hash(usernames_set) // returns e.g., 23223edw3234fcvdvc
# now pass this hash to the frontend.

on the frontend

checkValueInHash('jim') // returns true
checkValueInHash('nigel') // returns false

I appreciate I can simply pass the set of usernames to the frontend, but this would become infeasible as the number of items in the set becomes thousands. Also, there is security implications of proving a list of valid usernames.

Note I indent to solve the consistency problem (what happens when new users are created after the hash is sent to the frontend?) by using websockets to push a new string when new users are created.

Also note I appreciate I reduce this problem by sending username checks in bulk, but I would like to understand what other options are available too.

  • looks like I am more or less describing a bloom filter. – rikAtee Jun 1 '15 at 10:04
  • 1
    That's what I'd use - if it passes the bloom filter at the frontend, send the request and see if it fails. Note you can write up your solution as an answer and accept it yourself. – Useless Jun 1 '15 at 11:39
  • "This can cause a lag that detracts from the user experience..." this seems to call for an asynchronous approach (to keep the UI responsive, and not to discard partially-entered information from the user). Perhaps you should look for a design solution (not a coding solution) to this problem. – rwong Jun 1 '15 at 12:07

My current thinking is using a bloom filter: add all usernames to bloom filter on backend, compress the bits from the bloom filter and pass to frontend, inflate and populate a frontend bloom filter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.