MegaPowerRockstar Posted December 9, 2021 Report Share Posted December 9, 2021 Hi, I need help with logic. My requirement is, when someone calls my svc i need to pick a value from 2 values (A and Y) based on percentage basis. A should be assigned 80% Y should be assigned 20% This is based on requests received to the service. Could you please help me how this can be achieved. Quote Link to comment Share on other sites More sharing options...
MegaPowerRockstar Posted December 9, 2021 Author Report Share Posted December 9, 2021 ltt Quote Link to comment Share on other sites More sharing options...
rmJU72 Posted December 9, 2021 Report Share Posted December 9, 2021 On receiving a request, generate a random number between 1 and 10. If <=8 choose A else choose B 1 Quote Link to comment Share on other sites More sharing options...
Vaampire Posted December 9, 2021 Report Share Posted December 9, 2021 Just now, rmJU72 said: On receiving a request, generate a random number between 1 and 10. If <=8 choose A else choose B This should work. other approach is tracking how many times a was picked and how many times b was picked. Quote Link to comment Share on other sites More sharing options...
MegaPowerRockstar Posted December 9, 2021 Author Report Share Posted December 9, 2021 If we have multiple instances code is deployed, every instance will have its own random generation right. Quote Link to comment Share on other sites More sharing options...
hunkyfunky2 Posted December 9, 2021 Report Share Posted December 9, 2021 2 minutes ago, MegaPowerRockstar said: If we have multiple instances code is deployed, every instance will have its own random generation right. Do you want algorithm or practical solution with existing tool / environment? Above Algo is 100% right assuming it's done at load balancer / gateway level. Quote Link to comment Share on other sites More sharing options...
rmJU72 Posted December 9, 2021 Report Share Posted December 9, 2021 It will average out to what you need. If 100 instances and each instance gets 100 requests, each instance will choose 80 of A and 20 of B. Total across instances will be 8000 of A and 2000 of B. Quote Link to comment Share on other sites More sharing options...
hunkyfunky2 Posted December 9, 2021 Report Share Posted December 9, 2021 5 minutes ago, hunkyfunky2 said: Do you want algorithm or practical solution with existing tool / environment? Above Algo is 100% right assuming it's done at load balancer / gateway level. If all instances get roughly same number (round robin ) then it will still work Quote Link to comment Share on other sites More sharing options...
MegaPowerRockstar Posted December 9, 2021 Author Report Share Posted December 9, 2021 ok thanks Quote Link to comment Share on other sites More sharing options...
michaeledwards Posted December 9, 2021 Report Share Posted December 9, 2021 I had similar situation va, create a dictionary with group name and count, for each request randomly pick a number between 1-100 and then if its betweem 0-80 assign A else assign B, and for next iteration ignore the previously selected values while generating the random. once you hit the 100 values, clear the dictionary Quote Link to comment Share on other sites More sharing options...
kathiramdasu Posted December 9, 2021 Report Share Posted December 9, 2021 What you are asking is how to implement canary deployment ? Basic - go through cycle of requests coming in 1-8 goes to A and last 2 goes to B. Reset the counter. Generate a random number between 0-1. if it is less than .8 send it to A more than .8 send to B. Here we cannot guarantee the distribution since any random generator in an language is not truly random, they are pseudo random. 1 Quote Link to comment Share on other sites More sharing options...
Spartan Posted December 9, 2021 Report Share Posted December 9, 2021 Anna ..first of all idi algorithm endi.. just a random number generator function Loadbalancer meeda weighted distribution pedite...simplle assign 80-20 done.. ledu function rayali ante.. module.exports.hola = async (event) => { let random = Math.floor((Math.random() * 10) + 1); if(random > 8){ statusCode: 200, //send traffic to A body: JSON.stringify( { message: 'sending traffic to A', }, null, 2 ), } return { statusCode: 200, //send traffic to B body: JSON.stringify( { message: 'sending traffic to B', }, null, 2 ), }; }; ps: @sri_india anna..paina code lo emaina tappul unte. kottaga nerchukuntunna 1 Quote Link to comment Share on other sites More sharing options...
Spartan Posted December 10, 2021 Report Share Posted December 10, 2021 implement chesava leda Quote Link to comment Share on other sites More sharing options...
BeeerBob123 Posted February 21, 2022 Report Share Posted February 21, 2022 On 12/9/2021 at 8:11 AM, MegaPowerRockstar said: Hi, I need help with logic. My requirement is, when someone calls my svc i need to pick a value from 2 values (A and Y) based on percentage basis. A should be assigned 80% Y should be assigned 20% This is based on requests received to the service. Could you please help me how this can be achieved. ok Quote Link to comment Share on other sites More sharing options...
rajnik Posted February 21, 2022 Report Share Posted February 21, 2022 If you are using Spring, try spring cloud gateway https://cloud.spring.io/spring-cloud-static/spring-cloud-gateway/2.0.2.RELEASE/single/spring-cloud-gateway.html Its not going to be 100% exact but it works approximately for traffic routing. I had to implement custom traffic routing since my management wanted exact numbers. For ex: If it is configured 60/40 they wanted 60% traffic to goto one server and 40% to another exactly. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.