Serverless, ReactPHP, and Expanding Frontiers, May 2019

(singke) #1
4 \ May 2019 \ http://www.phparch.com

Deploying ReactPHP Applications

classes, and generally attempts to crash
explosively.
The solution to this is to raise the
hard and soft limit for your applica-
tion’s user. Before you do that, you have
to determine what to raise it to. Your
peak user count plus 15% as the margin
is a good bet. However, that doesn’t take
into account when your site goes viral.
ReactPHP is single threaded at its core,
so at some point, you’ll saturate a CPU
core. You can spawn multiple processes,
and even listen on the same IP:port with
react/socket’s^8 so_reuseport option for
TcpServer^9 to spread the load. You still
have to come to a limit matching the
resources available on your infrastruc-
ture, your architecture, and the usage of
your application. Setting the limits to
150.000 should get you started but it, as
always needs, monitoring.


php.ini
By default PHP lets you use 128MB
of memory. This might be more than
enough for your app, or not at all. As
with ulimit do some research on how
many users you expect and how much
memory your application needs. PHP’s
memory comes in two flavors, the
internal usage (the memory your appli-
cation uses) and the allocated memory
by the PHP process. The latter will
always be higher because it allocates
pages of memory for the application
running inside PHP to use. As a rule
of thumb, leave at the very least 50% of
the available free for the OS and other
services you run aside your application.
My memory limit is set to 1GB, and
increasing that when it starts to run out.
(But only when a memory leak in the
application doesn’t cause it.)


8 react/socket’s :
https://reactphp.org/socket/
9 TcpServer:
https://reactphp.org/socket/#tcpserver


When developing an application
with react/http be sure to tweak the
post_max_size INI setting as well.
Together with memory_limit it is used
to configure React\Http\Server where a
POST message cannot cause you to run

out of memory. It does so using the
following calculation: (memory_limit
/ 4) / post_max_size. The middleware
automatically set up in React\Http\
Server also respect max_input_vars, and
max_input_nesting_level to prevent

Figure 1. Memory Graph

Listing 1


  1. version: 2

  2. jobs:

  3. build:

  4. docker:



    • image: wyrihaximusnet/php:7.3-nts-alpine3.9-dev





    • image: rabbitmq:3.5.



  5. working_directory: ~/repo

  6. steps:



    • checkout





    • run: cp .env.example .env





    • run: composer install -n --ansi --no-progress --optimize-autoloader





    • run: ls -lasth





    • run: make





    • run: rm .env





    • run: composer install -n --ansi --no-progress --optimize-autoloader --no-dev





    • run: echo $CIRCLE_TAG >> ~/repo/version





    • persist_to_workspace:



  7. root: ~/repo

  8. paths:



    • ./*



  9. deploy:

  10. docker:



    • image: ansible/ansible:ubuntu



  11. environment:

  12. ANSIBLE_HOST_KEY_CHECKING: no

  13. working_directory: ~/repo

  14. steps:



    • checkout





    • attach_workspace:



  15. at: ~/repo



    • run:



  16. name: Install System Packages

  17. command: pip install ansible



    • run: ansible-galaxy install -r .circleci/requirements.yml





    • run: ANSIBLE_FORCE_COLOR=true ansible-playbook -i .circleci/hosts.ini
      .circleci/deploy.yml



  18. workflows:

  19. version: 2

  20. build-n-deploy:

  21. jobs:



    • build:



  22. filters:

  23. tags:

  24. only: /.*/



    • deploy:



  25. requires:



    • build



  26. filters:

  27. tags:

  28. only: /.*/

  29. branches:

  30. ignore: /.*/

Free download pdf