<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: elad gasner</title>
    <description>The latest articles on DEV Community by elad gasner (@gasner).</description>
    <link>https://dev.to/gasner</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1081350%2F29331721-3a5c-4479-8dc6-64dd4cfb9cf9.jpg</url>
      <title>DEV Community: elad gasner</title>
      <link>https://dev.to/gasner</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/gasner"/>
    <language>en</language>
    <item>
      <title>Simplicity in Optimization: Enhance Your System with Swap</title>
      <dc:creator>elad gasner</dc:creator>
      <pubDate>Tue, 26 Sep 2023 12:00:29 +0000</pubDate>
      <link>https://dev.to/gasner/title-simplicity-in-optimization-enhance-your-system-with-swap-1253</link>
      <guid>https://dev.to/gasner/title-simplicity-in-optimization-enhance-your-system-with-swap-1253</guid>
      <description>&lt;p&gt;Swap space, often referred to simply as "swap," is a dedicated area on a computer's storage device, typically a hard drive or SSD, used as virtual memory. It's an integral component of operating systems like Linux and Windows and plays a crucial role in efficiently managing a computer's memory (RAM). When the physical RAM reaches its capacity, swap space comes into play as an extension of your system's memory, ensuring that your system continues to function without interruption.&lt;/p&gt;

&lt;p&gt;Adding Swap Space for Improved Performance:&lt;/p&gt;

&lt;p&gt;If you find yourself with limited physical RAM and want to enhance your system's performance by providing additional virtual memory, adding swap space can be a beneficial solution. Here's how you can augment your system's capabilities with 2GB of RAM:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Before proceeding, make sure you have administrative privileges on your system.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Check Current Swap:&lt;/strong&gt; Before adding more swap space, it's a good idea to check if you already have existing swap space. Open a terminal and run:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;swapon &lt;span class="nt"&gt;--show&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will display any active swap partitions/files.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Create a Swap File:&lt;/strong&gt; If you don't have existing swap space, you can create a swap file. Choose a location with enough disk space. To create a 2GB swap file, run the following commands:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;fallocate &lt;span class="nt"&gt;-l&lt;/span&gt; 2G /swapfile   &lt;span class="c"&gt;# Create a 2GB swap file&lt;/span&gt;
&lt;span class="nb"&gt;sudo chmod &lt;/span&gt;600 /swapfile         &lt;span class="c"&gt;# Set appropriate permissions&lt;/span&gt;
&lt;span class="nb"&gt;sudo &lt;/span&gt;mkswap /swapfile            &lt;span class="c"&gt;# Format the file for swapping&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Activate the Swap File:&lt;/strong&gt; Once the swap file is created, you need to activate it:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;swapon /swapfile   &lt;span class="c"&gt;# Activate the swap file&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Make Swap Permanent:&lt;/strong&gt; The swap file will be deactivated after a reboot unless you add it to your system's configuration. Edit the &lt;code&gt;/etc/fstab&lt;/code&gt; file to make the swap permanent:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;nano /etc/fstab
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Add the following line at the end of the file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/swapfile none swap sw 0 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Save and exit the text editor.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Adjust Swappiness (Optional):&lt;/strong&gt; Swappiness controls how aggressively your system uses swap space. A value of 0 avoids swap as much as possible, while a value of 100 aggressively uses swap. To adjust swappiness, run:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;sysctl vm.swappiness&lt;span class="o"&gt;=&lt;/span&gt;10
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This sets swappiness to a lower value (e.g., 10) to avoid swapping too much. To make the change persistent after reboot, add the following line to the &lt;code&gt;/etc/sysctl.conf&lt;/code&gt; file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; vm.swappiness=10
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Check Swap:&lt;/strong&gt; After performing these steps, you can check if the swap space is active:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;swapon &lt;span class="nt"&gt;--show&lt;/span&gt;
free &lt;span class="nt"&gt;-h&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;free -h&lt;/code&gt; command will display your system's memory usage, including swap.&lt;/p&gt;

&lt;p&gt;Adding swap space can improve your system's performance, but it's important to note that swap is slower than physical RAM. It's better to have enough RAM for your needs, but swap can be a helpful supplement when RAM is limited.&lt;/p&gt;

</description>
      <category>devops</category>
      <category>fullstack</category>
      <category>webdev</category>
    </item>
    <item>
      <title>The Art of Resource Optimization: Lessons from Restaurant Operations for Server Efficiency</title>
      <dc:creator>elad gasner</dc:creator>
      <pubDate>Wed, 05 Jul 2023 20:54:38 +0000</pubDate>
      <link>https://dev.to/gasner/boosting-php-fpm-performance-unleashing-your-inner-chef-3g3n</link>
      <guid>https://dev.to/gasner/boosting-php-fpm-performance-unleashing-your-inner-chef-3g3n</guid>
      <description>&lt;p&gt;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&lt;br&gt;
world. But despite the excitement, there are important considerations you need to take into account.&lt;/p&gt;

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

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

&lt;p&gt;On the other hand, you have limited resources. Waiters and chefs require salaries, money, and if you want to make a&lt;br&gt;
profit, you need to minimize labor costs.&lt;/p&gt;

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

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

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

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

&lt;ul&gt;
&lt;li&gt;NGINX&lt;/li&gt;
&lt;li&gt;PHP-FPM&lt;/li&gt;
&lt;li&gt;MYSQL&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In that guide, we will focus on PHP-FPM. However, we can apply this to other services as well.&lt;/p&gt;

&lt;p&gt;First, let's start by understanding your resource usage.&lt;br&gt;
You can do this using the command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt; free &lt;span class="nt"&gt;-h&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;free&lt;/code&gt; command displays information about memory (RAM) usage on your computer in a readable and understandable&lt;br&gt;
format.&lt;/p&gt;

&lt;p&gt;The information displayed by the command includes:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Total:&lt;/strong&gt; The total amount of memory on your computer.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Used:&lt;/strong&gt; The amount of memory currently in use by processes and programs on your computer.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Free:&lt;/strong&gt; The amount of memory not in use and available for immediate use by additional processes or programs.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Available:&lt;/strong&gt; 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.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Buffers/Cache:&lt;/strong&gt; The amount of memory used by the system for temporary data storage or caching of information. This
data helps improve system performance.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now, let's determine the memory "cost" of each waiter, or in this case, each PHP-FPM process.&lt;/p&gt;

&lt;p&gt;You can use the &lt;code&gt;ps&lt;/code&gt; command to find and sort active PHP-FPM processes by their Resident Set Size (RSS), which is the&lt;br&gt;
amount of physical memory (RAM) used by the process.&lt;/p&gt;

&lt;p&gt;We will run the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;ps &lt;span class="nt"&gt;-ylC&lt;/span&gt; php-fpm &lt;span class="nt"&gt;--sort&lt;/span&gt;:rss
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The command displays a list of php-fpm processes and sorts them based on their physical memory size from smallest to&lt;br&gt;
largest.&lt;/p&gt;

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

&lt;p&gt;To calculate the average, we will use the 'awk' command and run the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;ps &lt;span class="nt"&gt;--no-headers&lt;/span&gt; &lt;span class="nt"&gt;-o&lt;/span&gt; &lt;span class="s2"&gt;"rss,cmd"&lt;/span&gt; &lt;span class="nt"&gt;-C&lt;/span&gt; php-fpm7.0 | &lt;span class="nb"&gt;awk&lt;/span&gt; &lt;span class="s1"&gt;'{ sum+=$1 } END { printf ("%d%s\n", sum/NR/1024,"M") }'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now that we have information about our resources and the average cost of each process,&lt;br&gt;
we can determine the number of processes we want to run.&lt;/p&gt;

&lt;p&gt;The settings can be found in the file /etc/php/X.0/fpm/pool.d/&lt;a href="http://www.conf"&gt;www.conf&lt;/a&gt;, depending on the PHP version.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pm.max_children = 17
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h6&gt;
  
  
  max_children
&lt;/h6&gt;

&lt;p&gt;First, we define the maximum number of servers we can handle by calculating a portion of the average resources required&lt;br&gt;
by each process.&lt;/p&gt;

&lt;p&gt;For example: 2560 Mb / 60 Mb = 42 &lt;strong&gt;max_children&lt;/strong&gt;&lt;/p&gt;

&lt;h6&gt;
  
  
  start_servers
&lt;/h6&gt;

&lt;p&gt;Once we know the maximum number, we don't want to use them all the time as it would overload the server.&lt;br&gt;
Therefore, we set a minimum number of servers when the restaurant opens by editing the value of &lt;em&gt;start_servers&lt;/em&gt;.&lt;/p&gt;

&lt;h6&gt;
  
  
  min_spare_servers &amp;amp; max_spare_servers
&lt;/h6&gt;

&lt;p&gt;After customers start arriving and the servers get busy,&lt;br&gt;
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&lt;br&gt;
organized.&lt;br&gt;
We achieve this by editing the parameter value of &lt;strong&gt;min_spare_servers&lt;/strong&gt;.&lt;/p&gt;

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

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

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

&lt;p&gt;.&lt;/p&gt;

</description>
      <category>php</category>
      <category>webdev</category>
      <category>backend</category>
    </item>
    <item>
      <title>Why you should build your own framework but you shouldn’t use it</title>
      <dc:creator>elad gasner</dc:creator>
      <pubDate>Sun, 14 May 2023 20:24:55 +0000</pubDate>
      <link>https://dev.to/gasner/why-you-should-build-your-own-framework-but-you-shouldnt-use-it-15jf</link>
      <guid>https://dev.to/gasner/why-you-should-build-your-own-framework-but-you-shouldnt-use-it-15jf</guid>
      <description>&lt;p&gt;Building your own web framework can be a rewarding experience for any developer. Not only do you gain a deeper understanding of how web development works, but you also have the potential to create a tool that can be used in your own projects.&lt;/p&gt;

&lt;p&gt;However, the question remains: should you use the framework you just built in your production projects? While it might be tempting to do so, I believe there are compelling reasons why you shouldn't.&lt;/p&gt;

&lt;p&gt;I want to share with you my experience of building my own framework, the challenges I faced, and why I ultimately decided not to use it for my production projects. By the end of this article, you'll have a better understanding of why building your own framework is a valuable learning experience, but why you should stick to using established frameworks for your production work.&lt;/p&gt;

&lt;p&gt;When I started learning Laravel, I decided to create my own framework from scratch. I wanted to understand how Laravel worked under the hood, and I thought it would be a good way to learn. I spent countless hours researching and coding, and eventually, I had my own custom framework up and running.&lt;/p&gt;

&lt;p&gt;At first, I was excited to start using my new framework for all my projects. But as I began to use it more and more, I realized that it had a lot of flaws. There were bugs I hadn't noticed, features that didn't work as intended, and performance issues that slowed down my applications.&lt;/p&gt;

&lt;p&gt;I tried to fix these issues, but it quickly became apparent that maintaining my own framework was a full-time job. I was spending more time fixing my framework than actually building applications. And every time I fixed one issue, another one seemed to pop up.&lt;/p&gt;

&lt;p&gt;That's when I realized that building your own framework is a great way to learn, but it's not necessarily the best way to build software. Frameworks like Laravel and Symfony are widely used and well-tested, and they have a large community of developers contributing to their development. By using one of these established frameworks, you can focus on building your application rather than maintaining your framework.&lt;/p&gt;

&lt;p&gt;Building your own web framework can be a fun and challenging project, but when it comes to production work, stability is key. Your clients rely on you to deliver a stable and reliable system that meets their needs. By using established frameworks like Laravel or Symfony, you can leverage the years of development and testing that have gone into these projects to create a more stable system.&lt;/p&gt;

&lt;p&gt;Of course, that's not to say that building your own framework is a waste of time. On the contrary, building your own framework can be an incredibly rewarding experience that teaches you a lot about web development. Just don't expect to use it for production applications. Instead, use it as a learning tool to gain a deeper understanding of how frameworks work.&lt;/p&gt;

&lt;p&gt;In conclusion, building your own framework can be a valuable learning experience, but it's not the best way to build software. Instead, use an established framework like Laravel or Symfony for your production applications. By doing so, you can focus on building great applications without getting bogged down by framework maintenance.&lt;/p&gt;

</description>
      <category>programming</category>
      <category>webdev</category>
      <category>discuss</category>
    </item>
  </channel>
</rss>
