Saturday, May 9, 2020

Read-a-head settings in Cassandra/MongoDB

Correlated with mongodb & Cassandra database:

Readahead setting is a kernel feature (and is not process dependent). It improves file reading performance. It attempts to read the blocks from the file into the memory before the application reads them if the file is being read sequentially.

Verify if you are running rotational hard disk or SSD's:

cat /sys/block/sda/queue/rotational
It returns '1' for hard disks and '0' for SSD's

To know current readahead settings:
Run: blockdev --getra /dev/sda

Replace sda with corresponding disk/volume group.
Example: 'lsblk' will display all available mounted or un-mounted disks.

For SSD's:
Edit /etc/rc.local file
vi /etc/rc.local

Add following lines in that file, for respective disks

In case of Cassandra, set to 8KB as recommended by datastax.
touch /var/lock/subsys/local
echo 0 > /sys/class/block/sda/queue/rotational
echo 8 > /sys/class/block/sda/queue/read_ahead_kb

In case of MongoDB, set to 256KB as suggested by mongodb.
touch /var/lock/subsys/local
echo 0 > /sys/class/block/sda/queue/rotational
echo 256 > /sys/class/block/sda/queue/read_ahead_kb

Above, we can ignore "echo 0.." as we know those are SSD's from first command.

Restart the nodes to take effect.

For runtime:
blockdev --setra 256 /dev/sda

Here we are setting to sda volume group. Change according to your use.

For persistance through reboots, add this command to boot script /etc/rc.local(create if its not there) or put in mongodb/cassandra init script.


References: 
https://serverfault.com/questions/650141/readahead-for-var-lib-mongo-is-set-to-4096kb

https://docs.datastax.com/en/dse/5.1/dse-admin/datastax_enterprise/config/configRecommendedSettings.html

https://lwn.net/Articles/155510/

***