Memcached Getting Started

Source: Internet
Author: User
Tags memcached

Memcached is a free, open-source, high-performance, distributed-object caching system that can be used to hold a number of frequently accessed objects or data, and the stored data is like a huge hash table, which indicates that the key-value is saved in memory. The simple thing is to call the data into memory and then read it from memory, which greatly improves the reading speed.

Why would there be two names of Memcache and memcached?
Memcache is the name of the project, and memcached is the file name of the main program on its server side.

Memcached Features:

    • Simple protocol
      It is a text line-based protocol that allows access to data directly via Telnet on the memcached server.

    • Based on Libevent event processing
      Libevent is a library of C development, it will be the BSD system Kqueue,linux system Epoll and other event processing functions encapsulated into an interface, with the traditional select, improve performance

    • Built-in memory management method
      All the data is stored in memory, so access to data is faster than the hard disk, when the memory is full, will automatically delete the unused cache through the LRU algorithm, but does not consider the data disaster tolerance, restart the problem, all the data will be lost

    • Distributed
      Each memcached server is different from each other, independently accessing data and not sharing any information. The server does not have distributed functionality, and distributed deployment depends on the memcached client

Installation of Memcache
It is divided into two processes:

    1. Memcache Server-side installation
      is to install memcached on servers (typically Linux systems) for data storage

    2. Installation of the memcached client
      To use the functions provided by the server-side memcache in the program

Memcache Workflow:

First check whether the client's request is issued in memcached, if present, directly return the data, no longer do anything to the database, if the requested data is no longer memcached, it will first query the database, the data obtained from the database to the client, At the same time, the data is cached to memcached (this process requires a service-side program to implement); Update the data in the memcached every time the database is updated, ensuring consistency; When the allocated memcached memory space is exhausted, the LRU (Least Recently used, least recently used) policy plus expiry expiration policy, the failed data is replaced first, and then replaced with the most recently unused data.

Memcached's memory algorithm:

Memcached uses slab allocation mechanism to allocate and manage memory, it divides allocated memory into a certain length of memory block according to the predetermined size, then divides the same size memory block into groups, the data is stored, according to the key value size to criticize match slab size, Find the nearest slab storage, so there is space waste phenomenon.

Traditional memory management: Using the memory allocated through Maclloc to reclaim memory through free, this way is prone to memory fragmentation, and reduce the operating system memory management efficiency.

Cache Policy for memcached:

The memcached cache policy is the LRU (least recently used) plus the expiry expiration policy. When you store data in memcached memory, it is possible to specify its expiration time in cache, which defaults to permanent. When the memcached server runs out of allocated memory, the data for the invalidated data is first replaced, and then the data that is not used recently. In LRU, memcached is using a lazy expiration policy that does not monitor the expiration of the key/value being deposited, but instead looks at the timestamp of the record when the key value is obtained and checks to see if the key/value is out of space. This reduces the load on the server.

Distributed algorithms:

    1. Remainder algorithm
      The integer hash value of the key of the first ball, divided by the number of servers, determines the access server according to the remainder, this method is simple and efficient, but when the memcached server dynamically increases or decreases, almost all of the caches will fail.

    2. Hash algorithm
      First calculate the hash value of the memcached server, and distribute it to 0 to 2 of the 32-square circle, and finally start from the location of the data map to the clockwise lookup, save the data to the first server found, if more than 2 32 times, or cannot find the server, Save the data to the first memcached server, if you add a memcached server, the keys on the first server that only increase the counter-clockwise direction of the server on the circle will be affected.

memcached Installation
Install Libevent First

# Download Libevent installation package wget libevent-2.0.22-stable.tar.gz# Unzip the tar zxvf libevent-2.0.22-stable.tar.gz-c/usr/local/# into the Extract directory cd/usr/local/ libevent-2.0.22-stable# configuration compile, install./configure-prefix=/usr/libeventmakemake Install

Installing memcached

# download memcached installation package wget unzip the installation package to the specified directory tar zxvf Memcached-1.4.24.tar.gz-c/usr/local/# into the extracted directory cd/usr/local/memcached-1.4.24# configuration, compilation, installation, Note: You need to specify the Libevent installation path when configure./configure-with-libevent=/usr/libevent/-prefix=/usr/local/memcached Makemake Install

When the installation is complete, start memcached

/usr/local/bin/memcached-d-M 10-u root-l 12000-c 256-p/tmp/
Ps-ef|grep memcached # View memcached process
Kill cat /tmp/ # End memcached Process

The startup parameters are described below:

    • The-D option is to start a daemon,

    • -M is the amount of memory allocated to Memcache, in megabytes, 10MB,

    • -U is the user running memcache, this is root,

    • -L is the server IP address of the listener, if there are multiple addresses, this specifies the IP address of the server,

    • -P is the port that sets Memcache listening, which is set to 12000, preferably more than 1024 ports,

    • The-c option is the maximum number of concurrent connections to run, the default is 1024, where 256 is set, according to the server load,

    • -P is set to save memcache pid file, I here is saved in/tmp/, can also start multiple daemons, but the port can not be duplicated.

Test memcached

[Email protected] memcached]# telnet 11211Trying to Escape character is ' ^] '. Set key1 0 4testSTOREDget key1value key1 0 4testENDquitConnection closed by foreign host.

memcached installation is complete!

Memcached Getting Started

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: 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.