Created
October 15, 2011 16:24
-
-
Save ajohnstone/1289808 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$nodes = array_filter(explode(' ',$_SERVER['argv'][1])); | |
$memcache = new Memcached; | |
//$memcache->setOption ( Memcached::OPT_DISTRIBUTION , Memcached::DISTRIBUTION_MODULA ); | |
$memcache->setOption ( Memcached::OPT_DISTRIBUTION , Memcached::DISTRIBUTION_CONSISTENT ); | |
foreach($nodes AS $node) { | |
list($host, $port) = explode(':',$node); | |
$memcache->addServer($host, $port); | |
} | |
$key = "test"; | |
$value = "test"; | |
var_dump( | |
$memcache->set($key, $value, 0) | |
); | |
var_dump($memcache->get($key), $key); | |
//Memcached::OPT_SERIALIZER | |
//Memcached::SERIALIZER_PHP | |
//Memcached::SERIALIZER_IGBINARY | |
//Memcached::SERIALIZER_JSON |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
iface="eth0" | |
range="192.168.2"; | |
server_count=10; | |
. /lib/lsb/init-functions | |
function debug_memcache() { | |
pid=`ps aux | grep memcache | grep "$1" | awk '{print $2}'` | |
strace -p $pid -o /tmp/memcached.strace.$2.log & | |
tcpdump -s 65535 -vv -x -n -q -tttt -i lo dst $1 and port 11211 > /tmp/memcached.tcpdump.$2.log & | |
} | |
case $1 in | |
start) | |
log_daemon_msg "Starting memcached" "memcached" | |
for i in $(seq 0 $server_count); do | |
if ! ip addr show | grep -q "$range.$i/24" > /dev/null; then | |
ip addr add $range.$i/24 dev eth0 | |
fi; | |
memcached -l "$range.$i" -uroot -d | |
debug_memcache "$range.$i" $i | |
done; | |
;; | |
stop) | |
log_daemon_msg "Stopping memcached" "memcached" | |
for i in $(seq 0 $server_count); do | |
if ps aux | grep memcache | grep "$range.$i"; then | |
kill -9 `ps aux | grep memcache | grep "$range.$i" | awk '{print $2}'`; | |
fi; | |
if ps aux | grep tcpdump | grep "$range.$i"; then | |
kill -9 `ps aux | grep tcpdump | grep "$range.$i" | awk '{print $2}'`; | |
fi; | |
if ip addr show | grep -q "$range.$i/24" > /dev/null; then | |
ip addr del $range.$i/24 dev eth0 | |
fi; | |
done; | |
;; | |
restart) | |
log_daemon_msg "Restarting memcached" "memcached" | |
./$0 stop | |
./$0 start | |
;; | |
test) | |
log_daemon_msg "Testing memcached" "memcached" | |
server_list=""; | |
for i in $(seq 0 $server_count); do | |
server_list="$server_list $range.$i:11211"; | |
done; | |
php memcached.key.distribution.test.php "$server_list" | |
;; | |
status) | |
;; | |
*) | |
log_success_msg "Usage: $0 {start|stop|restart|status|test}" | |
exit 1 | |
;; | |
esac | |
echo ""; | |
#echo "stats" | nc 162.31.1.9 11211 | |
#echo "stats settings" | nc 162.31.1.9 11211 | |
#Consistent Hashing | |
#Consistent Hashing is a model that allows for more stable distribution of keys given addition or removal of servers. In a normal hashing algorithm, changing the number of servers can cause many keys to be remapped to different servers, causing huge sets of cache misses. Consistent Hashing describes methods for mapping keys to a list of servers, where adding or removing servers causes a very minimal shift in where keys map to. | |
#So in short, with a normal hashing function, adding an eleventh server may cause 40%+ of your keys to suddenly point to different servers than normal. | |
#However, with a consistent hashing algorithm, adding an eleventh server should cause less than 10% of your keys to be reassigned. In practice this will vary, but it certainly helps. | |
#TODO: I know there's a better discussion of this that's linkable. help find it? I can never describe it well enough. | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2010-06-02 07:00:35.940609 IP6 ::1.45043 > ::1.11211: tcp 0 | |
0x0000: 6000 0000 0028 0640 0000 0000 0000 0000 | |
0x0010: 0000 0000 0000 0001 0000 0000 0000 0000 | |
0x0020: 0000 0000 0000 0001 aff3 2bcb a01b ce68 | |
0x0030: 0000 0000 a002 7ff0 ee24 0000 0204 3ff8 | |
0x0040: 0402 080a 0008 555b 0000 0000 0103 0306 | |
2010-06-02 07:00:35.940642 IP6 ::1.11211 > ::1.45043: tcp 0 | |
0x0000: 6000 0000 0014 0640 0000 0000 0000 0000 | |
0x0010: 0000 0000 0000 0001 0000 0000 0000 0000 | |
0x0020: 0000 0000 0000 0001 2bcb aff3 0000 0000 | |
0x0030: a01b ce69 5014 0000 658b 0000 | |
2010-06-02 07:00:35.940711 IP 127.0.0.1.57157 > 127.0.0.1.11211: tcp 0 | |
0x0000: 4510 003c 489e 4000 4006 f40b 7f00 0001 | |
0x0010: 7f00 0001 df45 2bcb cb4d 1dc2 0000 0000 | |
0x0020: a002 8018 4609 0000 0204 400c 0402 080a | |
0x0030: 0008 555c 0000 0000 0103 0306 | |
2010-06-02 07:00:35.940732 IP 127.0.0.1.11211 > 127.0.0.1.57157: tcp 0 | |
0x0000: 4500 003c 0000 4000 4006 3cba 7f00 0001 | |
0x0010: 7f00 0001 2bcb df45 cb31 4802 cb4d 1dc3 | |
0x0020: a012 8000 dd77 0000 0204 400c 0402 080a | |
0x0030: 0008 555c 0008 555c 0103 0306 | |
2010-06-02 07:00:35.940758 IP 127.0.0.1.57157 > 127.0.0.1.11211: tcp 0 | |
0x0000: 4510 0034 489f 4000 4006 f412 7f00 0001 | |
0x0010: 7f00 0001 df45 2bcb cb4d 1dc3 cb31 4803 | |
0x0020: 8010 0201 c49a 0000 0101 080a 0008 555c | |
0x0030: 0008 555c | |
2010-06-02 07:00:39.184468 IP 127.0.0.1.57157 > 127.0.0.1.11211: tcp 7 | |
0x0000: 4510 003b 48a0 4000 4006 f40a 7f00 0001 | |
0x0010: 7f00 0001 df45 2bcb cb4d 1dc3 cb31 4803 | |
0x0020: 8018 0201 fe2f 0000 0101 080a 0008 5887 | |
0x0030: 0008 555c 7374 6174 730d 0a | |
2010-06-02 07:00:39.184508 IP 127.0.0.1.11211 > 127.0.0.1.57157: tcp 0 | |
0x0000: 4500 0034 1d5f 4000 4006 1f63 7f00 0001 | |
0x0010: 7f00 0001 2bcb df45 cb31 4803 cb4d 1dca | |
0x0020: 8010 0200 be3e 0000 0101 080a 0008 5887 | |
0x0030: 0008 5887 | |
2010-06-02 07:00:39.184751 IP 127.0.0.1.11211 > 127.0.0.1.57157: tcp 788 | |
0x0000: 4500 0348 1d60 4000 4006 1c4e 7f00 0001 | |
0x0010: 7f00 0001 2bcb df45 cb31 4803 cb4d 1dca | |
0x0020: 8018 0200 013d 0000 0101 080a 0008 5887 | |
0x0030: 0008 5887 5354 4154 2070 6964 2032 3634 | |
0x0040: 340d 0a53 5441 5420 7570 7469 6d65 2032 | |
0x0050: 3433 310d 0a53 5441 5420 7469 6d65 2031 | |
0x0060: 3237 3534 3538 3433 380d 0a53 5441 5420 | |
0x0070: 7665 7273 696f 6e20 312e 342e 350d 0a53 | |
0x0080: 5441 5420 706f 696e 7465 725f 7369 7a65 | |
0x0090: 2033 320d 0a53 5441 5420 7275 7361 6765 | |
0x00a0: 5f75 7365 7220 302e 3030 3030 3030 0d0a | |
0x00b0: 5354 4154 2072 7573 6167 655f 7379 7374 | |
0x00c0: 656d 2030 2e30 3030 3030 300d 0a53 5441 | |
0x00d0: 5420 6375 7272 5f63 6f6e 6e65 6374 696f | |
0x00e0: 6e73 2035 0d0a 5354 4154 2074 6f74 616c | |
0x00f0: 5f63 6f6e 6e65 6374 696f 6e73 2038 0d0a | |
0x0100: 5354 4154 2063 6f6e 6e65 6374 696f 6e5f | |
0x0110: 7374 7275 6374 7572 6573 2036 0d0a 5354 | |
0x0120: 4154 2063 6d64 5f67 6574 2031 0d0a 5354 | |
0x0130: 4154 2063 6d64 5f73 6574 2030 0d0a 5354 | |
0x0140: 4154 2063 6d64 5f66 6c75 7368 2030 0d0a | |
0x0150: 5354 4154 2067 6574 5f68 6974 7320 300d | |
0x0160: 0a53 5441 5420 6765 745f 6d69 7373 6573 | |
0x0170: 2031 0d0a 5354 4154 2064 656c 6574 655f | |
0x0180: 6d69 7373 6573 2030 0d0a 5354 4154 2064 | |
0x0190: 656c 6574 655f 6869 7473 2030 0d0a 5354 | |
0x01a0: 4154 2069 6e63 725f 6d69 7373 6573 2030 | |
0x01b0: 0d0a 5354 4154 2069 6e63 725f 6869 7473 | |
0x01c0: 2030 0d0a 5354 4154 2064 6563 725f 6d69 | |
0x01d0: 7373 6573 2030 0d0a 5354 4154 2064 6563 | |
0x01e0: 725f 6869 7473 2030 0d0a 5354 4154 2063 | |
0x01f0: 6173 5f6d 6973 7365 7320 300d 0a53 5441 | |
0x0200: 5420 6361 735f 6869 7473 2030 0d0a 5354 | |
0x0210: 4154 2063 6173 5f62 6164 7661 6c20 300d | |
0x0220: 0a53 5441 5420 6175 7468 5f63 6d64 7320 | |
0x0230: 300d 0a53 5441 5420 6175 7468 5f65 7272 | |
0x0240: 6f72 7320 300d 0a53 5441 5420 6279 7465 | |
0x0250: 735f 7265 6164 2036 330d 0a53 5441 5420 | |
0x0260: 6279 7465 735f 7772 6974 7465 6e20 3136 | |
0x0270: 3133 0d0a 5354 4154 206c 696d 6974 5f6d | |
0x0280: 6178 6279 7465 7320 3637 3130 3838 3634 | |
0x0290: 0d0a 5354 4154 2061 6363 6570 7469 6e67 | |
0x02a0: 5f63 6f6e 6e73 2031 0d0a 5354 4154 206c | |
0x02b0: 6973 7465 6e5f 6469 7361 626c 6564 5f6e | |
0x02c0: 756d 2030 0d0a 5354 4154 2074 6872 6561 | |
0x02d0: 6473 2034 0d0a 5354 4154 2063 6f6e 6e5f | |
0x02e0: 7969 656c 6473 2030 0d0a 5354 4154 2062 | |
0x02f0: 7974 6573 2030 0d0a 5354 4154 2063 7572 | |
0x0300: 725f 6974 656d 7320 300d 0a53 5441 5420 | |
0x0310: 746f 7461 6c5f 6974 656d 7320 300d 0a53 | |
0x0320: 5441 5420 6576 6963 7469 6f6e 7320 300d | |
0x0330: 0a53 5441 5420 7265 636c 6169 6d65 6420 | |
0x0340: 300d 0a45 4e44 0d0a | |
2010-06-02 07:00:39.184765 IP 127.0.0.1.57157 > 127.0.0.1.11211: tcp 0 | |
0x0000: 4510 0034 48a1 4000 4006 f410 7f00 0001 | |
0x0010: 7f00 0001 df45 2bcb cb4d 1dca cb31 4b17 | |
0x0020: 8010 0219 bb11 0000 0101 080a 0008 5887 | |
0x0030: 0008 5887 | |
2010-06-02 07:00:43.096977 IP 127.0.0.1.57157 > 127.0.0.1.11211: tcp 0 | |
0x0000: 4510 0034 48a2 4000 4006 f40f 7f00 0001 | |
0x0010: 7f00 0001 df45 2bcb cb4d 1dca cb31 4b17 | |
0x0020: 8011 0219 b73e 0000 0101 080a 0008 5c59 | |
0x0030: 0008 5887 | |
2010-06-02 07:00:43.097280 IP 127.0.0.1.11211 > 127.0.0.1.57157: tcp 0 | |
0x0000: 4500 0034 1d61 4000 4006 1f61 7f00 0001 | |
0x0010: 7f00 0001 2bcb df45 cb31 4b17 cb4d 1dcb | |
0x0020: 8011 0200 b384 0000 0101 080a 0008 5c59 | |
0x0030: 0008 5c59 | |
2010-06-02 07:00:43.097303 IP 127.0.0.1.57157 > 127.0.0.1.11211: tcp 0 | |
0x0000: 4510 0034 48a3 4000 4006 f40e 7f00 0001 | |
0x0010: 7f00 0001 df45 2bcb cb4d 1dcb cb31 4b18 | |
0x0020: 8010 0219 b36b 0000 0101 080a 0008 5c59 | |
0x0030: 0008 5c59 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment