Do you want to make it sassy AF without breaking the bank or sacrificing any goats?
To kick things off: why this matters. When your server is running low on resources (like RAM), it has to start swapping data in and out of its slower storage devices, like hard drives or SSDs. This can cause a lot of unnecessary I/O operations that slow down everything else you’re trying to do. On the other hand, if your cache pressure is too high, your server might be using up all available memory for caching data instead of actually running applications and processes.
So how do we strike a balance between these two competing needs? By tuning our system settings! Here are some tips that will make your RHEL server sassier than a catwalk model in stilettos:
1. Check Your Memory Usage
Before you start tweaking anything, it’s essential to understand how much memory your server is currently using. You can do this by running the following command:
# This script checks the memory usage of a RHEL server using the `free` command with the `-m` flag to display the results in megabytes.
# First, we need to import the `free` command from the system's default path.
#!/bin/bash
# Next, we need to specify the `-m` flag to display the results in megabytes.
free -m
# This command will display the total amount of free and used memory on the system, as well as the amount of memory used by the kernel and buffers/cache.
# It's important to check memory usage before making any changes to the server to ensure that there is enough available memory for the changes to be made.
This will show you a breakdown of your system’s RAM usage in megabytes (MB). If you see that your “Mem Free” and “Buffers/Cached” values are consistently low, it might be time to increase your memory allocation.
2. Adjust Your Swap Settings
Swapping is the process by which data is moved from RAM to a slower storage device when there’s not enough available memory. By default, RHEL will use swap space if your system runs out of physical memory (RAM). However, this can lead to performance issues and slow down your server significantly. To avoid this, you can adjust your swap settings by editing the `/etc/sysctl.conf` file:
# This line uses the `sudo` command to run the `nano` text editor and open the `/etc/sysctl.conf` file for editing.
sudo nano /etc/sysctl.conf
# This line adds a comment to explain the purpose of the following code segment.
# The following lines adjust the swap settings to prevent performance issues caused by using too much swap space.
# This line uses the `echo` command to print a message to the terminal.
echo "vm.swappiness=10"
# This line uses the `sudo` command to run the `tee` command with the `-a` flag to append the output to the `/etc/sysctl.conf` file.
# The `tee` command allows us to write to a file while also displaying the output in the terminal.
# The `echo` command is used to print the `vm.swappiness=10` message to the terminal.
# The `sudo tee` command then appends this message to the `/etc/sysctl.conf` file.
sudo tee -a /etc/sysctl.conf <<< "vm.swappiness=10"
# This line uses the `echo` command to print a message to the terminal.
echo "vm.vfs_cache_pressure=50"
# This line uses the `sudo` command to run the `tee` command with the `-a` flag to append the output to the `/etc/sysctl.conf` file.
# The `tee` command allows us to write to a file while also displaying the output in the terminal.
# The `echo` command is used to print the `vm.vfs_cache_pressure=50` message to the terminal.
# The `sudo tee` command then appends this message to the `/etc/sysctl.conf` file.
sudo tee -a /etc/sysctl.conf <<< "vm.vfs_cache_pressure=50"
# This line uses the `sudo` command to run the `sysctl` command with the `-p` flag to reload the `/etc/sysctl.conf` file.
# The `sysctl` command is used to apply changes to the kernel parameters.
sudo sysctl -p
# This line uses the `echo` command to print a message to the terminal.
echo "Swap settings have been adjusted successfully."
Add or modify these lines (depending on what works best for your system):
#!/bin/bash
# This script is used to modify the virtual memory settings of the system.
# Set the swappiness value to 10.
sysctl vm.swappiness=10
# Set the vfs_cache_pressure value to 50.
sysctl vm.vfs_cache_pressure=50
# Set the min_free_kbytes value to 262144.
sysctl vm.min_free_kbytes=262144
# Set the max_map_count value to 262144.
sysctl vm.max_map_count=262144
# Explanation:
# The "sysctl" command is used to modify kernel parameters at runtime.
# The "vm.swappiness" parameter controls the tendency of the system to swap out memory.
# A lower value means the system will try to avoid swapping as much as possible.
# The "vm.vfs_cache_pressure" parameter controls the tendency of the system to reclaim memory that is used for caching of directory and inode objects.
# A lower value means the system will be less aggressive in reclaiming memory.
# The "vm.min_free_kbytes" parameter sets the minimum amount of free memory that should be available to the system.
# The "vm.max_map_count" parameter sets the maximum number of memory map areas a process can have.
The `vm.swappiness` setting controls how aggressively your system uses swap space, with a value of 0 being the most aggressive and a value of 100 being the least aggressive (default). The `vm.vfs_cache_pressure` setting determines how much memory is allocated for caching file metadata, while the `vm.min_free_kbytes` and `vm.max_map_count` settings control how much free RAM your system should keep available at all times.
3. Monitor Your Cache Pressure
To monitor your cache pressure in real-time, you can use the `watch` command to run a script that displays your current memory usage:
bash
# This script uses the `watch` command to monitor cache pressure in real-time.
# It displays the current memory usage by calculating the percentage of cached memory.
# The `watch` command runs the script every 1 second using the `-n` flag.
watch -n 1 'echo "Cache Pressure: $(free -m | awk '/Cached/ {print int($3/$2*100)}')%"'
# The `echo` command prints the output to the terminal.
# The `free -m` command displays the current memory usage in megabytes.
# The `awk` command searches for the line containing "Cached" and prints the third column, which represents the amount of cached memory.
# The `int` function converts the value to an integer.
# The `$(...)` syntax allows the output of the command to be used as a variable in the `echo` command.
# The `watch` command continuously runs the script every 1 second.
# The `-n` flag specifies the interval in seconds.
# The single quotes encapsulate the entire script to prevent the command substitution from happening before the `watch` command is executed.
# The backslash before the single quotes in the `awk` command is used to escape them and prevent them from being interpreted as the end of the single quotes encapsulating the script.
# The `echo` command displays the output in the format "Cache Pressure: XX%", where XX is the calculated percentage of cached memory.
This will display the percentage of cache pressure in real-time, which can help you identify any issues before they become a problem. If your cache pressure is consistently high (over 95%), it might be time to increase your memory allocation or adjust your swap settings again.
4. Tune Your Kernel Parameters
Finally, you can tune some kernel parameters that control how your system allocates and manages its resources:
# This script is used to tune kernel parameters for resource management on a Linux system.
# Set the dirty_background_ratio to 10% to control how much dirty data can be present in the system before it triggers a writeback process.
sudo sysctl -w vm.dirty_background_ratio=10
# Set the dirty_writeback_centisecs to 5000 milliseconds to specify the interval between two writeback processes.
sudo sysctl -w vm.dirty_writeback_centisecs=5000
# Set the min_free_kbytes to 262144 kilobytes to specify the minimum amount of free memory that should be available on the system.
sudo sysctl -w vm.min_free_kbytes=262144
# Explanation:
# The first command sets the dirty_background_ratio to 10%, which means that when the amount of dirty data in the system reaches 10% of the total available memory, it will trigger a writeback process to flush the data to disk.
# The second command sets the dirty_writeback_centisecs to 5000 milliseconds, which means that the system will wait for 5000 milliseconds before triggering another writeback process.
# The third command sets the min_free_kbytes to 262144 kilobytes, which means that the system will always try to keep at least 262144 kilobytes of free memory available for use. This helps prevent memory shortages and improves overall system performance.
The `vm.dirty_background_ratio` setting controls how much dirty data (data that needs to be written back to disk) is allowed in the background, while the `vm.dirty_writeback_centisecs` setting determines how often your system should flush its write-behind cache. The `vm.min_free_kbytes` setting controls how much free RAM your system should keep available at all times (same as before).
And that’s it! By following these tips, you can optimize your RHEL server for maximum sassiness and performance without breaking the bank or sacrificing any goats.