Forum Discussion

santosh_81454's avatar
santosh_81454
Icon for Nimbostratus rankNimbostratus
Sep 05, 2012

Redirect every 4th reuqest to external site

We are in process of migrating our old website to a new website. The new website is hosted on a different set of servers. As per management decision at the initial phase we are asked to redirect 25% of the traffic to the new site.

 

I was thinking of using the rand funtion as below (www.abc.com - Old site ; www.xyz.com - new site):

 

when CLIENT_ACCEPTED {

 

if { rand() < 0.25 } {

 

HTTP::redirect "https://www.xyz.com"

 

}

 

}

 

But the problem is as www.xyz.com is hosted else where, the connection count never reaches 25% and all my traffic is being directed to the new site.

 

Did anyone else, implement it using irule for % directions. ?

 

 

Thanks,

 

Santosh.

 

  • I think the problem with this implementation is that CLIENT_ACCEPTED fires several times within a user session. So while some of the connections won't initially be redirected, they eventual will trigger the redirect logic and be sent to the other server.

    Given that this is HTTP traffic, you probably also want to be mindful of sessions. Here's a crude little example of sending every fourth *NEW* request to the new site by way of a tracking cookie and counter session table.

    
    when HTTP_REQUEST {
     if the tracking cookie doesn't exist, determine skip count and redirect if necessary
    if { not ( [HTTP::cookie exists TRACKER] ) } {
    log local0. "TRACKER cookie doesn't exist"
     set cookie flag
    set setcookie 1
     detemine skip count (%25 of traffic)
    if { [table lookup -subtable TRACKERTABLE "track"] eq "" } {
    log local0. "tracker table doesn't exist - create it and set value to 1"
    table set -subtable TRACKERTABLE "track" 1
    } elseif { [expr [table lookup -subtable TRACKERTABLE "track"] < 4] }  {
    log local0. "tracker table value less than 4 - increment and allow local traffic"
    table incr -subtable TRACKERTABLE "track"
    } elseif { [table lookup -subtable TRACKERTABLE "track"] == 4 } {
    log local0. "tracker table equals 4 - reset to 1 and redirect to new site"
    table set -subtable TRACKERTABLE "track" 1
    HTTP::redirect "https://www.xyz.com"
    }
    } else {
    log local0. "TRACKER cookie exists"
    }
    }
    when HTTP_RESPONSE {
     if setcookie exists, create TRACKER cookie in response
    if { [info exists setcookie] and $setcookie == 1 } {
    HTTP::cookie insert name TRACKER value 1
    unset setcookie
    }
    }
     

    So when the user connects for the first time, they won't have the tracker cookie. You consult the session table to determine the count, redirect at 4 (and reset to 1), and simply increment the value if not 4. If the connection is not redirected, the HTTP_RESPONSE sends the tracker cookie so that all future requests from this client will not get redirected.

  • I think the problem with this implementation is that CLIENT_ACCEPTED fires several times within a user session. So while some of the connections won't initially be redirected, they eventual will trigger the redirect logic and be sent to the other server.

    Given that this is HTTP traffic, you probably also want to be mindful of sessions. Here's a crude little example of sending every fourth *NEW* request to the new site by way of a tracking cookie and counter session table.

    
    when HTTP_REQUEST {
     if the tracking cookie doesn't exist, determine skip count and redirect if necessary
    if { not ( [HTTP::cookie exists TRACKER] ) } {
    log local0. "TRACKER cookie doesn't exist"
     set cookie flag
    set setcookie 1
     detemine skip count (%25 of traffic)
    if { [table lookup -subtable TRACKERTABLE "track"] eq "" } {
    log local0. "tracker table doesn't exist - create it and set value to 1"
    table set -subtable TRACKERTABLE "track" 1
    } elseif { [expr [table lookup -subtable TRACKERTABLE "track"] < 4] }  {
    log local0. "tracker table value less than 4 - increment and allow local traffic"
    table incr -subtable TRACKERTABLE "track"
    } elseif { [table lookup -subtable TRACKERTABLE "track"] == 4 } {
    log local0. "tracker table equals 4 - reset to 1 and redirect to new site"
    table set -subtable TRACKERTABLE "track" 1
    HTTP::redirect "https://www.xyz.com"
    }
    } else {
    log local0. "TRACKER cookie exists"
    }
    }
    when HTTP_RESPONSE {
     if setcookie exists, create TRACKER cookie in response
    if { [info exists setcookie] and $setcookie == 1 } {
    HTTP::cookie insert name TRACKER value 1
    unset setcookie
    }
    }
     

    So when the user connects for the first time, they won't have the tracker cookie. You consult the session table to determine the count, redirect at 4 (and reset to 1), and simply increment the value if not 4. If the connection is not redirected, the HTTP_RESPONSE sends the tracker cookie so that all future requests from this client will not get redirected.