Applications usually do not write directly to the disk subsystem,(because is slow) but into cache or buffers.
The pdflush kernel threads then flushes out data in cache/buffers to the disk when it has time to do so (to keep sync the data in the cache/buffer with the data on the disk) or if a file size exceeds the buffer cache.
Swap is used when no more physical RAM is available, and also to store data that have not been used for a while even if there is enough RAM and we don't hit the limit( this is tunable )
1) When a process reads data from disk, the data is copied to memory.
a)The process and other processes can retrieve the same data from the copy of the data cached in memory.
2) When a process tries to change the data, the process changes the data in memory first.
a) Then it triggers pdflush, the dirty buffer should be synchronized to the data on disk as soon as
possible, or the data in memory could be lost if a sudden crash occurs
3) The flush occurs on a regular basis (kupdate) and when the proportion of dirty buffers in memory
exceeds a certain threshold (pdflush).
4) If all all physical memory is in use we will start using swap and therefore, feel a slowdown if accessing data.
Examples tunable for Swap and pdflush, this is only an example you need to investigate first the parameters that are good for your system on a test environment.
The swapiness parameter, defines how often the system will swap, the higher a number set here, the more the system
will swap to tune, simply echo a value from 0 to 100 onto /proc/sys/vm/swappiness.
Example sysctl -w vm.swappiness = 100
The dirty_background_ratio defines at what ratio of main memory (10% default) we want to write data to the disk
to the disk, by using this tunable we can achieve pdflush daemon to cause less frequent crashes.
Example sysctl -w vm.dirty_background_ratio=25
As in the example above, the tunable is the 10% of the system RAM, a value of 10 would mean that data will be
written into system memory until the file system cache has a size of 10% of the server’s RAM.
Example sysctl -w vm.dirty_ratio=20