PHP is alive. PHP 7 in practice
Recently, the Avito PHP projects have moved to the version of PHP 7.1. On this occasion, we decided to recall how the transition to PHP 7.0 took place for us and our colleagues from OLX. It's a long time ago, but there are beautiful graphics that you want to show the world.
The first part of the story is based on the article PHP's not dead! PHP7 in practice , written by our colleague from OLX Łukasz Szymański (Lukasz Szymanski): the transition of OLX to PHP 7. In the second part - the Avito transition experience in PHP 7.0 and PHP 7.1: the process, the difficulties, the results with the graphs.
Part 1. PHP 7 in OLXOLX Europe manages ten sites, the largest of which is OLX.pl. All our sites should work as efficiently as possible, so migration to PHP 7 has become our top priority.
In this post we will tell you what problems we had to face and what we managed to get with the transition to PHP 7. The transition was told at the PHPers Summit 2016 conference.
TransitionContrary to our fears, the migration went smoothly. Except for the standard list of necessary changes from the official documentation , we had to make only some edits related to our architecture.
It is worth mentioning that ten of our sites work in different countries. And the changes we roll out consistently: one site after another. This approach is particularly important for serious changes.
We started updating the version from the smallest site and went on to larger and larger ones, looking for tests to be successful. This allowed us to monitor the occurrence of unexpected problems and reduce the potential damage.
MemcacheFailure to support Memcache in PHP 7 pushed us to the transition to Memcached. I had to support two versions of the site: PHP 5 + Memcache and PHP 7 + Memcached.
To solve the problem, we used a simple wrapper. It selects the appropriate PHP module to connect to the cache, based on the information about the server on which the code is running.
<?phpHowever, the adventures with Memcached did not end there. It turned out that objects serialized with Memcache can not be deserialized with Memcached.
$factory = new CacheWrapperFactory();
extension_loaded('memcache') ? 'memcache' : 'memcached',
But the cache - it is for this and the cache, that it can be easily removed. Therefore, we simply deleted the old problem objects from the cache, and they were recreated using the new module.
APC, APCu, and OPCacheA little bit about terms.
APC (Alternative PHP Cache) is a cache of bytecode and user data.
APCu (APC User Cache) - only the cache of user data.
OPCache - only byte cache.
In PHP 7 there is no APC, we had to take both APCu and OPCache. Previously, we used the APC API in many critical parts of our framework that manages the cache, so we screwed the apcu-bc module, compatible with the APCu The APC API.
ResultsBelow are the results of the largest of our sites, OLX.pl.
CPU on ApacheOur web servers (Apache) revolve around 20 physical machines with 32 processor cores each. The peak CPU consumption as a result of migration decreased from 50% to 20%.
LA on ApacheSimilarly, the Load Average on web servers also decreased.
The numbers speak for themselves: reducing the load, saving resources, increasing efficiency. If your project managers or clients do not give enough time to migrate - these graphs will surely be able to convince them.
Reasons for the effectiveness of PHP 7Such striking results were achieved thanks to optimization in three main areas.
Less memory allocation operationsPHP 5 spent 20% of the CPU time consumed only for memory allocation operations. Developers of the language paid attention to this, reduced the number of memory allocation operations, which significantly reduced the CPU consumption.
Less memory consumption
This sketch shows the path that the processor needs to go through to access RAM (RAM), specifying the time for each step. As you can see, the segment to the RAM is the longest. PHP developers have reduced memory consumption by accelerating application response.
Less intermediate pointers
The last reason for improving the efficiency of PHP 7 is the decrease in the number of intermediate pointers. To do this, PHP developers got rid of a lot of pointers that refer to other pointers; Instead, they forced the pointers to refer directly to the requested entities.
CodeIn addition to improving efficiency, PHP 7 has a small revolution in the structure of the code.
The scalars in the function declarationPrior to PHP 7, the function arguments could only be an object, an interface, an array, or a callable function. Take for example the following code.
Obviously, using such methods promises many problems if you are not sure about the correctness of incoming arguments.
In addition to adding type checks to each method, you can use the ValueObject construct from object-oriented programming (DDD).
PHP 7 allows you to simply specify scalars, such as string, int, float, bool. In addition, you can specify the type of the return value.
Strict modeBut simply adding the above constructs to the code may not yield the expected results. All because PHP 7 by default works in coercive-mode, in which the usual type conversions occur. If you did not explicitly enable strict mode, you can rewrite the above method as follows.
Unfortunately, even adding the declare (strict_types = 1) construct to the PHPisNotDead.php file does not include strict mode. The explanation is in the example below. The comments indicate the return values.
Why is this happening? Strict typing in methods of class PHPisNotDead was included only for the returned values.
If you need to enable strict typing also for arguments, you'll have to add the strict_types declaration and all the files that call the class's methods.
More information about type specification and its impact on program execution can be found in documentation < tgsrcut>. Reading this documentation will save you from surprises while executing your code.
The futureIf even now you still have not decided to go to PHP 7, look in the list of <a rel="nofollow" href="http://php.net/supported-versions.php"> supported versions of PHP . Version 5.6 is no longer actively supported, and at the end of 2018 even fixes for critical vulnerabilities will cease. Active support continues for versions 7.0 and higher.
Follow the news of the PHP world and plans for new beliefs. The most interesting posts are: friendly classes , generic types and functions . Find other suggestions for language development in PHP RFC. <Tgsrcut>
ResultsPHP 7 is impressive: in addition to improving efficiency, it helps developers write better code. I have sketched a small allowance that will help you decide on the transition.
- When should I go to PHP 7?
- Right now.
Part 2. PHP 7 in Avito
The migration to PHP 7.0We, just like OLX, carried out the translation of our services to PHP 7 gradually. First, small small services were transferred, tested them, and corrected the errors that occurred. Next, we moved on to the server site administration update, then rolled out the remaining services and the site to PHP 7.
Transition difficultiesWe read the list of <a rel="nofollow" href="http://php.net/manual/en/migration70.incompatible.php"> backward incompatible changes . However, this did not protect them from all troubles.
In the old code, there was a class called String. PHP 7 issued the error "Can not use 'String' as class name as it is reserved". The class was renamed. Pay attention to the list of reserved words .
In PHP 7, the cache file format for the WSDL schema in SoapClient has changed. You can configure the cache to be stored in different directories depending on the version of PHP or completely clear the cache before changing the interpreter version.
The expansion of mongo, which we actively used, was not supported in the new PHP. Instead, we started using the official MongoDB PHP Library library. Went through the entire code and replaced MongoCollection :: insert () with MongoDB \ Collection :: insertOne (), MongoCollection :: remove () on MongoDB \ Collection :: deleteMany () and then in the list. Began using classes to work with BSON from the new MongoDB driver, for example, MongoDate instead of MongoDB \ BSON \ UTCDateTime.
The result isAdminke felt better.
Backend site is also happy.
Upgrade to PHP 7.1In PHP 7.1, a few very nice innovations have appeared: the void type for return values, iterable, the ability to return null for typed return values, the scope of constants, and so on. In addition, the period of active support for PHP 7.0 ends at the end of this year. We decided to upgrade to PHP 7.1.
SurprisesWhen updating on an even place a problem was formed. The php-memcached package for 7.1 pulled the php-igbinary package. When they put PHP 7.1 on one of the battle servers, errors started from the rest of the servers, in which the word "igbinary" appeared.
An old friend of Memcache, again the differences of serialization, but a little under another sauce. It turned out that the php-memcached module by default uses the first available serializer from the list: igbinary (in a separate module), msgpack (in a separate module), php (does not require a separate module, always available). And the server on which we installed 7.1 with igbinary, began to write data to the memcache, serialized igbinary. And on the other servers there was no support for this serializer, and they could not read the data written by the server with the updated PHP. Localized the problem, installed igbinary on all other servers, the errors stopped.
AfterwordPHP developers took a good course. They add useful tools, get rid of the shortcomings associated with the legacy of the language, seriously think about performance.
Earlier we already talked about Avito's transition to the service architecture ( times , two ). This architecture allows you to write in any language, and we usually write new services on Go or Python. However, the refusal of PHP speech is not: the main logic of the site (monolith) is still in PHP, and the team knows perfectly well how to work with it. New versions of the interpreter allow you to make the code better, and its execution faster.
Share your experience of moving to PHP 7 and above, we will be happy to discuss the discoveries and rakes that met you along the way.
|Vote for this post
Bring it to the Main Page