What does the sync command do?

Solution 1:

Any hard disk is orders of magnitude slower than your RAM, so linux uses any spare RAM you might have floating around to cache filesystem data. However, this should really never cause performance issues unless there is either something wrong with your hard disk, or the services on your server are attempting to write data at such a high rate for so long than the server cannot possibly cache or retrieve the data. It could also be a sign that your hard disk is reaching the end of its lifespan.

Anyhow:

  • running man sync will tell you what sync does [flushes FS buffers]
  • googling 'linux drop_caches' will tell you that echoing the number 3 into it releases all unneeded memory pages from the cache [this should not be necessary on a healthy system]
  • command1 && command2 breaks down to 'if command1 finishes successfully then run command2'
    • the partner for this is command1 || command2 aka 'if command1 fails then run command2'

The command you were given is a temporary fix at best and is symptomatic of something else wrong with your system. Either your disks are at their end-of-life, or your system is too underpowered for what you're doing with it, or both.

Solution 2:

AWS is not for the faint-hearted, and you just encountered one of the reasons why. The poor disk I/O situation on AWS is well-known, and one of the major factors to consider for anyone building an application on top of it. There are disk-optimized instances and a few other tricks (like building a RAID 0 out of EBS volumes) that you can try to improve matters. Make sure to use larger instances (at least m1.large) to ensure that the kernel can buffer disk I/O.