First of all, there's almost no need to ever defragment an ext4 filesystem. No, really, we mean it!
Are you using an SSD?
If you're running ext4 atop an SSD, you never want to defrag it under any scenario. This will actually hurt the performance and longevity of the drive and will never, ever produce a speed performance. When your data is stored onto the SSD's NAND, it's done so in an almost RAID-esque way; split into small pieces and balanced over many cells and NAND packages. It's already technically fragmented, but for the purpose of ensuring the best possible read/write speed, and it's balanced to keep a healthy write wear-levelled state of the drive. So, never defrag an SSD under any circumstance.
Are you using a HDD?
Spinning hard disks are naturally very slow - sometimes by thousands of times when compared with the latest NVMe SSDs. There is unfortunately very little that can be done to speed up HDDs, even if they are highly fragmented. Only under very particular scenarios will defragmenting make a difference; for example, if you have a directory with thousands of small files that have become highly fragmented. Defragmenting this particular directory may improve performance slightly, but we're talking by a handful of IOPS here.
Are you using RAID?
Defragmenting a filesystem atop RAID is usually a bad idea - especially so if you're running with a parity-based RAID setup such as RAID5, RAID6 or a ZFS RAIDZ. Parity-based RAID already splits up your data into small chunks and stores them around the underlying drives. The files are already fragmented and there's no way to defragment them in this scenario, so no need to try.
If, however, you're using basic RAID1 (or ZFS mirror) with 2 drives, this is theoretically the same as running a single disk. So, if you have a directory with many smaller files that has become highly fragmented and you're using spinning hard disks, you may get a small performance improvement by defragmenting these particular directories.
How to actually defragment?
If you're still reading despite all of the warnings and advice, then this is how you can actually defragment an ext4 filesystem.
First, you'll need to install the e4defrag package. This is included in e2fsprogs which most Linux distros have available for install:
apt install e2fsprogs
via a Debian-based distro, or
yum install e2fsprogs
via a RHEL-based distro
Next, you'll want to check the current fragmentation level of your particular directory with the -c flag, as below:
e4defrag -c /path/to/dir
It might take some time to run, especially on a large directory and/or slow filesystem. Once completed, e4defrag will tell you whether it thinks you should defragment. Pay attention to its output, as usually this advice is best. If it says so, or you really feel that it will make a difference, you can run a real defrag by dropping the -c flag, like so:
e4defrag /path/to/dir
Again, this may take a long time, so have some patience. Once completed, hopefully you will find some slight improvement in your random disk performance.