Redirect certain percentage of visitors to another page when traffic ramps up

I have been tasked with creating a script that could redirect 10%, 20%, 50% etc. of our sites traffic during peak times to a "technical difficulties" page, instructing the visitor to come back later in an effort to lessen the load of our applicants.

Our applicants are all funneled through a central landing page prior to applying, for this example lets say it is "www.example.com/index.php". So the question is, is it possible to create a script that during these high volume times we could go in and have it redirect every 5th, 10th, 20th visitor to "www.example.com/down.php"?

We are choosing to redirect these people not due to server load, but due to the fact we can only have a finite amount of applicants per month. This would create a few days where we could essentially ramp down our applicants so we don't go over that threshold.

I know how to completely redirect all visitors to a site but that will not work in this situation. I have browsed several similar questions on this site, and while still very helpful, I was not able to figure out how to properly handle a situation like this.

I appreciate any and all help you can give me, and/or just a point in the proper direction.

Answers


I would advice looking into other solutions, because this isn't a great way to treat your visitors, but if you really wanted to do it this way, use this:

$rand = mt_rand(1, 10);
if ($rand <= 3) { // In this case, 30% of your visitors get redirected.
    // redirect
// normal page

Presumably the users who are able to access your site would have access to more than one single page, so you would have to do something that would make it so that if a user has been granted access to the site, they would be remembered. To accomplish this, I would use sessions. So when a user accesses the site and doesn't have the session cookie present, just do:

if (rand(0,100) > 30) {
    // Set cookie
}
else {
    // so sorry. site is down for you
    // redirect
}

However, the question is, how exactly do you want to handle those who don't have access to the site? Because all they would have to do is refresh the page and 70% of the time, they will be granted access. If you want it to be permanent, you will have to find some way to blacklist them. You could do this in a cookie, but then a user could easily delete it and try again. The only way to make it persistent would be to store their IP address in the database. Which then brings us to our third problem, what about shared IPs? Perhaps you could check first for the session and then if it's not found, check for the blacklisted IP in the database. That way, the only way a user could get around it is to either have a dynamic IP or use a proxy.


It really depends on the situation:

  • If the server has no problem with the load at all and your actual goal is to limit the applications per day then this can be solved with a PHP script. Instead of redirecting to an error page I suggest telling the visitor the truth about the limit. This way the visitor doesn't hit [F5] several times in the hope that it will work a few seconds later as this increases server load even more. I also suggest setting a real limit instead of redirecting every n-th user.

  • If the server has problems handling the heavy load it might not even be able to redirect all requests. In this case you have to scale your server(s) to be able to handle the load. Possible solutions: use a load balancer a route traffic to different servers; upgrade your server; optimize your code (so the server might take the load without scaling your hardware)

  • If you only decided to set a limit of applications per day because the server is not able to handle the load, again scale your server(s) is the best solution.


Need Your Help

How to construct this NHibernate query

nhibernate hql

I have a one-to-many relationship. I would like to construct this query:

single file Django, DRF project

python django django-rest-framework

I am trying to bastardise Django and Django REST Framework into a single module so see if it can work. So far, I have the following code: