How 1.memcached works (by maintaining the key value, querying value values, operating in memory)
How does memcached work?
The magic of memcached comes from a two-stage hash (two-stage hash). Memcached is like a huge hash table that stores a lot of <key,value> pairs. With key, you can store or query arbitrary data.
The client can store the data on more than one memcached. When querying the data, the client first calculates the hash value of the key (phase a hash), and then selects a node, the client sends the request to the selected node, and the Memcached node finds the real data (item) through an internal hashing algorithm (phase two hash).
As an example, suppose there are 3 clients 1, 2, 3, 3 memcached A, B, C:
Client 1 wants to store the data "Barbaz" with Key "Foo". Client 1 first refers to the node list (A, B, C) and calculates the hash value of key "foo", assuming memcached B is selected. Then, Client 1 directly connect to memcached B, and the data "Barbaz" is stored through key "foo". Client 2 uses the same clients library (which means phase one is the same as the hashing algorithm) and also has the same memcached list (A, B, C).
So, after the same hash calculation (phase one), Client 2 calculates the key "foo" on memcached B, and then it requests memcached B directly to get the data "Barbaz".
Various clients have different forms of data storage in memcached (Perl storable, PHP Serialize, Java hibernate, JSON, etc.). Some client implementations do not have the same hashing algorithm. However, the memcached server-side behavior is always consistent
2.memcached Installation
Install the base package
Yum-y Install GCC gcc-c++
Http://memcached.org/files/memcached-1.4.24.tar.gz
Http://nchc.dl.sourceforge.net/project/levent/libevent/libevent-2.0/libevent-2.0.22-stable.tar.gz
The first is to download memcached, the latest version is 1.1.12, directly from the official website can be downloaded to memcached-1.1.12.tar.gz. In addition, memcached used to libevent, I downloaded the libevent-1.1a.tar.gz
The next step is to unpack, compile, and install libevent-1.1a.tar.gz and memcached-1.1.12.tar.gz, respectively:
# TAR-XZF Libevent-1.1a.tar.gz
# CD LIBEVENT-1.1A
#./configure--PREFIX=/USR
# make
# make Install
# CD:
# TAR-XZF Memcached-1.1.12.tar.gz
# CD memcached-1.1.12
#./configure--PREFIX=/USR
# make
# make Install
After the installation is complete, memcached should be in/usr/bin/memcached.
3. How do I use the memcached-server side?
Run on the service side:
#./memcached-d-M 2048-l 192.168.1.90-p 11211-u Root
#-L listener address, memcache No authentication function, strictly prohibited to use in the absence of protection
# #-D runs as a daemon
#-m Specifies the allocated memory size, in units m
#-P Listening Port
#-U Run user
#-C Maximum number of concurrent connections
# #-P PID File storage location
# #-F Growth factor
This will start a process that consumes 2G of memory and open port 11211 for receiving requests. Because 32-bit systems can handle only 4G of memory, 2-3 processes are run on 32-bit servers that use PAE larger than 4G of memory and are monitored on different ports.
# # Start successfully, listen to all addresses, including IPv6, this is not safe, preferably-l to specify the listening IP
4. Test Memcached
memcached Common commands and instructions for use
One, storage command
Format of the stored command:
<command name> <key> <flags> <exptime> <bytes>
<data block>
The parameters are described as follows:
<command name>set/add/replace
<key> Find keywords
<flags> clients use it to store extra information about key-value pairs
<exptime> Survival time of this data, 0 means forever
<bytes> number of bytes stored
<data block> stored data block (can be directly understood as value in the key-value structure)
[[Email protected] ~]# telnet 127.0.0.1 11211 # login Memcached
Trying 127.0.0.1 ...
Connected to 127.0.0.1.
Escape character is ' ^] '.
Set ID 0 0 5 # Save a value
12345
STORED
Get ID # Take value
VALUE ID 0 5
12345
END
Replace ID 0 0 5 # Update value
88888
STORED
Get ID # to see if the update
VALUE ID 0 5
88888
END
Quit
Connection closed by foreign host.
[[Email protected] ~]# telnet 127.0.0.1 11211
Trying 127.0.0.1 ...
Connected to 127.0.0.1.
Escape character is ' ^] '.
Get ID # re-login and save
VALUE ID 0 5
88888
END
Quit
Connection closed by foreign host.
[[Email protected] ~]# telnet 127.0.0.1 11211
Trying 127.0.0.1 ...
Connected to 127.0.0.1.
Escape character is ' ^] '.
Get ID
VALUE ID 0 5
88888
END
Delete ID # Remove value
DELETED
Get ID
END
Quit
Connection closed by foreign host.
Other:
The difference between sticky session and non-sticky session:
Sticky session refers to the Apache server will be a user's request to a node in the Tomcat cluster, the node will be responsible for saving the user's session, if the node hangs, then the user's session will disappear.
Non-sticky session: In this way, the load balancer will be based on the situation of the node, the user's request is distributed, that is, will copy multiple sessions distributed to multiple nodes, so that if one of the nodes in the session hangs, the other can continue to work, That is, as long as a node is not hung, the user's information will not disappear.
Ii. Introduction to the memcached strategy
Msm:memcachedsession Manager is a highly available Tomcat session support program that can support sticky sessions or non-sticky sessions, where I use a non-sticky session, which is multiple The session replication between Tomcat, of course, except that MSM is only a component, you can also choose other components, such as MEMCACHEDDB, Membase.
Memcached Installation and Testing