Chapter 2 memcached applications and compatible programs

Source: Internet
Author: User
Tags rehash intel pentium
5.1 Mixi Case Study
Mixi uses memcached in the early stage of service provision. With the sharp increase in website traffic, simply add
Because slave cannot meet the requirements, memcached is introduced. In addition, we have also verified the scalability,
It proves that the speed and stability of memcached can meet the needs. Now, memcached has become a very important service in Mixi.
.
Figure 5.1: Current System Components
Server Configuration and quantity
Mixi uses many servers, such as database servers, application servers, image servers, and reverse proxy servers. Single
Nearly 200 servers are running for a single memcached instance. The typical configuration of the memcached server is as follows:
• CPU: Intel Pentium 4 2.8 GHz
30
Idv2.com Chapter 2 Application and compatibility of memcached Program
• Memory: 4 GB
• Hard Disk: 146 GB SCSI
• Operating System: Linux (x86_64)
These servers were previously used for database servers. As CPU performance increases and memory prices fall, we actively integrate data
Database servers and application servers are replaced by servers with more powerful performance and more memory. In this way, the overall use of Mixi can be restrained.
The number of servers increases dramatically, reducing management costs. Because the memcached server occupies almost no CPU, It will be replaced.
Is used as the memcached server.
Memcached Process
Each memcached server starts only one memcached process. The memory allocated to memcached is 3 GB, And the startup Parameter
As follows:
/Usr/bin/memcached P
11211 u
Nobody m
3000 C
30720
Because the x86_64 operating system is used, 2 GB or more memory can be allocated. In a 32-bit operating system, each process can only
2 GB memory available. We have also considered starting multiple processes with less than 2 GB of memory allocated, but the TCP
The number of connections increases exponentially and the management becomes more complex. Therefore, Mixi uses a 64-bit operating system.
In addition, although the server memory is 4 GB, only 3 GB is allocated because the memory allocation volume exceeds this value, which may cause
Memory switching (SWAp ). In Chapter 2, I explained the memory storage "Slab allocator" of memcached,
When memcached is started, the specified memory allocation volume is the amount of data that memcached uses to save, excluding the "Slab allocator" book
Memory Used by the user and the management space set to save the data. Therefore, the actual memory allocation of the memcached process is required.
This is larger than the specified capacity.
Most of the data stored by Mixi in memcached is relatively small. In this way, the process size is much larger than the specified capacity. Because
Here, we repeatedly change the memory allocation volume for verification, and confirm that the size of 3 GB will not cause swap, which is the current number of applications
Value.
Memcached usage and client
Currently, Mixi uses about 200 memcached servers as a pool. The capacity of each server is
3 GB, then there will be a huge memory database of nearly GB. The client library uses
Cache: memcached: fast, interactive with the server. Of course, distributed caching Algorithm Used in Chapter 4th
Consistent hashing algorithm.
• Cache: memcached: fast search.
Cpan.org
The usage of memcached on the application layer is determined and implemented by the engineers who develop the application. However, in order to prevent the wheel from re-running
Creates and prevents the cache: memcached: fast lessons from happening again. We provide the cache: memcached: Fast wrap
Module.
Maintain the connection through cache: memcached: fast
Cache: memcached, the connection (file handle) with memcached is stored in the cache: memcached package
Class variable. In environments such as mod_perl and FastCGI, the variables in the package are not restarted at any time as CGI does,
Always maintained in the process. The result is that the connection to memcached is not closed, which reduces the overhead of TCP connection establishment.
It can also prevent TCP port resource depletion caused by repeated TCP connections and disconnections in a short period of time.
However, cache: memcached: fast does not have this function, so you need to put the cache: memcached: Fast object out of the module
Keep in class variables to ensure persistent connections.
31
Idv2.com Chapter 2 memcached applications and compatible programs
Package gihyo: memcached;
Use strict;
Use warnings;
Use cache: memcached: fast;
My @ server_list = QW/192.168.1.1: 11211 192.168.1.1: 11211 /;
My $ fast; # used to keep objects
Sub new {
My $ self = bless {}, shift;
If (! $ Fast ){
$ Fast = cache: memcached: Fast>
New ({servers => "@ server_list });
}
$ Self> {_
Fast} = $ fast;
Return $ self;
}
Sub get {
My $ self = shift;
$ Self> {_
Fast}>
Get (@_);
}
In the preceding example, the cache: memcached: Fast object is saved to the class variable $ fast.
Public Data Processing and rehash
The cache data shared by all users, such as the news on the Mixi homepage, and other data, will occupy many pages and access
The number of questions is also large. Under such conditions, access is easily concentrated on a memcached server. The access set itself is not
Yes, but once the server in the access set fails, memcached cannot be connected, which will cause a huge problem.
# 4th chapter memcached distributed algorithms | as mentioned in outline, cache: memcached has the rehash function, which means it cannot
When the server that stores the data is connected, the hash value is calculated again to connect to other servers.
However, cache: memcached: fast does not have this function. However, it can not connect to the server for a short period of time
The function of connecting to the server.
My $ fast = cache: memcached: Fast>
New ({
Max_failures => 3,
Failure_timeout => 1
});
If the max_failures connection fails within the second of failure_timeout, The memcached server is no longer connected. We
Is set to more than 3 times in 1 second.
In addition, Mixi also sets a naming rule for the key names of cache data shared by all users. Data that meets the naming rules is automatically saved
Among the multiple memcached servers, only one server is selected for obtaining. After creating this function library, you can make memcached
Server faults do not have any other impact.
5.2 memcached Application Experience
So far, we have introduced the internal structure and function library of memcached. Next we will introduce some other application experience.
32
Idv2.com Chapter 2 memcached applications and compatible programs
Start with daemontools
Generally, memcached runs quite stably, but Mixi uses the latest version 1.2.5 several times.
The memcached process is dead. The architecture ensures that services are not affected even if several memcached faults exist.
The memcached process can run normally if memcached is restarted.
Memcached process and automatic start method. Therefore, daemontools is used.
Daemontools is a set of UNIX service management tools developed by Qmail author djb. The program named supervise can be used
Restart or stop a service.
• Daemontools
The installation of daemontools is not described here. Mixi uses the following run script to start memcached.
#! /Bin/sh
If [F
/Etc/sysconfig/memcached]; then
./Etc/sysconfig/memcached
Fi
Exec 2> & 1
Exec/usr/bin/memcached P
$ Port u
$ User m
$ Cachesize C
$ Maxconn
$ Options
Monitoring
Mixi uses an open-source monitoring software named "Nagios" to monitor memcached.
• Nagios: Home
You can develop plug-ins in Nagios to monitor get and add actions of memcached in detail. However, Mixi only supports
Run the stats command to check the running status of memcached.
Define command {
Command_name check_memcached
Command_line $ user1 $/check_tcp H
$ Hostaddress $ P
11211 TB
5 e
S
'Stats "R" nquit "R" N' E
'Uptime' m
Crit
}
In addition, Mixi converts the results of the stats Directory into graphs through rrdtool for performance monitoring and daily memory usage.
Generate Reports and share them with developers through emails.
Memcached Performance
As mentioned earlier, memcached has excellent performance. Let's take a look at the actual cases of Mixi. The chart here is Server
The memcached server with the most concentrated access.
33
Idv2.com Chapter 2 memcached applications and compatible programs
Figure 5.2 request count
Figure 5.3: Traffic
Figure 5.4 TCP connections
Requests, traffic, and TCP connections are listed from top to bottom. The maximum number of requests is 400 QPS, and the traffic reaches Mbps.
The number of connections exceeds 10000. This server has no special hardware, that is, the general memcached server introduced at the beginning.
The CPU usage is as follows:
34
Idv2.com Chapter 2 memcached applications and compatible programs
Figure 5.5 CPU utilization
It can be seen that there are still idle parts. Therefore, memcached has high performance and can be used as a web application developer with peace of mind.
The place where temporary data or cached data is stored.
5.3 compatible with Applications
The implementation and protocols of memcached are very simple, so there are many implementations compatible with memcached. Some powerful extensions
You can write the memory data of memcached to the disk to realize data persistence and redundancy. Chapter 1 has introduced
The storage layer of memcached will become extensible (Pluggable) and will gradually support these features.
Here we will introduce several memcached-compatible applications.
Repcached
Provides replication patches for memcached.
Flared
Stored in qdbm. Asynchronous replication and fail over are also implemented.
Memcachedb
Stored in berkleydb. Message Queue is also implemented.
Tokyo tyrant
Store Data to Tokyo cabinet. Not only is it compatible with the memcached protocol, but it can also be accessed through HTTP.
Tokyo tyrant Case Study
Mixi uses Tokyo tyrant, which is compatible with the preceding applications. Tokyo tyrant is developed by pinglin for the Tokyo cabinet dBm.
Network interface. It has its own protocol, but also has memcached compatibility protocol, and can also exchange data through HTTP.
Although Tokyo cabinet is an implementation of writing data to a disk, the speed is quite fast.
Mixi does not use Tokyo tyrant as the cache server, but uses it as the DBMS that saves the combination of key-value pairs. Main
Used as a database that stores the last access time of a user. It is related to almost all Mixi services.
Data must be updated, so the load is quite high. MySQL is very cumbersome to process, and memcached is used separately to save data and
Data may be lost, so Tokyo tyrant is introduced. But you do not need to re-develop the client, you only need to use it intact

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.