mercredi 3 février 2016

Distribute non random 64 bit ints randomly in javascript

What I want

I have a set of new V2 api I would like to switch to becasue they are faster, better, and shinier then my V1 api's.

My Problem

My problem is my business thinks we are in a high risk part of the year because we are seasonal and make most of our money now. They are saying I have to wait 4 more months to turn on my new API's.

Solution

My solution is I want to be able to slowly turn the services on for say 4% of users first then slowly increasing the percentage as the company gains confidence in them.

To accomplish this there are 2 conditions I would like to meet:

  1. Decide which API I should call quickly on the client so I do not slow down the user experience.
  2. I want to continue consistently sending a user to the same API's if they fall into 4% group.

My app starts up with a config call where I will return an int representing the percent of users I want to see V2.

percentCallV2 = 4

Each user has an assigned 64 bit int userId assigned to them. Unfortunately these are not generated in a random fashion and are decided based off things like what data center they first visited. To change this is out of my control and would take much longer.

I want to be able to do :

if(userId%100 =< percentCallV2){
  //Call V2 API
}else{
  //Call V1
}

The problem is since the userId's are not random this could end up in alot more then 4% of users being sent to new V2 API's which would be bad especially if new API's can't handle all the load yet. To fix this I would like to run the userId through some sort of hash function that would randomly distribute the values in a consistent manner. This would enable a user to consistently call V2 api if they fell within percentage and I could be confident that I was only sending the amount of traffic I had intended.

Code would look something like this:

function randomHash(){
   //????
}

if(randomHash(userId)%100 =< percentCallV2){
  //Call V2 API
}else{
  //Call V1
}

Need Advice

What are some elegant, simple, and easy to explain ways to do this in javascript? It does not need to perfectly random but pretty close.




Aucun commentaire:

Enregistrer un commentaire