Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Memory caching software memcached

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Chapter 1 memcached

Memory caching software (memory is faster than disk)

Divided into server and client

Server memcached

Client memcache

The reason for its birth.

Memcached was born in 2003.

Before web1.0 2005, enterprises provided content primarily.

Web2.0 2005-2012 enterprises only provide a platform where users participate in uploading and downloading content.

Goal:

Solve the problem of high concurrent access and reduce the pressure of traditional database mysql.

Do not pay attention to data reliability, only care about high concurrent read and write.

Differentiation development.

Enterprise application scenarios:

1. Cache at the front end of the database

Read data: the developer logically accesses memcached first. If memcached does not have the required data to access mysql.

Write data: if you write the database at the same time, write the database to memcached. Or copy it from mysyl to memcached while writing to mysql.

Linux operation and maintenance role, build memcached services, provide services (information used by developers)

2. Session, a shared session service at the backend of the cluster

Session tickets. If you have this ticket in the website, you can browse any interface.

Solutions to shared sessions:

1) nginx scheduling algorithm IP_hash (disadvantage: resulting in load imbalance)

2) session sharing through memcached.

3) cookies (placed on the user's browser side) has the advantage of high concurrency. Disadvantages: easy to tamper, not safe.

1.1 build memcached server

Set up memcached service

1. Libevent (epoll model, libevent as event notification mechanism)

Yum install libevent-devel-y

Rpm-qa libevent-devel

2. Download memcached

Yum install memcached-y

Rpm-qa memcached

3. Start the service and check

[root@db01] # memcached-m 16m-c 2048-uroot-p 11211-d

-m specifies memory-c how many access connections-u specifies the user-p specifies the port number-d as a daemon

[root@db01 ~] # netstat-lntp | grep memcache

Tcp 0 0 0.0.0.0 11211 0.0.0.015 * LISTEN 4026/memcached

Tcp 0 0: 11211: * LISTEN 4026/memcached

MySQL database management

Memcached management

Insert statement of MySQL

Set command for Memcached

Select statement of MySQL

Get command for Memcached

Delete statement of MySQL

Delete command for Memcached

Syntax of related commands for manipulating Memcached

The following is the basic syntax for commands related to manipulating Memcached:

Set key 0 0 0

\ r\ n

\ r\ n

\ r\ n

Command

Description

Command name

Set writes data anyway, which will overwrite the old data

Add adds data only if the corresponding data does not exist.

Repalce replaces data only when it exists

Append is followed by the following: appenddatablock?

Prepend is preceded by: prependdatablock

Cas changes by version number

Key

A normal string that requires less than 250 characters, excluding spaces and control characters

Flags

Numeric values used by the client to identify the data format, such as json, xml, compression, etc.

Exptime

The survival time is forever, less than 30 days, 60 seconds, 60 seconds, 24 days, 30 seconds.

More than 30 days is unixtime.

Bytes

The number of byte bytes, excluding\ r\ n. The string intercepted / fetched according to the length can be 0, that is, the empty string

Datablock

The line of text ends with\ r\ nand can of course contain\ r or\ n

Status

STORED/NOT_FOUND

Download the telnet and nc packages

Yum install telnet nc-y

Telnet mode

Save it and take it out and delete it.

[root@web03 ~] # telnet 127.0.0.1 11211

Trying 127.0.0.1...

Connected to 127.0.0.1.

Escape character is'^]'.

Set k1 0 0 6

Oldboy

STORED

Get k1

VALUE k1 0 6

Oldboy

END

Delete k1

DELETED

In nc mode

[root@web03 ~] # printf "set key008 006\ r\ noldboy\ r\ n" | nc 127.0.0.1 11211

STORED

View in printf mode

Printf "get key008\ r\ n" | nc127.0.0.1 11211

Delete

Printf "delete key008\ r\ n" | nc127.0.0.1 11211

Three kinds of write, read and delete

[root@web03 ~] # printf "set key008 00 6\ r\ noldboy\ r\ n" | nc 127.0.0.1 11211

STORED

[root@web03 ~] # printf "get key008\ r\ n" | nc 127.0.0.1 11211

VALUE key008 0 6

Oldboy

END

[root@web03 ~] # printf "delete key008\ r\ n" | nc 127.0.0.1 11211

DELETED

Memcached View help

Memcached-h

Parameters related to Memcached startup command

Command parameter

Description

Process and connection settings:

-d

Running services as a daemon (daemon)

-u

Specify the user running Memcached. If the current user is root, you need to use this parameter to specify the user

-l

Specify the IP address of the server that the Memcached process listens to. You can leave this parameter unset.

-p (lowercase)

Specifies the Memcached service listening TCP port number, which defaults to 11211

-P (uppercase)

Set the pod file ($$) to save Memcached, and save PID to the specified file

Memory related settin

-m

Specifies the maximum memory that the Memcached service can cache data, which defaults to 64MB

-M

Disable LRU when the Memcached service memory is insufficient. If the memory is full, an error will be reported.

-f

Chunk size growth due to the default of 1.25

-L

Enable large memory pages to reduce memory waste and improve performance

Concurrent connection Settings

-c

Maximum number of concurrent connections. Default is 1024.

-t

Number of threads. Default is 4. Because Memcached uses NIO, too many threads are of little use

-R

Maximum number of requests per event. Default is 20.

-C

Disable CAS (you can disable version counting and reduce overhead)

Debugging parameters

-v

Print less errors/warnings

-vv

Print a lot of debugging information and error output to the console, as well as client commands and responses

-vvv

Print a lot of debugging information and error output, as well as internal state transitions

1.2memcached client

The memcached client is installed on the server where php is located before the program can access memcached

Pull memcache-2.2.5.tgz packets to / home/oldboy/tools/

Then compile and install

Tar zxf memcache-2.2.5.tgz

Cd memcache-2.2.5

/ application/php/bin/phpize

. / configure-enable-memcache-with-php-config=/application/php/bin/php-config

Make

Make install

Cd.. /

[root@web02 tools] # ll/application/php-5.5.32/lib/php/extensions/no-debug-non-zts-20121212/

Total 256

-rwxr-xr-x 1 root root 258064 Jun 9 14:44 memcache.so

Echo'> / application/nginx/html/blog/a.php

[root@web02 php] # cat / application/nginx/html/blog/a.php

Check the configuration of mc through the page: blog.etiantian.org/a.php.

Configuration:

Vim / application/php/lib/php.ini

Extension_dir = "/ application/php-5.5.32/lib/php/extensions/no-debug-non-zts-20121212/"

Extension = memcache.so

Restart Php

Pkill php-fpm

/ application/php/sbin/php-fpm

Check the configuration of mc again through the page: blog.etiantian.org/a.php. Ctrl + F

Search for the content of the memcache page, and if there is a result, it is successful.

Manage memcachedweb side

Pull the memadmin-1.0.12.tar.gz package into / home/oldboy/tools/

Tar zxvfC memadmin-1.0.12.tar.gz / application/nginx/html/blog/

Perform designated decompression

Memcached status information details

Parameters.

Value

Description

Actual effect

Pid

28123

Memcache service process ID

View service information

Uptime

20063728

Number of seconds the service has been running

Time

1440577412

Service current Unix timestamp

Version

1.4.4

Memcache version

Libevent

1.4.13-stable

Libevent version

Pointer_size

sixty-four

Operating system pointer size

Rusage_user

645314.961214

Process accumulated user time

Analyze the occupation of CPU

Rusage_system

1134305.468357

Process cumulative system time

Cuur_connections

1361

Current number of connections

Analyze the number of connections

Total_connections

18299935

Total number of connections since Memcached ran

Connection_structures

12455

Number of connection structures assigned by Memcached

Reserved_fds

twenty

Number of FD used internally

Cmd_get

60424570825

Number of get command requests

Analyze the command rate

Get_hits

58105159197

Number of get command hits

Get_misses

2319411628

Number of misses of get command

Curr_items

24628253

Current number of objects

Analyze the number of objects LRU frequency

Total_items

854196259

Number of bytes currently occupied by storage

Evictions

7509

Number of objects released by LRU

1.2.1Slab memory management mechanism

Today's Memcached uses the Slab Allocation mechanism to allocate and manage memory, as follows:

1) allocate the large memory to several slab of 1MB in advance, and then populate the small object for each slab. This small object is called chunk, which avoids a lot of repeated initialization and cleaning, and reduces the burden of the memory manager.

The principle of Slab Allocation memory allocation is that according to the predetermined size, the memory allocated to Memcached services is pre-divided into specific length memory blocks (chunk), and then the same size memory blocks (chunk) are divided into groups (chunks slab class). These memory blocks will not be released and can be reused.

2) add data object storage instructions.

The Memcached server holds a list of free chunk in the slab, based on which it selects the chunk and caches the data in it. When data is stored, Memcached selects the slab with the most appropriate data size according to the size of the received data, and allocates a minimum memory block (chunk) that can store the data. For example, a data with 100 bytes will be allocated to a block of memory in the following 112 bytes, so that 12 bytes will be wasted and this part of the space can not be used, which is also a disadvantage of the SlabAllocator mechanism.

1.2.2 data storage mechanism

The data that needs to be cached is saved on the server side in the form of key/value key-value pairs.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report