Jump to content

Need help with algorithm


MegaPowerRockstar

Recommended Posts

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.
 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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. 

 

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

  • Upvote 1
Link to comment
Share on other sites

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. thappulunte-manninchu-pedarayudu-movie.g

kottaga nerchukuntunna

  • Haha 1
Link to comment
Share on other sites

  • 2 months later...
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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...