Repcached implements Memcached master-slave Replication

Source: Internet
Author: User
Repcached implements the Memcached master-slave replication function working principle repcached implements the memcached replication function. It is a single master single slave solution, but masterslave is read/write and can be synchronized with each other, if the master node breaks down and slave detects that the connection is disconnected, it will automatically listen and become the master node.

Repcached implements the Memcached master-slave replication function working principle repcached implements the memcached replication function. It is a single master single slave solution, but the master/slave can be read and written and can be synchronized with each other, if the master node breaks down and slave detects that the connection is disconnected, it will automatically listen and become the master node.

RepcachedImplement MemcachedMaster-slave Replication



Working Principle


Repcached implements the memcached replication function. It is a single master single slave solution, but the master/slave can be read and written and can be synchronized with each other, if the master node breaks down the slave and detects that the connection is disconnected, it will automatically listen and become the master node. In this case, the crashed master node can only be enabled as the slave node, and roles can be exchanged between them to maintain the replication function. In other words, the master node does not have the preemption function. If the slave fails, the master node will detect a disconnection and relisten will wait for the new slave to join.

Application scenarios


When memcached is used for session sharing or other services, there will be a single point of failure (spof) of memcached. If memcached is down, the entire system user cannot log on to the session ).

Based on this situation, use repcached for memcached master-slave redundancy.

Repcached


Http://sourceforge.net/projects/repcached/files/repcached/

RepcachedInstallation Method


Repcached has two installation methods:

1. patch version installation
First install the corresponding version of memcached, and then the corresponding version of The Repcached patch.

2. Integrated version installation
Install the integrated version of memcached directly.

Method 1: Install patches

1. Install Memcache. For more information about how to install Memcache, see blog:
Http://ultrasql.blog.51cto.com/9591438/1632179

2. Download the corresponding repcached version patch installation file:
Assume that the installed memcahced version is 1.2.8. download the latest patch for this version:

wget http://softlayer-dal.dl.sourceforge.net/project/repcached/repcached/2.2.1-1.2.8/repcached-2.2.1-1.2.8.patch.gz    gzip -cd ../repcached-2.2.1-1.2.8.patch.gz | patch -p1    ./configure --enable-replication    make && make install

Method 2: Integrated version installation

1. Install libevent:

cd /tmp    wget http://downloads.sourceforge.net/levent/libevent-2.0.22-stable.tar.gz    tar zxvf libevent-2.0.22-stable.tar.gz    cd libevent-2.0.22-stable    ./configure --prefix=/usr/local/lib    make && make install

2. Add the libevent library file to the dynamic library:

vi /etc/ld.so.conf


Add the following lines at the end:
/Usr/local/lib // The Directory of the libevent library to be added.
Reload dynamic lib Library

ldconfig


Note: If you do not have this step, you will be prompted to not see the library file of libevent when starting memcached.

3. test whether the libevent is successfully installed:

ls -al /usr/lib | grep libevent-

4. Create a startup account:

groupadd memcached    useradd -g memcached memcached

5. Create a PID process directory and modify the owner:

mkdir /var/run/memcached    chown -R memcached.memcached /var/run/memcached

6. Install and integrate the memcached-repcached package:

cd /tmp    wget http://softlayer-dal.dl.sourceforge.net/project/repcached/repcached/2.2.1-1.2.8/memcached-1.2.8-repcached-2.2.1.tar.gz    cp memcached-1.2.8-repcached-2.2.1.tar.gz /usr/local    cd /usr/local    tar zxvf memcached-1.2.8-repcached-2.2.1.tar.gz    mv memcached-1.2.8-repcached-2.2.1 memcached    cd memcached    ./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/lib --enable-replication --enable-64bit


Note:By default, a single memcached process supports only 2 GB of memory. If you need more memory, you need to enable 64-bit support and add parameters during compilation:
Enable-64bit


make && make install


A compilation error is prompted:

make all-recursive    make[1]: Entering directory `/usr/local/memcached'    Making all in doc    make[2]: Entering directory `/usr/local/memcached/doc'    make[2]: Nothing to be done for `all'.    make[2]: Leaving directory `/usr/local/memcached/doc'    make[2]: Entering directory `/usr/local/memcached'    gcc -DHAVE_CONFIG_H -I. -DNDEBUG -m64 -g -O2 -MT memcached-memcached.o -MD     MP -MF .deps/memcached-memcached.Tpo -c -o memcached-memcached.o `test -f     memcached.c' || echo './'`memcached.c    memcached.c: In function ‘add_iov’:    memcached.c:697: error: ‘IOV_MAX’ undeclared (first use in this function)    memcached.c:697: error: (Each undeclared identifier is reported only once    memcached.c:697: error: for each function it appears in.)    make[2]: *** [memcached-memcached.o] Error 1    make[2]: Leaving directory `/usr/local/memcached'    make[1]: *** [all-recursive] Error 1    make[1]: Leaving directory `/usr/local/memcached'    make: *** [all] Error 2


Solution:

vi memcached.c


Set the following lines

/* FreeBSD 4.x doesn't have IOV_MAX exposed. */    #ifndef IOV_MAX    #if defined(__FreeBSD__) || defined(__APPLE__)    # define IOV_MAX 1024    #endif    #endif


Change

/* FreeBSD 4.x doesn't have IOV_MAX exposed. */    #ifndef IOV_MAX    # define IOV_MAX 1024    #endif


Recompile and install:

make && make install


View help:

./bin/memcached -h


memcached 1.2.8    repcached 2.2.1    -p 
 
   TCP port number to listen on (default: 11211)    -U 
  
    UDP port number to listen on (default: 11211, 0 is off)    -s 
   
     unix socket path to listen on (disables network support)    -a 
    
      access mask for unix socket, in octal (default 0700)    -l 
     
       interface to listen on, default is INDRR_ANY    -d run as a daemon    -r maximize core file limit    -u 
      
        assume identity of 
       
         (only when run as root) -m 
        
          max memory to use for items in megabytes, default is 64 MB -M return error on memory exhausted (rather than removing items) -c 
         
           max simultaneous connections, default is 1024 -k lock down all paged memory. Note that there is a limit on how much memory you may lock. Trying to allocate more than that would fail, so be sure you set the limit correctly for the user you started the daemon with (not for -u 
          
            user; under sh this is done with 'ulimit -S -l NUM_KB'). -v verbose (print errors/warnings while in event loop) -vv very verbose (also print client commands/reponses) -h print this help and exit -i print memcached and libevent license -P 
           
             save PID in 
            
             , only used with -d option -f 
             
               chunk size growth factor, default 1.25 -n 
              
                minimum space allocated for key+value+flags, default 48 -R Maximum number of requests per event limits the number of requests process for a given con nection to prevent starvation. default 20 -b Set the backlog queue limit (default 1024) -x 
               
                 hostname or IP address of peer repcached -X 
                
                  TCP port number for replication. 
                 
                   (default: 11212)
                 
                
               
              
             
            
           
          
         
        
       
      
     
    
   
  
 


Modify the memcached directory owner:

cd ..    chown -R memcached.memcached memcached

Configure master-slave Replication



Parameter description:

-X sets the IP address from which to synchronize data.

-X indicates the data synchronization port.

Memcached uses the default service port 11211 and the default synchronization listening port 11212.

Start Master:

/usr/local/memcached/bin/memcached -d -m 100 -l 192.168.11.51 -p 11211 -u memcached -c 1024 -x 192.168.11.52 -X 11212 -P /var/run/memcached/memcached-rep.pid

View the listening port:

netstat -tupln | grep memcached
tcp 0 0 192.168.11.51:11211 0.0.0.0:* LISTEN 18634/memcachedtcp 0 0 192.168.11.51:11212 0.0.0.0:* LISTEN 18634/memcachedudp 0 0 192.168.11.51:11211 0.0.0.0:* 18634/memcached

Start Slave:

/usr/local/memcached/bin/memcached -d -m 100 -l 192.168.11.52 -p 11211 -u memcached -c 1024 -x 192.168.11.51 -X 11212 -P /var/run/memcached/memcached-rep.pid

Note:-x 192.168.11.51 is the IP address of the Master for synchronization.

View the listening port:

netstat -tupln | grep memcached
tcp 0 0 192.168.11.52:11211 0.0.0.0:* LISTEN 24262/memcachedudp 0 0 192.168.11.52:11211 0.0.0.0:* 24262/memcached

Verify Data Synchronization



On the MasterCreate data:

[root@test01 bin]# telnet 192.168.11.51 11211
Trying 192.168.11.51...Connected to 192.168.11.51.Escape character is '^]'.get key1ENDset key1 0 0 2aaSTOREDquitConnection closed by foreign host.

In SlaveGet data:

[root@test02 bin]# telnet 192.168.11.52 11211
Trying 192.168.11.52...Connected to 192.168.11.52.Escape character is '^]'.get key1ENDget key1VALUE key1 0 2aaENDquitConnection closed by foreign host.

In SlaveCreate data:

[root@test02 bin]# telnet 192.168.11.52 11211
Trying 192.168.11.52...Connected to 192.168.11.52.Escape character is '^]'.get key2ENDset key2 0 0 3bSTOREDget key2VALUE key2 0 3bENDquitConnection closed by foreign host.

On the MasterGet data:

[root@test01 bin]# telnet 192.168.11.51 11211
Trying 192.168.11.51...Connected to 192.168.11.51.Escape character is '^]'.get key2VALUE key2 0 3bENDquitConnection closed by foreign host.

MemcachedHigh Availability


When starting Master and Slave, do not add the-l parameter to specify the listening address. Otherwise, keepalived cannot listen to the VIP address.

Then, keepalived can be configured as high availability.

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.