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

PHP extended Memcache distributed deployment method

2025-04-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces "PHP extended Memcache distributed deployment method". In daily operation, I believe many people have doubts about PHP extended Memcache distributed deployment method. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "PHP extended Memcache distributed deployment method". Next, please follow the editor to study!

Basic environment

In fact, the Memcache client based on the PHP extension has been implemented for a long time and is very stable. Let's first explain some nouns. Memcache is an open source project of danga.com, which can be compared to services like MySQL, while Memcache extended by PHP is actually a way to connect to Memcache.

First of all, to install Memcache, you can check out several other articles in the blog.

Second, install the PHP extension, the official address is http://pecl.php.net/package/memcache

Finally, start the Memcache service, such as this, to start multiple processes through different ports to simulate distribution:

The copy code is as follows:

/ usr/local/bin/memcached-d-p 11211-u root-m 10-c 1024-t 8-P / tmp/memcached.pid

/ usr/local/bin/memcached-d-p 11213-u root-m 10-c 1024-t 8-P / tmp/memcached.pid

/ usr/local/bin/memcached-d-p 11214-u root-m 10-c 1024-t 8-P / tmp/memcached.pid

Start three using only 10m of memory to facilitate testing.

Parameter description:

The-d option is to start a daemon

-m is the amount of memory allocated to Memcache in MB, and this is 512MB.

-u is the user running Memcache. This is root.

-l is the IP address of the listening server. If there are multiple addresses, I specify the IP address of the server 192.168.0.1.

-p is the port on which Memcache snooping is set. I have set 11211 here, preferably a port above 1024.

The-c option is the maximum number of concurrent connections running. The default is 1024. I set it here according to the load of your server.

-P is the pid file that is set to save Memcache. Here I am saving.

Distributed deployment

Memcache in PHP's PECL extension actually implemented multi-server support in version 2.0.0, which is now 2.2.5. Please look at the following code

$memcache = new Memcache;$memcache- > addServer ('localhost', 11211); $memcache- > addServer (' localhost', 11213); $memcache- > addServer ('localhost', 11214); $memStats = $memcache- > getExtendedStats (); print_r ($memStats)

Through the example above, the distributed deployment of Memcache has been realized, which is not very simple.

Benign Operation of distributed system

In the actual use of Memcache, the most serious problem encountered is that when adding or decreasing servers, it will lead to a wide range of cache loss, which may lead to the performance bottleneck of the database. When testing, you can test whether the data exists by shutting down a memcached process. For example:

Shutting down one of them during testing may result in data loss:

The value of 'string' was added before the addition of'(length=35) string 'String to store in memcached' (length=28) boolean falseboolean false in 11213.

In order to avoid this situation, please first look at the Consistent hashing algorithm, the Chinese introduction can refer to the comprehensive analysis of memcached-4. The distributed algorithm of memcached is realized by changing the algorithm of the selected server when accessing.

Although memcached is called a "distributed" cache server, there is no "distributed" function on the server side.

In the source code of the Memcache extension memcache.c for PHP

"memcache.hash_strategy" = standard

For

"memcache.hash_strategy" = consistent

Recompile, it is time to use the Consistent hashing algorithm to find the server to access the data.

Effective test data show that the use of Consistent hashing can greatly improve the large-scale loss of cache when adding and deleting Memcache.

NonConsistentHash: 92% of lookups changed after adding a target to the existing 10NonConsistentHash: 90% of lookups changed after removing 1 of 10 targetsConsistentHash: 6% of lookups changed after adding a target to the existing 10ConsistentHash: 9% of lookups changed after removing 1 of 10 targets

Security configuration

The Memcache server operates directly after connecting directly through the client, without any verification process, so it is more dangerous if the server is directly exposed to the Internet, the data leak will be checked by other unrelated people, and the server will be invaded, because Mecache is run with root authority, and there may be some unknown bug or buffer overflow conditions, which are unknown to us. So the danger is predictable.

Private network access

It is best to put the access between the two servers in the form of an intranet, usually between the Web server and the Memcache server. Most servers have two network cards, one pointing to the Internet and the other pointing to the private network, so let the Web server access the Memcache server through the network card of the private network. When our Memcache server is started, it monitors the IP address and port of the internal network, and the access between private networks can effectively prevent other illegal visits.

The copy code is as follows:

# memcached-d-m 1024-u root-l 192.168.0.200-p 11211-c 1024-P / tmp/memcached.pid

The Memcache server sets port 11211 to listen on the 192.168.0.200 ip through the intranet, which takes up 1024MB memory and allows up to 1024 concurrent connections.

Set up a firewall

Firewall is a simple and effective way, if both servers are connected to the network and need to access Memcache through the external network IP, then you can consider using firewalls or agents to filter illegal access. Generally, we can use ipfw under iptables or FreeBSD under Linux to specify some rules to prevent illegal access. For example, we can set only our Web server to access our Memcache server, while blocking other access.

# iptables-F# iptables-P INPUT DROP# iptables-An INPUT-p tcp-s 192.168.0.2-- dport 11211-j ACCEPT# iptables-An INPUT-p udp-s 192.168.0.2-- dport 11211-j ACCEPT

The above iptables rule is to allow only 192.168.0.2 this Web server to access the Memcache server, which can effectively prevent some illegal access, and accordingly can add some other rules to strengthen security, which can be done according to your own needs.

At this point, the study on "PHP extension Memcache distributed deployment method" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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

Development

Wechat

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

12
Report