8 \ May 2019 \ http://www.phparch.com
Deploying ReactPHP Applications
Graphing
For graphing, Grafana is the way to
go as it offers completely customizable
dashboards (see Figure 2), templating,
a wide range of data backends, and for
our next section: alerting.
To graph the current memory usage,
we would use the following Graphite
query aliasByNode(PREFIX.APP_TYPE.
APP_NAME.memory.*, 5). One of the small
but beautiful things about Grafana is
that it supports all kinds of data. We’ll
set ours to bytes for the Left Y under
the Axes tab.
There is much more to it, but that is
way out of scope for this article. I’ve
recently created an application template
which includes all of this using dock-
er-composer you can find it at reactive-apps/skeleton^22.
Alerting
Monitoring is excellent but alerting gives you the edge
when it comes to early downtime response. Grafana comes
with a robust alerting system allowing you to decide under
which circumstances an alert is sent out. Grafana supports a
set of notification protocols^23 such as email, Slack, and Tele-
gram. For my memory alerts (see Figure 3), my setup alerts
me whenever any of my ReactPHP applications use more
than 150MB of memory. Most of them run under 50MB but
when traffic peaks their memory usage also goes up. Memory
usage is an indicator for me to keep an eye on the server to see
if it handles the traffic well.
Conclusion
While deploying a ReactPHP application might sound a lot
more complicated in reality it isn’t. It does, however, as we
explored in this article, require more planning and prepara-
tions because you’re deploying a daemon rather than a site
handled by NGINX and FPM/Apache. Planning and prepa-
ration make sure your application runs smoothly and scales
well with your expanding user base.
Cees-Jan has been developing web
applications and services with PHP since the
early 2000’s. He is a core maintainer of
ReactPHP and founder of async first PHP
API Clients^24. When Cees-Jan isn’t devel-
oping, he enjoys trying out new craft beers,
meeting developers at local meetups, and the
peace and quiet of the garden.
@WyriHaximus
22 reactive-apps/skeleton:
https://github.com/reactive-apps/skeleton
23 notification protocols: https://phpa.me/grafana-alerts
24 PHP API Clients: https://github.com/php-api-clients
Figure 2. Full Dashboard
Figure 3. Alert Config
Related Reading
- Evolving PHP by Chris Pitt. March 2018.
https://phparch.com/article/evolving-php/ - Learning to Code with Minecraft, Part Two
by Chris Pitt. March 2017.
https://phparch.com/magazine/2017-2/march/ - Writing Asynchronous Code in PHP with Icicle
by Aaron Piotrowski. July 2017.
https://phparch.com/magazine/2017-2/july/