Build a Memcached cluster using Magent

Source: Internet
Author: User
Tags keep alive
Magent is used to build a Memcached cluster. Because there is no communication between the Memcached server and the server, and no data is copied and backed up, a single point of failure may occur when any server node fails, if you need to implement HA, you need to solve it in another way. Prevents spof through Magent cache proxy

Magent is used to build a Memcached cluster. Because there is no communication between the Memcached server and the server, and no data is copied and backed up, a single point of failure may occur when any server node fails, if you need to implement HA, you need to solve it in another way. Prevents spof through Magent cache proxy

MagentBuild MemcachedCluster



MemcachedCluster Introduction


Because the Memcached server does not communicate with the server and does not perform any data replication and backup, a single point of failure occurs when any server node fails. If HA is required, you need to solve the problem in another way.


The Magent cache proxy prevents spof. The cache proxy can also back up data, connect to the cache proxy server through the client, and connect the cache proxy server to the cache connection server, the cache proxy server can connect to multiple Memcached machines to synchronize data between each Memcached machine. If one of the cache servers goes down, the system can continue to work. If one of the Memcached servers goes down, data will not be lost and data integrity can be guaranteed.

Build MemcachedCluster


The Magent architecture solution has been described in the Magent introduction blog in detail in the previous article. The following example shows how Magent builds a Memcached cluster. in the production environment, a robust architecture scheme is designed based on the business characteristics.

650) this. width = 650; "title =" clip_image002 "style =" border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; border-left: 0px; padding-right: 0px; "alt =" clip_image002 "src =" http://www.68idc.cn/help/uploads/allimg/151111/1214395007-0.jpg "border =" 0 "height =" 175 "/>

Existing test machine: 192.168.11.51/52/68

Install libevent and memcached on the three test machines to start the memcached instance;

Then install magent on 51 and 52 to start the magent instance.

Install and start memcachedInstance


For detailed steps, see the previous blog "install and configure Memcached 1.4.22" to start the following instances:

/usr/local/bin/memcached -d -m 256 -u memcached -l 192.168.11.51 -p 11211 -c 1024 -P /var/run/memcached/memcached.pid/usr/local/bin/memcached -d -m 256 -u memcached -l 192.168.11.52 -p 11211 -c 1024 -P /var/run/memcached/memcached.pid/usr/local/bin/memcached -d -m 256 -u memcached -l 192.168.11.68 -p 11211 -c 1024 -P /var/run/memcached/memcached.pid

Install and start magentInstance


Example.


1. Install magent under/usr/local:

Cd/usr/local mkdir magent cd magent wget http://memagent.googlecode.com/files/magent-0.5.tar.gz (if not directly accessible, you can download the installation package first and then upload it to the server) tar zxvf magent-0.5.tar.gz


2. Modify the Configuration:
Add at the beginning of ketama. h file

#ifndef SSIZE_MAX    #define SSIZE_MAX 32767    #endif


ln -s /usr/lib64/libm.so /usr/lib64/libm.a/sbin/ldconfigsed -i "s#LIBS = -levent#LIBS = -levent -lm#g" Makefile    vi Makefile


Set

CFLAGS = -Wall -O2 -g

To:

CFLAGS = -lrt -Wall -O2 -g

Save


3. Compile:

make


Output the following information:

gcc -lrt -Wall -O2 -g -c -o magent.o magent.c    gcc -lrt -Wall -O2 -g -c -o ketama.o ketama.c    gcc -lrt -Wall -O2 -g -o magent magent.o ketama.o -levent –lm


4. view the Command help:

./magent –h


memcached agent v0.4 Build-Date: Apr 21 2015 09:21:10    Usage:    -h this message    -u uid    -g gid    -p port, default is 11211. (0 to disable tcp support)    -s ip:port, set memcached server ip and port    -b ip:port, set backup memcached server ip and port    -l ip, local bind ip address, default is 0.0.0.0    -n number, set max connections, default is 4096    -D don't go to background    -k use ketama key allocation algorithm    -f file, unix socket path to listen on. default is off    -i number, set max keep alive connections for one memcached server, default is 20    -v verbose


5. Start the magent instance

/usr/local/magent/magent -u root -n 4096 -l 192.168.11.51 -p 11200 -s 192.168.11.51:11211 -s 192.168.11.52:11211 -b 192.168.11.68:11211    /usr/local/magent/magent -u root -n 4096 -l 192.168.11.52 -p 11200 -s 192.168.11.51:11211 -s 192.168.11.52:11211 -b 192.168.11.68:11211

Test procedure


Log on to the magent on 51 and store key1 to key5:

[root@mongo01 ~]# telnet 192.168.11.51 11200Trying 192.168.11.51...Connected to 192.168.11.51.Escape character is '^]'.statsmemcached agent v0.4matrix 1 -> 192.168.11.51:11211, pool size 0matrix 2 -> 192.168.11.52:11211, pool size 0ENDset key1 0 0 11STOREDset key2 0 0 222STOREDset key3 0 0 3333STOREDset key4 0 0 44444STOREDset key5 0 0 555555STOREDquitConnection closed by foreign host.


Log on to memcached on 51 and get key2 and key4:

[root@mongo01 ~]# telnet 192.168.11.51 11211Trying 192.168.11.51...Connected to 192.168.11.51.Escape character is '^]'.get key1ENDget key2VALUE key2 0 222ENDget key3ENDget key4VALUE key4 0 44444ENDget key5ENDquitConnection closed by foreign host.


Log on to memcached on 52 and get key1, key3, and key5:

[root@mongo02 ~]# telnet 192.168.11.52 11211Trying 192.168.11.52...Connected to 192.168.11.52.Escape character is '^]'.get key1VALUE key1 0 11ENDget key2ENDget key3VALUE key3 0 3333ENDget key4ENDget key5VALUE key5 0 555555ENDquitConnection closed by foreign host.


Log on to memcached on 68 and get key1 to key5:

[root@szlnmp01 ~]# telnet 192.168.11.68 11211Trying 192.168.11.68...Connected to 192.168.11.68.Escape character is '^]'.get key1VALUE key1 0 11ENDget key2VALUE key2 0 222ENDget key3VALUE key3 0 3333ENDget key4VALUE key4 0 44444ENDget key5VALUE key5 0 555555ENDquitConnection closed by foreign host.


Stop the memcached process of 52 and get the key1 to key5 through the magent on 51:

kill `cat /var/run/memcached/memcached.pid`
[root@mongo01 magent]# telnet 192.168.11.51 11200Trying 192.168.11.51...Connected to 192.168.11.51.Escape character is '^]'.get key1VALUE key1 0 11ENDget key2VALUE key2 0 222ENDget key3VALUE key3 0 3333ENDget key4VALUE key4 0 44444ENDget key5VALUE key5 0 555555ENDquitConnection closed by foreign host.


Recover the memcached process of 52 and get only key2 and key4 through the magent on 51:

[root@mongo01 ~]# telnet 192.168.11.51 11200Trying 192.168.11.51...Connected to 192.168.11.51.Escape character is '^]'.get key1ENDget key2VALUE key2 0 222ENDget key3ENDget key4VALUE key4 0 44444ENDget key5ENDquitConnection closed by foreign host.


Through the above tests, we can draw a conclusion:

1. values stored in the magent connection pool will be stored in all memcached of the magent agent respectively.

2. If a memcached instance is down, the value can be obtained through the magent proxy.

3. if memcached is restored and restarted, the value obtained through the magent method is Null. This is because the value in memcache disappears with the memcache Service stopped (because it is in the memory ), however, the magent is assigned to a machine by hash calculation based on the key. After memcache is restarted, the value is also obtained from this machine. All obtained values are null.


Solution:

1. After each memcache crash fix, you can write a program to copy all the information of other memcache in the cluster to the memcache after the current crash fix.

2. Write the proxy by yourself. When the value obtained from a memcached service is Null, the proxy will return the value to other memcached instances.


Note:

The call method of magent is the same as that of memcached. The client can switch to magent mode without modifying the code.

Cache and DBSynchronization


It is safer to retrieve data from the Cache during query and operate the cache and DB simultaneously during add, updae, and delete operations.

Of course, you can also regularly synchronize cache and DB data. Different services should have different options.

Magent-0.6Summary of version-related errors


The following error occurs:

gcc -Wall -g -O2 -I/usr/local/include -m64 -c -o magent.o magent.c    magent.c: In function ‘writev_list’:    magent.c:729: error: ‘SSIZE_MAX’ undeclared (first use in this function)    magent.c:729: error: (Each undeclared identifier is reported only once    magent.c:729: error: for each function it appears in.)    make: *** [magent.o] Error 1


Solution:
Add at the beginning of ketama. h file

#ifndef SSIZE_MAX    #define SSIZE_MAX 32767    #endif


Make again


The following error occurs:

gcc -Wall -g -O2 -I/usr/local/include -m64 -c -o magent.o magent.c    gcc -Wall -g -O2 -I/usr/local/include -m64 -c -o ketama.o ketama.c    gcc -Wall -g -O2 -I/usr/local/include -m64 -o magent magent.o ketama.o     usr/lib64/libevent.a /usr/lib64/libm.a     gcc: /usr/lib64/libevent.a: No such file or directory    gcc: /usr/lib64/libm.a: No such file or directory    make: *** [magent] Error 1


Solution:

ln -s /usr/lib64/libm.so /usr/lib64/libm.a    vi Makefile


Find LIBS =/usr/lib64/libevent. a/usr/lib64/libm..
Modify the format as follows:
LIBS =/usr/ /Libevent. a/usr/lib64/libm.
For example: LIBS =/usr/lib/libevent. a/usr/lib64/libm.
Save


Make again


The following error occurs:

gcc -Wall -g -O2 -I/usr/local/include -m64 -o magent magent.o ketama.o /usr/lib/libevent.a /usr/lib64/libm.a     /usr/lib/libevent.a(event.o): In function `gettime':    /tmp/libevent-2.0.22-stable/event.c:370: undefined reference to `clock_gettime'    /usr/lib/libevent.a(event.o): In function `detect_monotonic':    /tmp/libevent-2.0.22-stable/event.c:340: undefined reference to `clock_gettime'    collect2: ld returned 1 exit status    make: *** [magent] Error 1


Solution:

vi Makefile

Set

CFLAGS = -Wall -g -O2 -I/usr/local/include $(M64)

To:

CFLAGS = -lrt -Wall -g -O2 -I/usr/local/include $(M64)

Save


Make again


Output:

gcc -lrt -Wall -g -O2 -I/usr/local/include -m64 -o magent magent.o ketama.o /usr/lib/libevent.a /usr/lib64/libm.a

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.