The Art of Resource Optimization: Lessons from Restaurant Operations for Server Efficiency
Tags: php, webdev, backend
Published: 2023-07-05 20:54:38


Imagine you want to open a new and charming restaurant in the heart of the city. With unique menu ideas, a strongbackground in culinary arts, and a thriving restaurant market, you're excited to introduce your new creation to the
world. But despite the excitement, there are important considerations you need to take into account.

On the one hand, you want to provide the best and fastest service possible. Customers who feel that your service is slow
and inefficient will choose to go to a competing restaurant.

Therefore, your initial thought will be to have as many waiters and chefs as possible. Maybe even one waiter and one
chef per table. This way, you can ensure the fastest service.

On the other hand, you have limited resources. Waiters and chefs require salaries, money, and if you want to make a
profit, you need to minimize labor costs.

So perhaps just one chef and one waiter? This will negatively impact your service when there is a high volume of
customers. One waiter and one chef will struggle to handle the load, and the service will be very slow.

Another option is to time the waiters based on demand. Call the waiter only when customers arrive. However, this is also
not a good solution because it takes time for the waiter to arrive and start providing service, which will negatively
affect customer service.

The solution is to manage wisely. Define a small number of waiters who are consistently present from the opening of the
restaurant. Set a maximum number of waiters available during peak demand.
When there is no waiter available, there is no reason for them to leave immediately when there is no demand. Therefore,
define a time for how long the waiter will stay after finishing their service.

The server is like the restaurant, providing the main service. In each server, you have a limited number of resources,
such as RAM. Just as in a restaurant, you have various roles like waiters and chefs, in a server, you have a number of
processes running in the background, such as in a LEMP server:

* NGINX
* PHP-FPM
* MYSQL

In that guide, we will focus on PHP-FPM. However, we can apply this to other services as well.

First, let's start by understanding your resource usage.
You can do this using the command:

```bash
 free -h
```

The `free` command displays information about memory (RAM) usage on your computer in a readable and understandable
format.

The information displayed by the command includes:

1. **Total:** The total amount of memory on your computer.
2. **Used:** The amount of memory currently in use by processes and programs on your computer.
3. **Free:** The amount of memory not in use and available for immediate use by additional processes or programs.
4. **Available:** The amount of memory available for additional use. This includes memory not in use at the moment and
   can be immediately used by additional processes or programs.
5. **Buffers/Cache:** The amount of memory used by the system for temporary data storage or caching of information. This
   data helps improve system performance.

Now, let's determine the memory "cost" of each waiter, or in this case, each PHP-FPM process.

You can use the `ps` command to find and sort active PHP-FPM processes by their Resident Set Size (RSS), which is the
amount of physical memory (RAM) used by the process.

We will run the following command:

```bash
ps -ylC php-fpm --sort:rss
```

The command displays a list of php-fpm processes and sorts them based on their physical memory size from smallest to
largest.

- **"ps":** The command itself to display a list of processes.
- **"-y":** Displays the processes in a "table" format with additional columns.
- **"-l":** Shows detailed information about the processes, including the parent process of each process.
- **"-C php-fpm":** Displays processes only for the process named "php-fpm".
- **"--sort:rss":** Sorts the processes based on their physical memory size (RSS, Resident Set Size) from smallest to
largest.

To calculate the average, we will use the 'awk' command and run the following:

```bash
ps --no-headers -o "rss,cmd" -C php-fpm7.0 | awk '{ sum+=$1 } END { printf ("%d%s\n", sum/NR/1024,"M") }'
```

Now that we have information about our resources and the average cost of each process,
we can determine the number of processes we want to run.

The settings can be found in the file /etc/php/X.0/fpm/pool.d/www.conf, depending on the PHP version.

```apacheconf
pm.max_children = 17
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
```
###### max_children
First, we define the maximum number of servers we can handle by calculating a portion of the average resources required
by each process.

For example: 2560 Mb / 60 Mb = 42 **max_children**

###### start_servers
Once we know the maximum number, we don't want to use them all the time as it would overload the server.
Therefore, we set a minimum number of servers when the restaurant opens by editing the value of *start_servers*.

###### min_spare_servers & max_spare_servers
After customers start arriving and the servers get busy,
we want to set a minimum number of available servers so that customers don't have to wait for a server to be called and
organized.
We achieve this by editing the parameter value of **min_spare_servers**.

In other words, if we set **start_servers** to 5 and **min_spare_servers** to 3, when the server load increases, 5
processes will automatically be running.
Once 3 of them start providing service, the process manager (pm) will start an additional server to ensure availability.

If the load becomes excessive and many servers are already running, we don't want to send them home quickly.
Instead, they will stay in the restaurant. However, since there is currently no load,
we define a maximum limit for the number of spares using the **max_spare_servers** field.

To summarize, in the restaurant world, as well as in the server world,
proper allocation of resources and processes is the key to smooth and efficient operation. With precise settings and
smart use of technological tools, we can ensure that your restaurant (host server) positively impacts customers (users)
and meets their needs professionally and efficiently.

.