Install and use memcached

Source: Internet
Author: User

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 up an applicationProgramArchitecture:

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 viewSource code.

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

Telnet local host 11211

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
SetCommand to add a new key-value pair to the cache. If the key already exists, the previous value is replaced.

Note the following interactions:SetCommand:

Set userid 0 0 512345 stored

If you useSetThe 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 isUseridThe 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,AddCommand 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.

UseAddStandard interaction of commands:

Set userid 0 0 512345 storedadd userid 0 0 555555not_storedadd companyid 0 0 3564 stored

Replace
Only when the key already exists,ReplaceCommand to replace the key in the cache. If no key exists in the cache, you will receiveNot_storedResponse.

UseReplaceStandard interaction of commands:

Replace accountid 0 0 567890not_storedset accountid 0 0 567890 storedreplace accountid 0 0 555555 stored

The last two basic commands are:GetAndDelete. These commands are quite easy to understand and use similar syntax, as shown below:

Command <key>

Next, let's look at the application of these commands.

Get
GetThe command is used to retrieve values related to the previously added key-value pair. You will useGetPerform most search operations.

UseGetTypical interaction of commands:

Set userid 0 0 512345 storedget useridvalue userid 0 512345 endget bobend

As you can see,GetThe command is quite simple. You use a key to callGetIf 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.DeleteCommand to delete any existing values in memcached. You will use a key to callDeleteIf the key exists in the cache, the value is deleted. If it does not exist, returnNot_foundMessage.

UseDeleteCommand client server interaction:

Set userid 0 0 598765 storeddelete bobnot_founddelete useriddeletedget useridend

Advanced memcached client commands

The two advanced commands that can be used in memcached are:GetsAndCAS.GetsAndCASThe 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
GetsCommand functions are similar to basicGetCommand. The difference between the two commands is,GetsThe returned information is slightly more: the 64-bit integer value is very similar to the "version" identifier of the name/value pair.

UseGetsCommand client server interaction:

Set userid 0 0 512345 storedget useridvalue userid 0 512345 endgets useridvalue userid 0 5412345end

ConsiderationsGetAndGetsDifferences between commands.GetsThe command returns an additional value-in this example, integer value 4 is used to identify the name/value pair. If you execute anotherSetCommand, thenGetsThe 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 533333 storedgets useridvalue userid 0 5533333end

You can seeGetsIs 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 usesSetSyntax similar to the command, but includes an additional value:GetsThe returned additional value.

Note the following:CASCommand Interaction:

Set userid 0 0 555555 storedgets useridvalue userid 0 5655555 endcas userid 0 0 5633333 stored

As you can see, I use an additional integer value of 6 to callGetsCommand, 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 indicatorCASCommand

Set userid 0 0 555555 storedgets useridvalue userid 0 5855555 endcas userid 0 0 5633333 exists

Note: I have not usedGetsAndCASCommand to return the exists value to indicate failure. In essenceGetsAndCASCommand 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 areStatsAndFlush_allCommand.

Stats
StatsThe command function is called to dump the current statistics of the connected memcached instance. In the following example, runStatsCommand displays information about the current memcached instance:

Statsstat PID 63 stat uptime 101758 stat time 1248643186 stat version 1.4.11stat pointer_size 32 stat rusage_user 1.177192 stat rusage_system 2.365370 stat curr_items 2 stat limit 8 stat bytes 119 stat curr_connections 6 stat limit 7 stat connection_structures 7 stat limit _get 12 stat performance_set 12 stat get_hits 12 stat get_misses 0 stat evictions 0 stat bytes_read 471 stat bytes_written 535 stat limit_maxbytes 67108864 stat 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 someSetCommand and run it againStatsCommand, pay attention to the changes.

Flush_all
Flush_allIs 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_allCan provide a lot of use. Below is a usageFlush_allExample:

Set userid 0 0 555555 storedget 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.StatsCommand 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 executionGetTimes and missedGetThe percentage of times. To determine this percentage, run it againStatsCommand, as shown in listing 8:

Listing 8. Calculating cache hit rate

Statsstat PID 6825 stat uptime 540692 stat time 1249252262 stat version 1.2.6stat pointer_size 32 stat rusage_user 0.056003 stat rusage_system 0.180011 stat curr_items 595 stat limit 961 stat bytes 4587415 stat curr_connections 3 stat limit 22 stat connection_structures 4 stat limit _get 2688 stat performance_set 961 stat get_hits 1908 stat get_misses 780 stat evictions 0 stat bytes_read 5770762 stat bytes_written 7421373 stat limit_maxbytes 536870912 stat 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)

 

 

 

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.