How to integrate memcached into your environment?
Before starting to install and use using memcached, we need to know how to integrate memcached into your environment. Although memcached can be used anywhere, I found that memcached often has the maximum effect when several regular queries need to be executed at the database layer. I often set up a series of memcached instances between the database and the application server, and adopt a simple mode to read and write these servers. Figure 1 helps you understand how to set the application architecture:
Figure 1. architecture of the sample application using memcached
The architecture is quite easy to understand. I have created a web layer, including some Apache instances. The next layer is the application itself. This layer is usually run 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 using this configuration, you must use a slightly different method to perform database read and write operations.
Read
The order in which I perform read operations is to obtain requests from the web layer (a database query is required) and check the query results stored in the cache. If I find the value, return it. If not found, execute the query and store the results in the cache, and then return the results to the web layer.
Write
When writing data to a database, you first need to perform the database write operation, and then set any previously cached results affected by this write operation to invalid. This process helps prevent data inconsistency between the cache and the database.
Install memcached
Memcached supports some operating systems, including Linux®Windows®, Mac OS, and Solaris. In this article, I will detail how to build and install memcached through source files. The main reason for using this method is that I can view the source code when encountering problems.
Libevent
Libevent is the only prerequisite for installing memcached. It is the asynchronous event notification library on which memcached depends. You can find the source file about libevent on monkey.org. Next, find the source file of the latest version. For this article, we use stable version 1.4.11. After obtaining the archive file, decompress it to a convenient location, and then execute the command in Listing 1:
Listing 1. Generating and installing libevent
cd libevent-1.4.11-stable/./configuremakemake install |
Memcached
Obtain the memcached source file from danga interactive and select the latest distribution version. At the time of writing this article, the latest version is 1.4.0. Decompress tar.gz to a convenient location and execute the command in Listing 2:
Listing 2. Generating and installing memcached
cd memcached-1.4.0/./configuremakemake install |
After completing these steps, you should install a memcached working copy and use it. Let's give a brief introduction and then use it.
Use memcached
To start using memcached, you must first start the memcached server and then connect to it using the Telnet client.
To start memcached, run the following command in listing 3:
Listing 3. Starting memcached
./memcached -d -m 2048 -l 10.0.0.40 -p 11211 |
This will start memcached (-d
) To allocate 2 GB of memory (-m 2048
), And specify the listener localhost, that is, 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 built-in Telnet clients, but if you are using a Windows-based operating system, you need to download a third-party client. Putty is recommended.
After the Telnet client is installed, run the following command in Listing 4:
Listing 4. Connecting to memcached
If everything is normal, you should receive a telnet response, which indicatesConnected to localhost (already connected to localhost). If this response is not received, the previous steps should be returned and the source files of libevent and memcached have been successfully generated.
You have now logged on to the memcached server. Later, you will be able to communicate with memcached through a series of simple commands. Nine memcached client commands can be divided into three types:
Basic
Advanced
Management
Basic memcached client commands
You will use five basic memcached commands to perform the simplest operations. These commands and operations include:
set
add
replace
get
delete
The first three commands are standard modification commands used to operate key-value pairs stored in memcached. They are both very easy to use and all use the syntax shown in listing 5:
Listing 5. Modifying command syntax
command <key> <flags> <expiration time> <bytes><value> |
Table 1 defines the parameters and usage of the memcached command.
Table 1. modify command parameters of memcached
Parameters |
Usage |
Key |
Key is used to find the cache value. |
Flags |
It can include an integer parameter of a key-value pair. The client uses it to store additional information about the key-value pair. |
Expiration time |
The duration of saving the key-value pair in the cache (in seconds, 0 indicates permanent) |
Bytes |
Byte points stored in the cache |
Value |
Stored value (always in the second row) |
Now let's take a look at the actual use of these commands.
Set
set
Command to add a new key-value pair to the cache. If the key already exists, the previous value is replaced.
Note the following interactions:set
Command:
set userId 0 0 512345STORED |
If you useset
The command correctly sets the key-value pair, and the server uses the wordStored. In this example, a key-value pair is added to the cache, whose key isuserId
The value is12345
. And set the expiration time to 0, which will notify memcached that you want to store this value in the cache until you delete it.
Add
If no key exists in the cache,add
Command to add a key-value pair to the cache. If a key already exists in the cache, the previous values remain the same and you will get a responseNot_stored.
Useadd
Standard interaction of commands:
set userId 0 0 512345STOREDadd userId 0 0 555555NOT_STOREDadd companyId 0 0 3564STORED |
Replace
Only when the key already exists,replace
Command to replace the key in the cache. If no key exists in the cache, you will receiveNot_storedResponse.
Usereplace
Standard interaction of commands:
replace accountId 0 0 567890NOT_STOREDset accountId 0 0 567890STOREDreplace accountId 0 0 555555STORED |
The last two basic commands are:get
Anddelete
. These commands are quite easy to understand and use similar syntax, as shown below:
Next, let's look at the application of these commands.
Get
get
The command is used to retrieve values related to the previously added key-value pair. You will useget
Perform most search operations.
Useget
Typical interaction of commands:
set userId 0 0 512345STOREDget userIdVALUE userId 0 512345ENDget bobEND |
As you can see,get
The command is quite simple. You use a key to callget
If the key exists in the cache, the corresponding value is returned. If no content exists, NO content is returned.
Delete
The last basic command isdelete
.delete
Command to delete any existing values in memcached. You will use a key to calldelete
If the key exists in the cache, the value is deleted. If it does not exist, returnNot_foundMessage.
Usedelete
Command client server interaction:
set userId 0 0 598765STOREDdelete bobNOT_FOUNDdelete userIdDELETEDget userIdEND |
Advanced memcached client commands
The two advanced commands that can be used in memcached are:gets
Andcas
.gets
Andcas
The command must be used in combination. You will use these two commands to ensure that the existing name/value pairs are not set as new values (if the value has been updated ). Let's look at these commands separately.
Gets
gets
Command functions are similar to basicget
Command. The difference between the two commands is,gets
The returned information is slightly more: the 64-bit integer value is very similar to the "version" identifier of the name/value pair.
Usegets
Command client server interaction:
set userId 0 0 512345STOREDget userIdVALUE userId 0 512345ENDgets userIdVALUE userId 0 5 412345END |
Considerationsget
Andgets
Differences between commands.gets
The command returns an additional value-in this example, integer value 4 is used to identify the name/value pair. If you execute anotherset
Command, thengets
The returned additional value is changed to indicate that the name/value pair has been updated. Listing 6 shows an example:
Listing 6. Set updated version indicator
set userId 0 0 533333STOREDgets userIdVALUE userId 0 5 533333END |
You can seegets
Is the returned value? It has been updated to 5. This value is changed every time you modify the name/value pair.
CAS
cas
(Check and set) is a very convenient memcached command, used to set the value of the name/value pair (if this name/value pair is executed on your lastgets
). It usesset
Syntax similar to the command, but includes an additional value:gets
The returned additional value.
Note the following:cas
Command Interaction:
set userId 0 0 555555STOREDgets userIdVALUE userId 0 5 655555ENDcas userId 0 0 5 633333STORED |
As you can see, I use an additional integer value of 6 to callgets
Command, and the operation runs in a very sequential order. Now let's take a look at a series of commands in listing 7:
Listing 7. Using the old version of the indicatorcas
Command
set userId 0 0 555555STOREDgets userIdVALUE userId 0 5 855555ENDcas userId 0 0 5 633333EXISTS |
Note: I have not usedgets
Andcas
Command to return the exists value to indicate failure. In essencegets
Andcas
Command to prevent you from using the name/value pair that has been updated since the last read.
Cache Management commands
The last two memcached commands are used to monitor and clear memcached instances. They arestats
Andflush_all
Command.
Stats
stats
The command function is called to dump the current statistics of the connected memcached instance. In the following example, runstats
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 7STAT connection_structures 7STAT cmd_get 12STAT cmd_set 12STAT get_hits 12STAT get_misses 0STAT evictions 0STAT bytes_read 471STAT bytes_written 535STAT limit_maxbytes 67108864STAT threads 4END |
Most outputs here are easy to understand. I will explain the meanings of these values in detail when discussing cache performance later. For the moment, let's take a look at the output and then use the new key to run someset
Command and run it againstats
Command, pay attention to the changes.
Flush_all
flush_all
Is the last command to be introduced. This simplest command is only used to clear all name/value pairs in the cache. If you need to reset the cache to a clean stateflush_all
Can provide a lot of use. Below is a usageflush_all
Example:
set userId 0 0 555555STOREDget userIdVALUE userId 0 555555ENDflush_allOKget userIdEND |
Cache Performance
At the end of this article, I will discuss how to use the advanced memcached command to determine the cache performance.stats
Command for Cache Optimization. The two most important statistics are et_hits and get_misses. These two values indicate the number of times (get_hits) of finding the name/value pair and the number of times (get_misses) of not finding the name/value pair ).
With these values, we can determine the cache utilization. When starting the cache for the first time, we can see that get_misses will naturally increase, but after a certain amount of use, these get_misses values should gradually become stable-This indicates that the cache is mainly used for common read operations. If you see that get_misses continues to increase rapidly and get_hits gradually becomes stable, you need to determine what the cached content is. You may have cached the error content.
Another way to determine the cache efficiency is to view the cache hit rate (hit ratio ). Cache hit rate indicates executionget
Times and missedget
The percentage of times. To determine this percentage, run it againstats
Command, as shown in listing 8:
Listing 8. Calculating cache hit rate
statsSTAT pid 6825STAT uptime 540692STAT time 1249252262STAT version 1.2.6STAT pointer_size 32STAT rusage_user 0.056003STAT rusage_system 0.180011STAT curr_items 595STAT total_items 961STAT bytes 4587415STAT curr_connections 3STAT total_connections 22STAT connection_structures 4STAT cmd_get 2688STAT cmd_set 961STAT get_hits 1908STAT get_misses 780STAT evictions 0STAT bytes_read 5770762STAT bytes_written 7421373STAT limit_maxbytes 536870912STAT threads 1END |
Now, divide the value of get_hits by pai_gets. In this example, your hit rate is about 71%. Ideally, you may want a higher percentage-the higher the ratio, the better. View statistics and measure them from time to determine the efficiency of cache policies.
Common commands are as follows:
Start/end
Memcached-D-M 10-u root-l 192.168.0.122-P 11200-C 256-P/tmp/memcached. PID
-D option is to start a daemon,
-M indicates the amount of memory allocated to memcache. The unit is mb. The value is 10 MB.
-U is the user who runs memcache. Here it is root.
-L is the IP address of the listener server. If there are multiple IP addresses, the IP address 192.168.0.122 of the server is specified here.
-P is the port used to set memcache listening. Here, 12000 is set, preferably over 1024.
-The C option is the maximum number of concurrent connections running. The default value is 1024. Here, 256 is set based on the load of your server.
-P is the PID file for saving memcache.
Kill 'cat/tmp/memcached. Pi'
Get running status
Echo stats | NC 192.168.1.123 11200
Watch "Echo stats | NC 192.168.1.123 11200" (Real-time status)
Install and use memcached