Forum Discussion

Ulf_Wätterstam's avatar
Ulf_Wätterstam
Icon for Nimbostratus rankNimbostratus
Feb 11, 2011

Using election hash to load balance memCached

Hi,

 

 

I read the old tread http://devcentral.f5.com/Forums/tabid/1082223/asg/50/showtab/groupforums/aff/5/aft/19155/afv/topic/Default.aspx followed by the explanation articles http://devcentral.f5.com/Tutorials/TechTips/tabid/63/articleType/ArticleView/articleId/135/Hash-Load-Balancing-and-Persistence-on-BIG-IP-LTM.aspx and http://devcentral.f5.com/wiki/default.aspx/iRules/ElectionHashLoadBalancingAndPersistence.html .

 

Have anyone done this? I thought I made it to work but, same node do not always win….

 

 

My iRule

 

when RULE_INIT {

 

set ::mem_debug 1

 

}

 

 

when CLIENT_ACCEPTED {

 

if {$::mem_debug}{log local0.debug "[IP::remote_addr]:[TCP::remote_port] > [IP::local_addr]:[TCP::local_port] @ [virtual]"}

 

TCP::collect

 

}

 

 

when CLIENT_DATA {

 

set S ""

 

set cdata [TCP::payload]

 

if {$::mem_debug}{log local0.debug "Client Data: $cdata"}

 

set key [getfield $cdata " " 2]

 

if {$::mem_debug}{log local0.debug "Found key: $key"}

 

foreach N [active_members -list ftest-memCached] {

 

log local0.debug "Node to be evaluated: $N"

 

if { [md5 $N$key] > $S } {

 

set S [md5 $N$key]

 

set W $N

 

if {$::mem_debug}{log local0.debug "Node now in lead: $W"}

 

}

 

}

 

if {$::mem_debug}{log local0.debug "Node that won: $W for $key"}

 

pool ftest-memCached member [lindex $W 0] [lindex $W 1]

 

TCP::release

 

TCP::collect

 

}

 

 

Give me this in the log.

 

Feb 11 14:17:27 tmm tmm[1711]: Rule ElectionHashForMemCached : 10.22.6.101:56779 > 10.22.6.60:11211 @ virt-lab-memCached_SipLblNet

 

Feb 11 14:17:27 tmm tmm[1711]: Rule ElectionHashForMemCached : Client Data: set testKey 0 60 14 testingTesting

 

Feb 11 14:17:27 tmm tmm[1711]: Rule ElectionHashForMemCached : Found key: testKey

 

Feb 11 14:17:27 tmm tmm[1711]: Rule ElectionHashForMemCached : Node to be evaluated: 10.22.9.61 11211

 

Feb 11 14:17:27 tmm tmm[1711]: Rule ElectionHashForMemCached : Node now in lead: 10.22.9.61 11211

 

Feb 11 14:17:27 tmm tmm[1711]: Rule ElectionHashForMemCached : Node to be evaluated: 10.22.9.60 11211

 

Feb 11 14:17:27 tmm tmm[1711]: Rule ElectionHashForMemCached : Node that won: 10.22.9.61 11211 for testKey

 

 

Feb 11 14:17:27 tmm tmm[1711]: Rule ElectionHashForMemCached : 10.22.6.101:56780 > 10.22.6.60:11211 @ virt-lab-memCached_SipLblNet

 

Feb 11 14:17:27 tmm tmm[1711]: Rule ElectionHashForMemCached : Client Data: get testKey

 

Feb 11 14:17:27 tmm tmm[1711]: Rule ElectionHashForMemCached : Found key: testKey

 

Feb 11 14:17:27 tmm tmm[1711]: Rule ElectionHashForMemCached : Node to be evaluated: 10.22.9.61 11211

 

Feb 11 14:17:27 tmm tmm[1711]: Rule ElectionHashForMemCached : Node now in lead: 10.22.9.61 11211

 

Feb 11 14:17:27 tmm tmm[1711]: Rule ElectionHashForMemCached : Node to be evaluated: 10.22.9.60 11211

 

Feb 11 14:17:27 tmm tmm[1711]: Rule ElectionHashForMemCached : Node now in lead: 10.22.9.60 11211

 

Feb 11 14:17:27 tmm tmm[1711]: Rule ElectionHashForMemCached : Node that won: 10.22.9.60 11211 for testKey

 

 

As you see two different node wins even though the key is the same. What do I do wrong?

 

 

Thanks Ulf

 

 

P.S I'm using 9.4.8 HF4 D.S

 

 

2 Replies

  • Ok, I found out why.... So in case someone else is struggling with this I add what I found out.

     

    It is the way I take out the key.

     

     

    set key [getfield $cdata " " 2]

     

     

    This returns in case of string ‘set testKey 0 6….’ a string length of 7 (‘testKey’) but when the string is ’get testKey’ it will return a string length of 9 (‘testkey\r\n’ maybe). So I needed to trim my key.

     

     

    set key [string trim [getfield $cdata " " 2]]

     

     

    now I get the same value in my key and the algorithm works

     

  • George_Watkins_'s avatar
    George_Watkins_
    Historic F5 Account
    Here's an iApp for routing memcached requests: https://devcentral.f5.com/wiki/iapp.Memcached_iApp_Template.ashx