Cache persistence on domain name

Under 4.x I’ve used a very simple cache rule to send requests to a set of caches. However, back in 4.2 PTF6, a ‘special’ feature was added to allow persistence to a cache based on the level of the host name (see below). I was wonder if this ‘feature’ was carried forward in 9.x or is there a iRule that will accomplish the same thing.

Here is how the feature works:

b internal set cache_uses_host_domain = 0 (default) - makes cache rules use the URI

b internal set cache_uses_host_domain = 1 - makes cache rule use 1st level TLD. (i.e. .com, .net, .org, etc.)

b internal set cache_uses_host_domain = 2 - makes cache rule use 2nd level domain (i.e. merck.com, yahoo.com, hotmail.com, etc.)

b internal set cache_uses_host_domain = 3 - makes cache rule use 3rd level domain (i.e. www.merck.com , ftp.yahoo.com

, www.slashdot.org ,etc.)

We have not yet implemented the 4.x cache rule command in 9.0. This is currently planned for the release in late spring.

In the meantime, I will look into writing a Tcl equivalent.

So is this why I get an error when I attempt to use the cache rule, even the example that is the manual?

when HTTP_REQUEST {  
   if { [HTTP::host] starts_with "abc" } { 
     cache { [HTTP::uri] ends_with "html" or [HTTP::uri] ends_with "gif" } { 
       origin_pool origin_server 
       cache_pool cache_servers 
       hot_pool cache_servers 
       hot_threshold 100 
       cool_threshold 10 
       hit_period 60 
       content_hash_size 1024 
     } else { 
       pool host named_servers 
     } 
   } 
 }

Yes, I must apologize for that.

This feature was originally scheduled to be implemented in the initial v9.0 release, but do to resource scheduling issues we did not get it completed. Unfortunately, we never got this additional information about it not getting completed communicated to the tech pubs folks and so the command was still documented.

Again, my apologies on this. We are working to get it implemented now and should have it available shortly, however, our next release cycle is not until spring. We will get you an interim version if you go through our support department. I will also look into implementing equivalent functionality using iRules (without the cache command) and post it here if it’s feasible.

Thanks for your patience on this.

Here is the current cache rule I am using in the 4.x:

cache (not (http_host == "xxxx")) {  
    origin_pool Caches  
    cache_pool Caches  
  }

And we are going down to the 2nd level domain (i.e. yahoo.com)

Here is an iRule I came up with. Will it do the same?

when HTTP_REQUEST {  
    if { [HTTP::host] != "xxxx"} {   
      pool cache_pool  
      persist universal [domain [HTTP::host] 2] 86400  
    } else {  
      pool cache_pool  
      persist universal [domain [HTTP::host] 2] 86400   
    }   
  }

Just check ing to see if there was a possibility of getting a TCL equivalent written for this functionality?

The following is an approximation of the logic used by the cache rule command. However, it does not remap uri’s to a new cache member when a cache member goes down. In this rule, that is instead handled at the time a request for the uri comes in. I don’t foresee this really being an issue though.

Here’s the rule:

when HTTP_REQUEST { 
    if {  } { 
       pool cache_pool 
       set key [crc32 [concat [domain [HTTP::host] 2] [HTTP::uri]]] 
       set cache_mbr [persist lookup hash $key node] 
       if { $cache_mbr ne "" } { 
           Make sure request isn't coming from the cache 
          if { [IP::addr [IP::remote_addr] equals $cache_mbr] } { 
              Yup, send the request from the cache to the origin pool 
             pool origin_pool 
             return 
          } 
       } 
        Make sure persistence record is added for this host/uri 
       persist hash $key 
    } else { 
       pool origin_pool 
    } 
 }

Substitute with whatever expression you would use to determine whether or not you want to send requests to the cache pool.

Oh, I forgot to mention that the above rule doesn’t handle the concept of hot content being spread across the entire cache.

To do that, the rule would have to track the hit rate which we currently can’t do efficiently because we have to use the Tcl clock command (which can have a potential performance impact).

You are welcome to try out the following rule which does track the hit rate and disables persistence so that the hot content is load-balance across the entire set of cache servers.

(I also further customized the rule to be closer to your original 4.x example - using the expression and pools, etc):

when HTTP_REQUEST {  
    if { [HTTP::host] starts_with "abc" and \ 
         ( [HTTP::uri] ends_with "html" or \ 
           [HTTP::uri] ends_with "gif" ) } {  
       pool cache_servers 
       set key [crc32 [concat [domain [HTTP::host] 2] [HTTP::uri]]]  
       set cache_mbr [persist lookup hash $key node]  
       if { $cache_mbr ne "" } {  
           Make sure request isn't coming from the cache  
          if { [IP::addr [IP::remote_addr] equals $cache_mbr] } {  
              Yup, send the request from the cache to the origin pool  
             pool origin_server 
          } else { 
             set cur_time [clock seconds] 
             set hit_info [session lookup hash $key] 
  
             if { $hit_info ne "" } { 
                set hit_time [lindex $hit_info 0] 
                set hit_total [lindex $hit_info 1] 
                set hit_list [lindex $hit_info 2] 
                set hot [lindex $hit_info 3] 
  
                set hit_count [lindex $hit_list 0] 
  
                 Calculate current hit rate 
                set $delta_time [expr $cur_time - $hit_time] 
  
                 If the amount of elapsed time is > hit period 
                if {$delta_time > 60} { 
                    Just re-initialize the entire hit period 
                   set hit_info "" 
                } else { 
                    Shift out the elapsed hits 
                   for {} {$delta_time > 0} {incr delta_time -1} { 
                      set hit_total [expr $hit_total - [lindex $hit_list 0]] 
                      set hit_list [lrange hit_list 1 end] 
                      lappend hit_list 0 
                   } 
                } 
             } 
  
             if { $hit_info eq "" } { 
                 Initialize hit period with no hits 
                for {set hot 0} {$hot <= 60} {incr hot} { 
                   lappend hit_list 0 
                } 
                set hit_count 0 
                set hit_total 0 
                set hot 0 
             } 
  
              Update current hit count 
             lset hit_list end [incr hit_count] 
             incr hit_total 
  
             if { $hit_total > 100 } { 
                 Above hot threshold 
                set hot 1 
             } elseif { $hot and $hit_total < 10 } { 
                 Below cool threshold 
                set hot 0 
             } 
             if { not $hot } { 
                 Use persistence to go to the same cache (with a 3600 secs timeout) 
                persist hash $key 3600 
             } 
  
             session add hash $key { $cur_time $hit_total $hit_list $hot } 
          }  
       } else { 
           Make sure persistence record is added for this host/uri  
          persist hash $key 3600 
       } 
    } else {  
       pool origin_pool  
    }  
 }

Warning: I have not tested this rule.

First of all I would like to THANK YOU for getting this iRule together. However, I have a few questions about the hash and the persistence.

Based on the rule, it creates a unique key (using crc32) based on the host/uri and then persists on key. The default persist time is 180 seconds. Don’t you think this would create a lot of persist records since the number of unique host/uri combinations are almost endless?

Based on the hash, will the BigIP always send it to the same member? For example, if a client requests and HTTP page and based on the hash the BigIP sends it to member cache-1 and then 10 minutes later another client requests the same page, will the BigIP hash and send the request back to memeber cache-1 even if the persist record is no longer there?

I also see that you can get the pool member that the hash would send the request to (set cache_mbr [persist lookup hash $key node]), is it possible to just send the request directly to the member and not worry about the persistence?

Yes, the default persist time is 180 seconds, however, if you notice in the second rule I actually set this to an hour. Yes, it does have the potential to create a lot of persist records, but they are fairly small and the system can support millions of them with no effect on performance.

Yes, the drawback to using regular persistence is that when the persist entry times out (due to lack of usage), then the new request will get load balanced to a new member. However, one of the side benefits to HASH persistence is that it even if there is no persist record, it will still persist to the member that is a result of taking the number of active pool member modulo the hash value. So, this should help keep the requests going to the same member even though the persist entry timed out. (I actually forgot about this last night and hence I don’t think increasing the persist timeout value is necessary).

Lastly, yes you could just set the request directly to the member and not worry about it, however, persistence already takes care of that so why bother? (I guess if you wanted to simulate the hash persistence behavior above, you could certainly do that).

I used the following rule and it works great with HTTP requests. However, it breaks HTTPS requests.

Any idea why???

when HTTP_REQUEST {  
    if { [HTTP::host] != "xxxxx"  } {  
       pool Caches  
       set key [crc32 [domain [HTTP::host] 2]]  
        Make sure persistence record is added for this domain  
       persist hash $key  
    } else {  
       pool Caches  
    }  
 }

Please describe what you mean by “breaks”. I don’t see anything in the above rule that should affect SSL. Thanks.

Basically, the setup is an F5 with 3 interfaces, one to the internal network, one to a segment with proxy/caches and the third to a segment with firewalls and then out to the Internet.

The browser is pointing at a VIP on the F5 that has the rule applied to it. Requests hit the VIP, get load balanced to the proxy/caches and then back through the F5 and out to the firewalls and the Internet. Regular HTTP works just fine.

However, when I attempt to get to an HTTPS site, it fails. From the traces, I see the client make the request, it gets sent to the proxy/cache, out to the Internet. The server replies HTTP/1.0 200 Connection established, which I see getting to the proxy/cache and then the proxy/cache sends it back to the client, but the client never recieves the packet. I’ve put sniffers on the client and on the internal F5 interface, it just never comes out.

Are you terminating the SSL before going to the cache?

Nope.

Just sending it to the proxy/cache and from the proxy/cache, just out to the firewalls.

I would then also assume your clients are configured to use the vip as a proxy?

If so, you have to understand that https connections are proxied in a very different manner than http. The client first sends a CONNECT method to the proxy which causes it to establish a connection to the host. Upon making that connection, the proxy replies with a 200 OK and then passes all subsequent data directly between the client and target host allowing the encryption to succeed.

In order to handle this in the bigip, we have had to add a HTTP::disable command that turns off http processing after the 200 OK response has occurred. This command will be available in 9.0.5.

Once you have this command, then you will need to modify your rule to look for the CONNECT method and issue the HTTP::disable command to allow the encrypted connection to flow freely between the client and destination host.

You can read the following post for more background information on proxying an https connection, however you likely won’t be able to use the rule in that post because it doesn’t work with the http profile:

http://devcentral.f5.com/default.aspx?tabid=28&forumid=5&postid=1274&view=topic

Hope this helps.

Correct, our client’s browsers are pointed at the VIP for their proxy.

Any idea when 9.0.5 will be available?

9.0.5 should be available, literally, any day now (possibly even today or tomorrow)…

Also, I’d like to point out that we are currently working on incorporating automatic support for the CONNECT method in the next major release (9.2). This means that the http parser will automatically disable itself after the connect method receives a 200 OK response from the proxy. So once this support is in, it will no longer be necessary to even write a special rule to handle it.

OK I see 9.0.5 is out, but I can not find in the release notes the HTTP::disable ‘feature’.

My Rule is currently:

when HTTP_REQUEST { 
    if { [HTTP::host] != "xxxxx" } { 
       pool Caches 
       set key [crc32 [domain [HTTP::host] 2]] 
        Make sure persistence record is added for this domain 
       persist hash $key 
    } else { 
       pool Caches 
    } 
 }

Where would I stick tghe HTTP::disable in???

Ok, I don’t have time to specifically test this, but I would assume sticking it at the end should be fine:

when HTTP_REQUEST { 
    if { [HTTP::host] != "xxxxx" } { 
       pool Caches 
       set key [crc32 [domain [HTTP::host] 2]] 
        Make sure persistence record is added for this domain 
       persist hash $key 
    } else { 
       pool Caches 
    } 
    HTTP::disable 
 }

You might alternatively only want to disable if the method is CONNECT, like so:

when HTTP_REQUEST { 
    if { [HTTP::host] != "xxxxx" } { 
       pool Caches 
       set key [crc32 [domain [HTTP::host] 2]] 
        Make sure persistence record is added for this domain 
       persist hash $key 
    } else { 
       pool Caches 
    } 
    if { [string toupper [HTTP::method]] eq "CONNECT" } { 
        Proxy connect method should continue as a passthru 
       HTTP::disable 
    } 
 }