This article is for backend web and mobile app developers who wants to learn how to use HTTP2 protocol to reduce data consumption and optimize overall performance of their application.
Who wants to drive a Tesla Model S P100D on a cow path?
The answer is, of course, nobody.
We recently had a similar situation while working on a client’s project.
The problem was, the data consumption rate was bit large in our client’s app. Each time an API was called, the app was required to perform an initial handshake to transfer the necessary amount of data. So, we reduced this data consumption rate by 21.95% and optimized overall performance.
We’ve one client for whom we’ve developed an app which allows its users to order goods from any store in Saudi Arabia. The app has gathered all the stores in its database. So, if a user needs anything from any store, he can post his order from the app, and one of the couriers will deliver the order.
“That’s a cool app; What’s the problem then?”
The problem we solved is technical; If you’ve a technical background, then you might already know what is HTTPS.
According to Wikipedia, HTTPS is a secure version of HTTP (Hypertext Transfer Protocol) for secure communication over a computer network which is widely used on the Internet. The main advantage of HTTPS protocol is the authentication of visited website and protection of the privacy and integrity of the exchanged data.
Now the problem was, We were performing the initial handshake every time an API was called; this resulted in slow performance and more data consumption. Although, the actual amount of data transferred during the handshake is less than 5 KB, but for such small requests, this can be quite a bit of overhead. However, once the handshake is done, it uses a very fast form of symmetric encryption which reduces overhead to minimal. But the main issue is, making lots of small requests over HTTPS is quite slower than HTTP, but if you transfer a lot of data in a single request, the difference will be insignificant.
Furthermore, in HTTP/1.1 keepalive is the default behavior. This means you can perform a single handshake and then send lots of requests over the same connection. But still, the performance difference will not be quite noticeable.
We had assigned two of our most experienced iOS developers for this project. One of them is the iOS team leader in our organization. During the research phase, he discovered a new way in which he reduced the need to perform initial handshake to just once, and then each time an API was called, only the required amount of data was transferred. However, even after making these changes, the difference in data consumption was still not quite noticeable. So, our iOS developers decided to dig more into the subject and found that by using HTTPS/2 protocol, instead of HTTP/1.1, the data consumption rate could be decreased to a more greater amount. And he applied it.
This way, by minimizing the need of initial handshake to one and by using HTTPS/2 protocol, we reduced the amount of data to be transferred upon API call, and eventually the data consumption.
For those who don’t know, Here are the benefits of using HTTPS/2 Protocol.
To learn how to use HTTP2 protocol and upgrade your existing apache system, follow these simple instructions:
$ sudo -i $ apt-get install python-software-properties $ add-apt-repository -y ppa:ondrej/apache2 $ apt-key update $ apt-get update
The above commands will add the latest repository to your system and it will also update the list of available packages that your system is aware of.
$ apt-get --only-upgrade install apache2 -y
This will enable the HTTP2 apache mod.
$ a2enmod http2
Now, edit your Apache2 virtual host configuration to include the protocols directive.
<VirtualHost *:443> # for enabling h2 on an https server Protocols h2 http/1.1 # other SSL configurations go here # ... </VirtualHost>
Lastly, restart Apache2.
$ service apache2 restart
So, by following these simple instructions, you can easily upgrade your existing apache system to use HTTP/2.
The Apache MPM (Multi-processing Module) prefork, starting from Apache 2.4.27, no longer supports HTTP/2.
It shows following error message:
“AH10034: The mpm module (prefork.c) is not supported by mod_http2. The mpm determines how things are processed in your server. HTTP/2 has more demands in this regard and the currently selected mpm will just not do. This is an advisory warning. Your server will continue to work, but the HTTP/2 protocol will be inactive.”
To fix this problem, our iOS developer selected a different MPM – Event prefork. Though you can also choose Worker prefork, but we strongly recommend you to use the Event prefork.
Now, if you’re using PHP, it is more likely that PHP is integrated to Apache via mod_php module, which requires the prefork MPM.
If you switch out from prefork MPM, you’ll need to use PHP as FastCGI. And to switch to php -fpm, you can do as following.
apachectl stop apt-get install php7.1-fpm # Install the php-fpm from your PHP repository. This package name depends on the vendor. a2enmod proxy_fcgi setenvif a2enconf php7.1-fpm # Again, this depends on your PHP vendor. a2dismod php7.1 # This disables mod_php. a2dismod mpm_prefork # This disables the prefork MPM. Only one MPM can run at a time. a2enmod mpm_event # Enable event MPM. You could also enable mpm_worker. apachectl start
However, while switching to php -fpm, please note that the package names could be different in other repositories. Therefore, change package name and apt -get commands to match your PHP vendor and package manager.
If you still have any doubt or question regarding our mobile app development services, feel free to fill out our Contact Us form and drop us a line.
LET'S TALK VALIDATE YOUR IDEA!