Reference: http://www.czhphp.com/archives/252
How do I incorporate memcached into your environment?
Before you begin to install and use the using memcached, we need to know how to incorporate memcached into your environment. Although memcached can be used anywhere, I find that memcached often works best when several recurring queries need to be executed in the database layer. I often set up a series of memcached instances between the database and the application server and use a simple pattern to read and write to these servers. Figure 1 can help you understand how to set up your application architecture:
Figure 1. Sample application Architecture using memcached
The architecture is fairly easy to understand. I've built a Web tier that includes some Apache instances. The next layer is the application itself. This layer typically runs on Apache Tomcat or other open source application servers. The next layer is where the memcached instance is configured-that is, between the application server and the database server. When you use this configuration, you need to perform read and write operations on the database in a slightly different way.
Read
The order in which I perform the read operation is to fetch the request from the Web tier (which requires a database query) and examine the results of the query that was previously stored in the cache. If I find the value I want, I'll return it. If it is not found, the query is executed and the results are stored in the cache before the results are returned to the Web tier.
Write
When you write data to a database, you first need to perform a database write operation, and then set any previously cached results that are affected by this write operation to be invalid. This process helps prevent data inconsistencies between the cache and the database.
Installing memcached
Memcached supports some operating systems, including Linux, Windows, Mac OS, and Solaris. In this article, I'll explain in detail how to build and install memcached from a source file. The main reason for this is that I can view the source code when I'm having problems.
Libevent
Libevent is the only prerequisite for installing memcached. It is the asynchronous event notification library that memcached relies on. You can find the source file on monkey.org on the libevent. Next, find the latest version of the source file. For this article, we use the stable version 1.4.11. Once the archive has been obtained, unzip it to a convenient location and then execute the command in Listing 1:
Listing 1. Build and install Libevent
CD Libevent-1.4.11-stable/./configuremakemake Install |
Memcached
Obtain the memcached source file from Danga Interactive , and still select the latest distribution version. At the time of writing this article, the latest version is 1.4.0. Unzip the tar.gz to a convenient location and execute the command in Listing 2:
Listing 2. Build and install memcached
CD Memcached-1.4.0/./configuremakemake Install |
After you complete these steps, you should have a memcached working copy installed, and you can use it. Let's take a brief introduction and then use it.
Using memcached
To get started with memcached, you first need to start the memcached server and then connect to it using a Telnet client.
To start memcached, execute the command in Listing 3:
Listing 3. Start memcached
./memcached-d-M 2048-l 10.0.0.40-p 11211 |
This launches memcached () as a daemon -d
, allocates 2GB of memory ( -m 2048
), and specifies a listening localhost, port 11211. You can modify these values as needed, but the above settings are sufficient to complete the exercises in this article. Next, you need to connect to memcached. You will use a simple Telnet client to connect to the memcached server.
Most operating systems provide a built-in Telnet client, but if you are using a Windows-based operating system, you need to download a third-party client. I recommend using PuTTy.
After the Telnet client is installed, execute the command in Listing 4:
Listing 4. Connect to Memcached
If everything is OK, you should get a telnet response that will indicate Connected to localhost (already connected to localhost). If you do not get this response, you should return to the previous steps and ensure that the source files for both libevent and memcached have been successfully generated.
You are now logged on to the memcached server. Thereafter, you will be able to communicate with memcached through a series of simple commands. 9 memcached Client commands can be divided into three categories:
Basic memcached Client Commands
You will perform the simplest operation with five basic memcached commands. These commands and actions include:
set
add
replace
get
delete
The first three commands are standard modification commands for manipulating key-value pairs stored in memcached. They are both very easy to use and are all using the syntax shown in Listing 5:
Listing 5. Modify command syntax
Command <key> <flags> <expiration time> <bytes><value> |
Table 1 defines the parameters and usages of the memcached Modify command.
Table 1. Memcached Modifying command parameters
Parameters |
usage |
Key |
Key to find cached values |
Flags |
You can include integer parameters for key-value pairs that the client uses to store additional information about key-value pairs |
Expiration time |
The length of time (in seconds, 0 for forever) that a key-value pair is saved in the cache |
bytes |
The byte points stored in the cache |
Value |
Stored values (always on the second row) |
Now, let's take a look at the actual use of these commands.
Set
set
The command is used to add a new key-value pair to the cache. If the key already exists, the previous value will be replaced.
Note the following interaction, which uses the set
command:
Set userId 0 0 512345STORED |
If set
a key-value pair is correctly set using a command, the server responds with the word STORED . This example adds a key-value pair to the cache with a key userId
value of 12345
. and set the expiration time to 0, which notifies memcached that you want to store this value in the cache until you delete it.
Add
The add
command adds a key-value pair to the cache only if the key does not exist in the cache. If the key already exists in the cache, the previous value will remain the same, and you will get a response of not_stored.
The following are add
standard interactions using commands:
Set userId 0 0 512345STOREDadd userid 0 0 555555not_storedadd companyid 0 0 3564STORED |
Replace
replace
the command replaces the key in the cache only if the key already exists. If the key does not exist in the cache, you will receive a not_stored response from the memcached server.
The following are replace
standard interactions using commands:
Replace accountId 0 0 567890not_storedset accountId 0 0 567890STOREDreplace accountId 0 0 555555STORED |
The last two basic commands are get
and delete
. These commands are fairly easy to understand and use a similar syntax, as follows:
Next look at the application of these commands.
Get
get
The command is used to retrieve the value associated with the previously added key-value pair. You will use to get
perform most of the retrieval operations.
The following are get
typical interactions using commands:
Set userid 0 0 512345STOREDget useridvalue userId 0 512345ENDget bobend |
As you can see, the get
command is quite simple. You use a key to invoke get
, and if the key exists in the cache, the corresponding value is returned. If it does not exist, nothing is returned.
Delete
The last basic command is delete
. delete
the command is used to delete any existing values in the memcached. You will use a key call delete
and if the key exists in the cache, delete the value. If it does not exist, a not_found message is returned.
Here is delete
the client server interaction using the command:
Set userId 0 0 598765STOREDdelete bobnot_founddelete useriddeletedget useridend |
Advanced memcached Client Commands
The two advanced commands that can be used in memcached are the gets
and cas
. gets
and cas
commands need to be used in combination. You will use these two commands to ensure that the existing name/value pairs are not set to the new value if the value has been updated. Let's take a look at these orders separately.
gets
gets
The function of the command is similar to the basic get
command. The difference between the two commands is that the gets
information returned is slightly more: The 64-bit integer value is much like the "version" identifier of a name/value pair.
Here is gets
the client server interaction using the command:
Consider get
gets
the differences between the commands. The gets
command returns an extra value-in this case, the integer value 4, which identifies the name/value pair. If another command is executed on this name/value pair set
, the gets
extra value returned will change to indicate that the name/value pair has been updated. Listing 6 shows an example:
Listing 6. Set Update Version indicator
Did you see gets
the value returned? It has been updated to 5. Each time you modify a name/value pair, the value changes.
CAs
cas
(check and set) is a very handy memcached command that sets the value of a name/value pair if the name/value pair has not been updated since you last executed it gets
. It uses set
syntax similar to the command, but includes an extra value: gets
the extra value returned.
Note the following cas
interactions using commands:
As you can see, I use the extra integer value 6来 to invoke gets
the command, and the operation runs in a very sequential order. Now let's take a look at the series of commands in Listing 7:
Listing 7. Commands that use the old version designator cas
Note that I did not use gets
the most recently returned integer value, and the cas
command returned a EXISTS value in order to fail. In essence, simultaneous use gets
and cas
commands prevent you from using name/value pairs that have been updated since the last read.
Cache Management Commands
The last two memcached commands are used to monitor and clean memcached instances. They are stats
and flush_all
commands.
Stats
stats
The command functions as its name: the current statistics of the memcached instance to which the dump is connected. In the following example, the Execute stats
command displays information about the current memcached instance:
Statsstat PID 63STAT Uptime 101758STAT time 1248643186STAT version 1.4.11STAT pointer_size 32STAT rusage_user 1.177192STAT Rusage_system 2.365370STAT curr_items 2STAT total_items 8STAT bytes 119STAT curr_connections 6STAT total_connections 7STA T connection_structures 7STAT cmd_get 12STAT cmd_set 12STAT get_hits 12STAT get_misses 0STAT evictions 0STAT bytes_read 47 1STAT bytes_written 535STAT limit_maxbytes 67108864STAT Threads 4END |
Most of the output here is very easy to understand. Later, when we discuss caching performance, I'll explain in detail what these values mean. For now, let's take a look at the output and then use the new key to run some set
commands and run stats
the command again, noticing what's changed.
Flush_all
flush_all
is the last command to be introduced. This simplest command is only used to clean up all name/value pairs in the cache. If you need to reset the cache to a clean state, it flush_all
can be a great use. Here is an flush_all
example of use:
Set userid 0 0 555555STOREDget useridvalue userId 0 555555endflush_allokget useridend |
Cache Performance
At the end of this article, I'll discuss how to use the Advanced memcached command to determine the performance of the cache. stats
commands are used to tune the use of the cache. The two most important statistics to note are et_hits and get_misses. These two values indicate the number of times the name/value pair was found (get_hits) and the number of name/value pairs that were not found (get_misses).
By combining these values, we can determine how the cache is utilized. When you start the cache for the first time, you can see that get_misses will naturally increase, but after a certain amount of usage, these get_misses values should gradually stabilize-this means that the cache is primarily used for common read operations. If you see get_misses continue to increase rapidly, and get_hits gradually become smooth, then you need to determine what the cached content is. You may have cached the wrong content.
Another way to determine cache efficiency is to view the cache hit ratio. The cache Hit ratio represents the get
number of executions and the get
percentage of missed times. To determine this percentage, you need to run the stats
command again, as shown in Listing 8:
Listing 8. Calculate cache Hit Ratio
Statsstat PID 6825STAT Uptime 540692STAT time 1249252262STAT version 1.2.6STAT pointer_size 32STAT rusage_user 0.056003STA T rusage_system 0.180011STAT curr_items 595STAT total_items 961STAT bytes 4587415STAT curr_connections 3STAT Total_connec tions 22STAT connection_structures 4STAT cmd_get 2688STAT cmd_set 961STAT get_hits 1908STAT get_misses 780STAT Evictions 0 STAT bytes_read 5770762STAT bytes_written 7421373STAT limit_maxbytes 536870912STAT Threads 1END |
Now, divide the value of get_hits by Cmd_gets. In this case, your hit rate is about 71%. Ideally, you might want to get a higher percentage-the higher the ratio, the better. Viewing statistics and measuring them from time to times can be a good way to determine the efficiency of your caching strategy.
Commands are often as follows:
Start/End
Memcached-d-M 10-u root-l 192.168.0.122-p 11200-c 256-p/tmp/memcached.pid
The-D option is to start a daemon,
-M is the amount of memory allocated to Memcache, in megabytes, 10MB
-U is the user running memcache, this is root
-L is the server IP address of the listener, if there are multiple addresses, this specifies the IP address of the server 192.168.0.122
-P is the port that sets Memcache listening, which is set to 12000, preferably more than 1024 ports
The-c option is the maximum number of concurrent connections, the default is 1024, where 256 is set according to the load on your server
-P is a PID file that is set to save Memcache
Kill ' Cat/tmp/memcached.pid '
Get Run status
Echo Stats | NC 192.168.1.123 11200
Watch "Echo Stats | NC 192.168.1.123 11200 "(real-time status)
Reference: http://www.czhphp.com/archives/252
Blog has moved, please visit the following address: http://www.czhphp.com
Installing and using memcached