Forum Discussion

Sashi_81625's avatar
Sashi_81625
Icon for Nimbostratus rankNimbostratus
Mar 26, 2012

persisting the jsessionid cookie within F5 session table

 

Use Case:

 

we have in-house search servers that will crawl the customer-facing application. the limitations with our search software is that it doesnt persist the connections to app server and also cannot resubmit the jsessionid cookie.

 

because of these limitations, we noticed many performance issues including session overflow scenario on app servers.

 

 

How iRule helped

 

since we were already using F5 in our design, I explored the possibility of using iRules to store the jsessionid cookies and then reuse them over the crawl period.

 

And it worked out pretty well, we were able to bring down the application sessions from over 150K to just 300.

 

 

Notes:

 

1. our application flow presented a specific pattern for all requests - the initial request to app server always end up in 301 without any jsessionid cookie created.

 

2. the subsequent requests will have a response code of 302 or 200 with session cookie being created (if not already present). also, incase of 302s, there will be no query parameters.

 

 

below is the code

 

 

  •  
    when HTTP_REQUEST {
    
      set sid ""
      set hostid [getfield [HTTP::host] "." 1]
      if { [HTTP::uri] starts_with "/static" } {
         HTTP::respond 200 OK
      } else {
        set key [URI::query [HTTP::uri] "f5key"]
        if {$key != ""} {
            if f5key is present in query parameters
            persist the request first
           persist uie "$hostid$key"
           set path [URI::decode [URI::path[HTTP::uri]]]
            retrieve the jsessionid cookie value from F5 session table
           set sid [table lookup "$key$hostid"]
            insert the cookie value so that application can reuse it
           HTTP::header insert Cookie "JSESSIONID=$sid"
            update the URI without query parameters
           HTTP::uri $path
        }
      }
    }
    
    when SERVER_CONNECTED {
    
       I chose 1200 as timeout because application 
       has 20mins as session idle timeout
      set prid [table add "pkey$hostid" 5999 1200]
      if { $prid < 6300 } {
        set prid [table incr "pkey$hostid"]
         create persistence record based on hostname and key
        persist add uie "$hostid$prid" 1200
      }
    }
    
    when HTTP_RESPONSE {
    
      table add "skey$hostid" 5999 1200
      if {$key != ""} {
         set cid $key
      } else {
         set cid [table incr "skey$hostid"]
         if { $cid > 6300 } {
            set cid [table set "skey$hostid" 6000 1200]
         }
      }
    
      if JSESSIONID shows up in set-cookie header 
      store it in F5 session table
      if { [HTTP::cookie exists "JSESSIONID"] } {   
        set sid [HTTP::cookie value JSESSIONID]  
        table set "$cid$hostid" $sid 1200
      }
    
      if { [HTTP::status] starts_with "3" } {
         set location [URI::decode [HTTP::header Location]]
          pass the key as query parameter
         set query "?f5key=$cid"
         set newLocation "$location$query"
         HTTP::header replace Location $newLocation
      }
    
    }