Major Website Optimization-memcache technology, optimization-memcache technology-PHP Tutorial

Source: Internet
Author: User
Tags crc32 delete key zts
Large website optimization-memcache technology, optimization-memcache technology. Large website optimization-memcache technology, optimization-memcache technology large website optimization-memcache technology memory + cache memory cache memcache introduction memcache is a distributed high-speed cache system large website optimization-memcache technology, optimization-memcache technology
Large website optimization-memcache technology

Memory + cache memory cache

Introduction to memcache

Memcache is a setDistributedHigh speedCache systemDeveloped by Brad Fitzpatrick of LiveJournal, it is currently used by many websites to speed up website access, especially for large websites that require frequent access to databases. This is an open-source software, which is released with BSD license authorization. [Retrieved from Baidu Encyclopedia]

Official website: http://memcached.org/

Distributed: multiple Memcache servers are used to manage the data architecture.

Cache system: caches the data queried by the user into the memory, so that it can be obtained directly from the memory next time. Reduces disk IO overhead.

What is nosql? (SQL Relational Database Service)

A: MySQL is called a relational database. it is mainly characterized by a two-dimensional table structure (rows and columns in a table). the table and table are in a relationship. (oracle (Java), db2,Sqlserver)

Non-relational data: the database that does not use SQL statements as queries (the system that stores data), and does not have the concept of a two-dimensional table in a strict sense. Its data structure is a huge hash table (key-value)

Hash table advantages: The time complexity is 0 (1): As data increases, the query time does not change by an order of magnitude (1 s.

Disadvantages of the hash table: the hash collision of different keys corresponds to the same value

Key1 = MD5 (SQL)

Asion

Key2

12

Key3

Shenzhen

Key4

Iphone

  • Selinux is not disabled

    Make selinux effective immediately

  • 2.1 prepare the environment

    In Linux, tools such as gcc, g-c ++, make (makefile), cmake, autoconfig (configure), and libtool are required.

    In LinuxNetworkingUse the following command

    # Yum install-YGcc make cmake autoconfig libtool

    -Y does not require confirmation interaction

    2.2 compile and install memcached

    Memcached depends onLibeventLibrary, so you need to first install, respectively to their respective official website to download stable version

    Libevetnt official website: http://libevent.org/

    Memcache official website: http://memcached.org/

    Compile and install libevent, compile and install memcached, and specify the installation path of libevent when installing memcached.

    Procedure:

    Upload

  • Download the libevent, decompress it, and install it (generally, the software installation in Linux is in the/usr/local/src directory, and the installed software is usually in/usr/local/NAME)

    #./Configure -- prefix =/usr/local/libevent & make install

    B. install memcache, decompress, compile, and install memcache

    #./Configure -- prefix =/usr/local/memcached -- with-libevent =/usr/local/libevent & make install

    2.3 memcached startup

    #/Usr/local/memcached/bin/memcached-m 64-p 11211-u nobody-vv

    Note:: Memcached is successfully started, but the information is output to the console.

    If you want to enable memcached as a service in the background, you only need to add the-d option (daemon background)

    #/Usr/local/memcached/bin/memcached-m 64-p 11211-u nobody-d

    How can I check whether the server is started normally?

    # Ps axu | grep memcached

    To view the parameter information, use memcached-h to view the help information:

    Operate memcache

    The communication between the memcached client and the server is very simple. The text-based protocol, similar to the http protocol, can be directly used for interaction through Telnet.

    Use Telnet (quit)

    # Telent server IP 11211

  • Windows

    Use ctrl +] to enable Telnet Echo after connection

  • Linux

    2. basic commands:

    Learn about the add, delete, modify, and query commands of memcache:

    Add key flag expire length

    Key: Name

    Flag: 1 memcache is saved as a string

    Expire: Expiration Time, memcache time

    Length: data length (B)

    ※Add

    # Add name 1 0 2 # add a key to the memcache server with a name of 2 bytes in length and valid for a long time

    How to understand expire

    Set the cache validity period in three formats

  • Set the number of seconds. it starts with the setting and expires after n seconds.
  • Timestamp, which expires after the specified timestamp
  • It is set to 0 and does not expire automatically (note that it is not permanently valid. A. during memcache installation, a maximum validity period is specified. the default value is 30 days (source code). B. it may be squeezed out in less than 30 days)

    Minimum recently used principle

    ※Delete

    # Delete key

    ※Replace

    # Replace key flag expire length

    ※Get

    # Get key

    ※If set contains replace, add

    # Set key flag expire length

    Name exists:

    Age does not exist:

    ※Incr increase

    # Incr age NUMBER

    ※Decr reduction

    # Decr age NUMBER

    ※Stats collects memcache server information.

    # Stats

    ※Flush_all clears all data.

    # Flush_all

    Use php to operate memcache
  • Windows
  • Download the extension dll file to (http://downloads.php.net/pierre/)
  • Put the file in the directory corresponding to extension_dir of php.
  • Modify the php. ini file, add extension = php_memcache.dll, and introduce the file
  • Restart Apache

  • Linux (how to enable an extension for php in Linux, let's talk about the general solution)

    Beta: Test version: Generally, there are some minor bugs, but you need to use them to find out. if there are any problems, you can give official feedback. Then repair

    Alpha: internal beta version: the version used during internal development. Generally, this version has many bugs. However, this version often has new features. (This is usually done by a new company) alpha has some compensation.

    Stable: stable version: it has no bugs and can run stably.

    Notes for using ftp:

  • Whether the service vsftpd status is enabled normally in vsftpd
  • Generally, only common users can connect to ftp (root cannot connect to ftp) to obtain sudo
  • The size and space of your home directory are insufficient (the file is successfully uploaded, but the displayed size is 0)
  • Whether the firewall is disabled (iptables-F is disabled) (iptables-L)

    # Cls

    # Cd

    # Vim. bashrc

    Linux Download Mode

    #: Exit wq in lowercase x

    Linux in editing mode

    # Z Capital

    #/Usr/local/php/bin/phpize absolute path phpize

    D. use the configure file generated above to collect system information without specifying the installation path

    #./Configure -- with-php-config =/usr/local/php/bin/php-config tell it how to find the php configuration file

    E. execute compilation and installation

    # Make & make install

    Note: You can view the structure after the preceding command is executed.

    # Ls/usr/local/php/lib/php/extend/no-debug-non-zts-20090626/

    Note: In Linux, how does one view the location of the php. ini configuration file?

    Solution: phpinfo ();

    Note: In Linux, you must back up the configuration file before modifying it.

    Php. ini-backup-2016-1-12

    Note: what is. so?

    . So is a shared object in Linux. it is a. dll file in Windows.

    Note: In Linux, you can disable a service.

    # Pkill-9 httpd

    # Ps aux | grep httpd

    # Ps uax | grep httpd

    Add the address generated above to the php. ini configuration file, as shown below:

    Extension_dir =/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/

    Extension = memcacahe. so

    Create a test. php file to test whether php has a memcache module.

    Operate memcache in php, save and get the value

    Memcached memory management and deletion mechanism

    Note: the maximum value of Memcache can only be 1 MB.

    Note: memory fragmentation always exists, but which method can minimize memory fragmentation.

    1. what is memory fragmentation?

    When using this memory cache system, due to constant application and release, some small memory fragments will be formed and cannot be used. this phenomenon is called memory fragmentation. This small red block is the space that the operating system cannot use.

  • How can this problem be solved?

    Memcache uses slab allocator to manage (The size of each slab class is 1 MB.)

    The smallest unit is chunk (small block): the data warehouse.

    Multiple small units form a chunks: composed of multiple small blocks (all small blocks have the same size)

    The size of each slab class is 1 MB.

  • How do I select a proper memcache size?

    Note:If the slab of 122Bytes is full, there is a Bytes data. Where can I store it?

    A: Certainly there will be no 144, but there will still be a 122 chunk, which uses the LRU algorithm for data storage.

  • Memory waste caused by fixed chunk, 22B

  • Factor optimization

    When memcached is started, slab class will be organized according to a certain size. you can use-f to specify

    The default value is 1.25. the ratio between adjacent chunks is the increase factor. You can adjust the cache factor based on your website's business.

    Because each business is different, the minimum chunk required is different. This parameter makes our system more adaptable to our own business, because data can be set by ourselves.

  • Memcache inert deletion

    Memcached does not monitor whether the record expires, but checks the timestamp of the record during get to check whether the record expires. This behavior is called lazy (inert) expiration. Therefore, memcached does not consume CPU time on expired monitoring.

    For example, if a set (name, asion, 0, 3600) expires after 3600 seconds, it will not be automatically deleted after it expires. only when the get query is performed, the system checks whether it expires, if it expires, it is deleted.

    For example, # add name 1 8 2 after 8 s, is it invalid or does not exist? Stats analysis

    Analysis: it indicates that you are invalid, not nonexistent. only when you get it the next time will memcache check whether it expires and delete it when it expires.

  • Memcache LRU algorithm

    Memcached will give priority to the space of records that have timed out, but even so, there will be insufficient space to append new records. in this case, we need to use the Least Recently Used (LRU) mechanism to allocate space.

    As the name suggests, this is a mechanism to delete records that are "least recently used. Therefore, when the memory space of memcached is insufficient (when the new space cannot be obtained from the slab class), the memcached will be searched from the recently unused record and empty.

    To the new records. From the perspective of caching, this model is ideal.

    When the data space in the mecache (64 MB by default) is full, can you continue to store the data?

    A: It can be stored. to delete expired data, if none of them have expired, delete the most inactive data and free up space to add data later.

    For example, if a Bytes data record is generated when the data is full, how can this problem be solved? (Even if it is permanently valid, it will be kicked)

    Analysis: Memory managementLRU algorithm, FIFO algorithm

  • Some memcache parameters Ctrl + C

    Note: If you enter ctrl + s in vim, you can exit with ctrl + q.

    -P listening port

    -L connected IP address. the default value is local

    -D start: start the memcached service.

    -D restart: restart the memcached service.

    -D stop | shutdown the running memcached service

    -D install the memcached service

    -D uninstall memcached service

    -U runs as the identity (only valid when running as root)

    -MB maximum memory usage, in MB. The default value is 64 MB.

    Note: If the system is 32-bit, the maximum limit is 2 GB. if the system is 64-bit, there is no limit.

    -An error is returned when M memory is used up, instead of deleting items.

    -C: maximum number of simultaneous connections. the default value is 1024.

    -F block size growth factor, default value: 1.25

    -N: Minimum allocation space. the default value of key + value + flags is 48.

    -H Show Help

    -V output warning and error message

    -Vv: print the client's request and return information

    -I print the copyright information of memcached and libevent.

    Use cases

    Save: 1. File 2. mysql

    Q: What if there are too many session files in a folder and retrieval slows down?

    Answer: hierarchical processing

    Memcache is used for storage, and Memcache is used for storage in a distributed manner (multiple Memcache servers are used for storage)

    Use memcache to save the session

    1. modify the php. ini file. the configuration information is as follows:

    Session. save_handler = memcache # indicates that the session is saved using memcache.

    Session. save_path = "tcp: // 127.0.0.1: 11211" # specify the address and port of the memcache server

  • Test

    1. enable session and save session

  • Get session from Memcache

    Advanced features distributed memcache configuration

    What is distributed?

    Because the service capability of a single memcache is limited, multiple memcache instances can be used to provide the cache function. This architecture is called the distributed slow storage (cluster) system of memcache.

    How to implement it?

    You can understand how to distribute data to multiple Memcache servers.

    Before the client implements distributed data storage, the data is saved to the memcache server based on certain algorithms, obtain data from the corresponding memcache server based on the same algorithm.

    Distributed Algorithms

  • Touch algorithm

    The key value is used to obtain the remainder of the number of servers, and then the corresponding value is saved to the memcache server of the corresponding remainder. Generally, this hash functionCrc32 (key)% 3

    The crc32 () function converts a string to a 32-bit integer.

    Disadvantage: when a server goes down or needs to be added to a server, the cache data basically becomes invalid because the divisor has changed. Not strict formula. hit rate = retrieved data/total number 1/N indicates the number of servers

    The problem is that when memcache goes down, the cache data becomes invalid, and the pressure on MySQL increases sharply,

    At this time, MySQL will go down, and then restart MySQL. MySQL will go down again in a short period of time, and then a little delay (a part of the cache has been re-established ), downtime. Over time, MySQL becomes stable and the cache system is successfully established. Because the cached data does not exist, all requests must be forwarded to MySQL. this phenomenon is called the avalanche of memcache.

    Overview:

  • Consistent hash for distributed

  • Assume there is a ring with so many positive integers ranging from 0 to 2 ^ 32.

  • After the number of nodes on the server is computed using the hash function (crc32 (), the nodes are mapped clockwise to the ring above.

  • Save the key of the data to be stored to a node that is no smaller than itself in a clockwise direction based on the hash function above.

    Note:If memcache goes down, data will be lost. However, if you want to minimize data loss and use consistent hash, even if one server goes down, it only affects data on one server.

    Virtual node: share the task

    Cache avalanche

    What is the cause of avalanche?

  • Because of improper algorithms and touch algorithms, a large number of cache failures may lead to an avalanche.

    Solution: Consistent hash Algorithm

  • The cache time is the same time, and the cache system will all expire at the same time, which will also cause an avalanche

    Solution: Set the cache time to a random time range (3-9 hours)

    Because the cache data of a memcache node is invalid, the cache hit rate of other memcache nodes is reduced. the missing data in the cache is queried in the MySQL database. in a short period of time, this causes a huge pressure on the MySQL server and downtime, which is called a cache avalanche.

    After MySQL is restarted, it will go down again in a short period of time, but some cached data has been created. After MySQL is started multiple times, the cache is completely rebuilt and MySQL will not go down until it becomes stable.

    Solution: Set the cache time to a random value in a range (3-9 hours), so that it will expire in different time periods and the reconstruction work will be shared to different time periods.

    How to make memcache highly available
  • UseRepcachedImplementation: replication cached is a memcached high availability technology invented by the Japanese, short for replication buffer technology.

  • MemcacheDBIs a distributed, key-value formatPersistent storage system.Developed by sina personnel. It is not a cache component, but a reliable and fast persistent storage engine based on object access. The protocol is consistent with memcache (incomplete), so many memcached clients can connect to it. MemcacheDB uses Berkeley DB as a persistent storage component. Therefore, many Berkeley DB features are supported. Extension
  • In Linux, how does one add an extension to a php and perform common steps?

    A:

  • How does memcache ensure security?

    A:

    Because the memcache design is extremely concise, there is no permission restriction. Why not set permissions? Only the cache function is provided.

  • On the intranet 192.168.1.110, the intranet IP address cannot be accessed from the Internet.
  • Write oneFirewall verification rulesOnly the packets with the specified IP address can be transferred in, and all others can be discarded.

    192.168.1.221 --- 224

    3. what should I do if there are too many files when I use files to save session files?

    Generally, when there are more than 65535 session files, the session acquisition will become abnormally slow, which means php code execution is slow. how can this problem be solved?

    A:

    Tiering: create a folder starting with a A-Z under a folder and then A_Z creates

    Use memcache for processing: if the processing capability of a single memcache is limited, distributed memcache is used for processing.

  • How does one understand that a server load balancer cluster is also a highly available cluster?

    A: The server load balancer cluster provides high availability. if a server is down, it can still provide normal services, but it is a little difficult to provide services.

    Because the server load balancer cluster does not provideKeepalive. Monitoring Mechanism

    Case:

    How to use Memcache in projects?

    Memcache caches data to reduce the pressure on MySQL.

  • Write a list page

  • Details page

    Extension
  • Install a common php extension method in Linux

    A:

  • Download the corresponding extended source http://pecl.php.net/package/memcache
  • Upload the extension to the Linux server and place it under (/usr/local/src)
  • Run tar-zxvf NAME.tar.gz
  • Cd NAME
  • The phpize name (/usr/local/php/bin/phpize) under the absolute path must be in the directory of the extension package.
  • Run. /configure -- with-php-config =/usr/local/php/bin/php-config (-- with-php-config = the php-config path under the absolute path, you do not need to specify the installation path)
  • Make & make install
  • A directory is generated, which contains the NAME. so file.
  • Modify the configuration file of php. ini and add two lines of information.

    Extension_dir = 'directory generated above'

    Extension = 'name. so'

  • Restart Apache, use the phpinfo () function to test the function, enter the address in the browser, and search for Memcache, which indicates that the extension is successfully enabled.

  • Large website optimization-memcache technology memory + cache memory cache introduction memcache is a distributed high-speed cache system...

    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.