当前位置:网站首页>Memcached comprehensive analysis – 5 Memcached applications and compatible programs

Memcached comprehensive analysis – 5 Memcached applications and compatible programs

2022-06-24 21:27:00 Hello,C++!

Publication day :2008/7/30
author : Nagano Yaguang (Masahiro Nagano)
Link to the original text :http://gihyo.jp/dev/feature/01/memcached/0005

I am a Mixi Nagano .memcached It's finally over . To The last time until , We introduced and memcached Directly related topics , This time I will introduce some mixi Cases and Practical topics , And introduce some related to memcached Compatible programs .

mixi A case study

mixi Used in the early stages of service delivery memcached. With the rapid increase of website visits , Just add... To the database slave Can't meet the needs , So it introduces memcached. Besides , We also tested it in terms of increasing scalability , Proved memcached The speed and stability can meet the needs . Now? ,memcached Has become mixi A very important part of the service .

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-kEiwlAAQ-1656034632805)(https://pic001.cnblogs.com/img/dudu/200809/2008092817263955.png)]

chart 1 Today's system components

Server configuration and number

mixi A lot of servers are used , Such as database server 、 application server 、 Picture server 、 Reverse proxy server, etc . only memcached It's close to 200 One server is running . memcached The typical configuration of the server is as follows :

  • CPU:Intel Pentium 4 2.8GHz
  • Memory :4GB
  • Hard disk :146GB SCSI
  • operating system :Linux(x86_64)

These servers have been used for database servers, etc . With CPU Performance improvement 、 The price of memory is down , We actively put the database server 、 The application server has been replaced with a more powerful one 、 Servers with more memory . such , Can inhibit mixi A dramatic increase in the number of servers used as a whole , Reduce management costs . because memcached The server takes up almost nothing CPU, Use the replaced server as memcached The server .

memcached process

Each station memcached The server starts only one memcached process . Assigned to memcached The memory of is 3GB, The startup parameters are as follows :

/usr/bin/memcached -p 11211 -u nobody -m 3000 -c 30720

Due to the use of x86_64 Operating system of , So we can distribute 2GB Above memory .32 Bit operating system , Each process can only use at most 2GB Memory . I've also considered starting multiple assignments 2GB The following memory processes , But on such a server TCP The number of connections will multiply , Management has also become complicated , therefore mixi It's unified 64 Bit operating system .

in addition , Although the memory of the server is 4GB, But they only allocated 3GB, Because the amount of memory allocated exceeds this value , It could lead to memory swapping (swap). Serial The first 2 Time in Maesaka explained memcached Memory storage “slab allocator”, It was said at the time that ,memcached Startup time The specified memory allocation is memcached The amount used to hold data , Do not include “slab allocator” Memory occupied by itself 、 And the management space set up to save data . therefore ,memcached The actual memory allocation of a process is larger than The specified capacity should be large , This should be noted .

mixi Save in memcached Most of the data in are small . such , The size of the process is larger than The specified capacity is much larger . therefore , We repeatedly change the amount of memory allocation to verify , Confirmed. 3GB The size of will not trigger swap, So that's what we're using now .

memcached Usage and client

Now? ,mixi Our service will 200 Right and left memcached Server as a pool Use . The capacity of each server is 3GB, Then all of us have nearly 600GB Huge in memory database . The client library uses the Cache::Memcached::Fast, Interact with the server . Of course , The distributed algorithm for caching uses The first 4 Time Introduced Consistent Hashing Algorithm .

On the application layer memcached How to use it is up to the engineer who develops the application to decide and implement it . however , To prevent the wheels from being rebuilt 、 prevent Cache::Memcached::Fast The lessons of the past are happening again , We provide Cache::Memcached::Fast Of wrap Module and use .

adopt Cache::Memcached::Fast Keep connected

Cache::Memcached Under the circumstances , And memcached The connection of ( File handle ) Save in Cache::Memcached Class variables in the package . stay mod_perl and FastCGI Etc , Variables in packages don't look like CGI So restart at any time , It's about keeping... In the process . The result is not to disconnect from memcached The connection of , Less TCP The overhead of establishing a connection , At the same time, it can also prevent repeated operations in a short period of time TCP Connect 、 To break off Caused by TCP Port resources are exhausted .

however ,Cache::Memcached::Fast No such function , So you need to be outside the module take Cache::Memcached::Fast Objects remain in class variables , To ensure a persistent connection .

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 hold 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 example above ,Cache::Memcached::Fast Object to a class variable $fast in .

Processing and management of public data rehash

Such as mixi The news on the home page of this cache data shared by all users 、 Setting information and other data , It takes up a lot of pages , The number of visits is also very large . Under such conditions , It's easy to focus on one station memcached Server . Access concentration itself is not a problem , But once the server in the access center fails, it will lead to memcached Unable to connect , There's going to be a huge problem .

Serial The first 4 Time I mentioned ,Cache::Memcached Have rehash function , That is, in the case of unable to connect to the server where the data is stored , It will be calculated again hash value , Connect to other servers .

however ,Cache::Memcached::Fast No such function . however , It can be used when the connection to the server fails , No longer connect to the server in a short time .

my $fast = Cache::Memcached::Fast->new({
    max_failures     => 3,
    failure_timeout  => 1
});

stay failure_timeout Within seconds max_failures Last connection failed , No longer connect to the memcached The server . Our setup is 1 Second 3 More than once .

Besides ,mixi It also sets naming rules for the key names of the cached data shared by all users , Data conforming to the naming rules will be automatically saved to multiple computers memcached Server , Only one server is selected from the list . After creating the library , You can make memcached Server failure No more impact .

memcached Application experience

So far, I've introduced memcached Internal structure and function library , Next, introduce some other application experience .

adopt daemontools start-up

Usually memcached It's quite stable , but mixi The latest version in use now 1.2.5 It happened several times memcached The process died . The architecture ensures that even if there are several memcached fault It will not affect the service , But for the memcached Process dead server , Just restart memcached, It can run normally , So surveillance is used memcached Process and start automatically . So I used daemontools.

daemontools yes qmail The author of DJB Developed UNIX Service management toolset , It's called supervise Can be used for service startup 、 Stop service restart, etc .

Not covered here daemontools Installed. .mixi Using 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

monitor

mixi Used the name “nagios” Open source monitoring software to monitor memcached.

stay nagios Plug ins can be easily developed in , Can be monitored in detail memcached Of get、add Wait for the action . however mixi Only through stats Order to confirm memcached Operating state .

define command {
command_name                   check_memcached
command_line                   $USER1$/check_tcp -H $HOSTADDRESS$ -p 11211 -t 5 -E -s 'stats\r\nquit\r\n' -e 'uptime' -M crit
}

Besides ,mixi take stats The result of the directory is through rrdtool Turn it into graphics , Performance monitoring , And report the daily memory usage , Share with developers via email .

memcached Performance of

It has been introduced in the series ,memcached It's very good . Let's see mixi The actual case of . The chart described here is the most centralized access used by the service memcached The server .

img

chart 2 Number of requests

img

chart 3 Traffic

img

chart 4 TCP The number of connections

From top to bottom is the number of requests 、 Traffic and TCP The number of connections . The maximum number of requests is 15000qps, The flow reaches 400Mbps, At this time, the number of connections has exceeded 10000 individual . The server has no special hardware , It's just the ordinary memcached The server . At this time CPU The utilization rate is :

img

chart 5 CPU utilization

so , There are still idle Part of . therefore ,memcached Very high performance , It can be used as Web Where application developers can safely store temporary data or cache data .

Compatible applications

memcached The implementation and protocol are very simple , So there's a lot to do with memcached Compatible implementation . Some powerful extensions can make memcached Write the memory data to disk , Achieve data persistence and redundancy . Serial The first 3 Time Introduced , After the memcached Our storage layer will become scalable (pluggable), Gradually support these functions .

Here are a few examples of memcached Compatible applications .

  • repcached

    by memcached Provide copies (replication) Functional patch.

  • Flared

    Store in QDBM. Asynchronous replication and fail over And so on .

  • memcachedb

    Store in BerkleyDB. It's also achieved message queue.

  • Tokyo Tyrant

    Store data to Tokyo Cabinet. Not only with memcached Protocol compatible , Also can pass the HTTP Visit .

Tokyo Tyrant Case study

mixi Using... In the compatible application above Tokyo Tyrant.Tokyo Tyrant It was developed by Pinglin Tokyo Cabinet DBM Network interface . It has its own protocol , But it also has memcached Compatibility protocol , It can also be done through HTTP Data exchange .Tokyo Cabinet Although it's an implementation of writing data to disk , But it's pretty fast .

mixi Did not Tokyo Tyrant As a caching server , Instead, use it as a way to save the combination of key value pairs DBMS To use . It is mainly used as a database to store the last access time of users . It's related to almost all mixi It's all about service , Every time a user visits a page, the data is updated , So the load is quite high .MySQL It's very cumbersome , Use alone memcached Saving data may lead to data loss , So we introduced Tokyo Tyrant. But there's no need to redevelop the client , Just use it as it is Cache::Memcached::Fast that will do , It's also one of the advantages . About Tokyo Tyrant Details of , Please refer to our company's development blog.

summary

As of this time ,“memcached Comprehensive analysis ” The series is over . We introduced memcached The basis of 、 internal structure 、 Distributed algorithm and application . After reading, if you can understand memcached Have an interest in , It's our pleasure . About mixi The system of 、 Application information , Please refer to our Development blog. Thank you for reading .

For ease of reading , Now pack the original translation results into PDF file , download .

Series navigation :

memcached A complete analysis of –1. memcached The basis of

memcached Comprehensive analysis –2. understand memcached Memory storage

memcached Comprehensive analysis –3. memcached The deletion mechanism and development direction of

memcached Comprehensive analysis –4. memcached Distributed algorithm

memcached Comprehensive analysis –5. memcached Applications and compatible programs

原网站

版权声明
本文为[Hello,C++!]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206241432585744.html