In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "how to achieve connection pooling in php". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
What is connection pooling
In practical application development, there are generally many requirements to access downstream (including but not limited to services / databases / caches). When the concurrency is very low, there is usually no problem, but when the service stand-alone QPS reaches hundreds, thousands, or even tens of thousands, if each access still has to go through steps such as establishing a connection = "sending and receiving requests =" closing connections = "releasing resources." The performance of the system is bound to decline sharply, and even cause the system to crash.
Connection pooling is the most commonly used method to solve this problem, and its idea is very simple, that is, when the service starts, several connections are established first, and when there is a request, one is taken out, downstream operations are performed, and then put back. In order to avoid repeated establishment and destruction of connections, in order to improve performance.
How to implement connection pooling
Connection pooling is a stand-alone service that requires resident processes. A complete connection pooling operation usually goes through the following steps:
(1) establish a connection pool object (service startup).
(2) create an initial number of connections according to the parameters specified in advance (that is, the number of idle connections).
(3) for an access request, get a connection directly from the connection pool. If there are no idle connections in the connection pool object, and the number of connections does not reach the maximum (that is, the maximum number of active connections), create a new connection; if it reaches the maximum, set a certain timeout to obtain the connection.
(4) use connection to access service.
(5) when the access service is completed, release the connection (at this time, the release connection is not really closed, but put it in the idle queue. If the actual number of idle connections is greater than the initial number of idle connections, the connection is released.
(6) release connection pooling objects (release connection pooling objects and all connections during service stop and maintenance).
Java is friendly to connection pooling and has many mature solutions, such as DBCP, C3P0, Druid and so on. PHP is relatively weak in this respect, although Mysql provides long-connected API, but in the case of a large number of PHP machines and large scale, mysql_pconnect can not save MySQL resources, but will increase the load of the database. This is mainly because it relies on apache or fpm. For example, if you have 100 PHP application servers, each machine needs to start 100 apache or fpm worker processes, and each process will generate a long connection to MySQL, resulting in a total of 10, 000 MySQL connections. As we all know, MySQL takes up 1 thread per connection. If there are 10, 000 connections, MYSQL needs to create 10, 000 threads, so a lot of system resources will be wasted on inter-thread context switching. In fact, database operations are not done everywhere in your business code, so if you create so many connections, but many of them are free, it will be a waste of resources.
In view of this, if PHP wants to solve the problem of connection reuse independently, it must implement its own connection pool. Fortunately, the PHP extension swoole can just do this, and it is very convenient to make a connection pool using the task function provided by swoole.
The following is part of the code to implement connection pooling using swoole:
Through this piece of code, PHP can implement connection pooling and solve the problem that connections are not reusable.
Of course, the above is just an example, and it is certainly not enough to use it in a production environment. A ready-made tool, SMProxy, is recommended here.
SMProxy is a mysql database connection pool developed using swoole based on the mysql protocol. It has the following features:
Support for read-write separation
Support database connection pool, which can effectively solve the bottleneck of database connection caused by PHP.
Support for SQL92 standards
Adopt cooperative scheduling
Support multiple database connections, multiple databases, multiple users, flexible collocation
Abide by MySQL native protocol, cross-language, cross-platform general middleware agent
Support for MySQL transactions
Support for HandshakeV10 protocol version
Perfectly compatible with MySQL5.5-8.0
Compatible with all major frameworks to improve performance seamlessly
In addition, it also supports domestic mainstream frameworks such as Laravel and ThinkPHP.
So, if you are using PHP and want to use connection pooling, take a look at this tool.
That's all for "how to implement connection pooling in php". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.