I’m using tinyrss as an RSS reader software which is running on my raspberry pi. While its performance is acceptable I’m always wanting it to be a little snappier. Judging from watching top output the pi is most of the time busy with the
mysql process. I already tried every hint in the web to make mysql faster. I switched from InnoDb to MyISAM (that helped) and tuned the buffers.
I suspected one of the reasons for the slowness the small usb disks on which the mysql databases are saved. That disks are not fast and using the slow USB 2.0 interface. So what would be a faster disk which would not be bottlenecked by the usb interface?
As even the PIs card reader is quite slow and even the ethernet adapter is connected internally via USB I can only think of one faster storage space: the RAM. A ramdisk would provide the fastest storage the pi can offer.
Why a Ramdisk/Disk RAID-1
According to this (german) Heise article it is possible to run a fast ssd in conjunction with a slow hdd in a RAID-1. Readings would be provided by the fast disk. The write speed is limited to the speed of the lower disk. This way you can save your money for just one SSD and use a cheap HDD as the backup disk. I already installed such a setup successfully on desktop pc.
My idea was to use a ramdisk as the fast disk and a slow partition on a usb stick as the slow write disk. The advantage of this setup would be that you have the speed of the RAM when reading data but still can be sure that your data is saved to mass storage on the usb partition. You do not have to worry about restarts and crashes, the ramdisk would be filled with the latest data from usb disk at each startup. Pretty nice. So lets try it out.
Building a Ramdisk/Disk RAID-1
As the PI does only have 512 MB of RAM there is not much room for a ramdisk. However, was my tinyrss database is only 70 MB in size and usually not more than 100 MB of RAM are used in the pi – there is some RAM left that can be used for experiments. I decided to spent 250 MB of RAM for the ramdisk.
The first thing you need is a ramdisk. You cannot use tmpfs as it does not provide a block device. Instead of that you have to use the /dev/ram* devices. By default they are only 4 MB in size. This must be changed by kernel boot parameter.
Add this option to the command line in /boot/cmdline.txt
After reboot the ramdisks provide 250 MB of space.
Let’s check the speed of the ramdisk:
sudo hdparm -t /dev/ram0 /dev/ram0: Timing buffered disk reads: 250 MB in 2.35 seconds = 106.54 MB/sec
Well, a PC can do much more, but compared to the usb disk it is really fast.
sudo hdparm -t /dev/sda /dev/sda: Timing buffered disk reads: 70 MB in 3.03 seconds = 23.12 MB/sec
Moreover in the ramdisk file access should be more or less instant what is even more important.
Now lets create the raid. I used a small partition on an usb stick as sda1.
sudo mdadm --create -n 2 --level=1 /dev/md0 /dev/ram0 --write-mostly /dev/sda1
And now lets check the read speed on the newly created raid:
sudo hdparm -t /dev/md0 /dev/md0: Timing buffered disk reads: 202 MB in 3.03 seconds = 66.73 MB/sec
Well, much faster than the usb partition alone!
Running mysql from ramdisk/usb raid-1
Afterwards you have to create file system on the raid device /dev/md0 and mount it. I copied the database files from my usb disk to the raid device and pointed the mysql data directory to there.
After restarting the mysql server I did some benchmarks whether tinyrss runs faster in this setup.
Interesting idea, but not faster
In the end the database running from the raid was a little bit slower than when running from the usb disk. So my experiment was a failure.
The mysql database is clearly CPU and not I/O bound. As the data access via raid requires more CPU power it is obvious that the raid slowed things down. The faster access thanks to the ramdisk does not matter.
For this case I gave up the idea of a ramdisk/disk raid-1 because it did not make anything faster. However, there might be other use cases which are not CPU bound. In that cases this might be an idea worth to try out.
Having instant access to your data in RAM and having it written onto a harddisk at the same time might be interesting for some people out there.
Great idea. Did you experience any differences in serving web content like php.
Yes, as described serving web content was actually a little slower.
The slowness of mysql on my Pi was caused by missing cpu power, not slowness of the I/O. As the raid layer uses some more CPU power, it is logical that the application runs slower.
However, this is the result for my use case. There may be use cases which are really I/O limited and in that case, my setup might speed things up.
I did understand that you use mysql which needs CPU power and thus slows down the system while the RAMDISK cant outperform as it can not support mysql access.
However I was wondering if just plain html services or php services can outperform better. Assuming html is just taking the file from disk and pushing it into the ethernet socket your RAMDISK should have played a more important role and thus perform better than just serving the file from the usb-stick. right?
Serving plain files should indeed be faster with that ramdisk. I have not tested though.
(Note that a plain RAM-disk which is filled at bootup would be a better choice instead of a raid when you have static files only)
Mysql can access a ramdisk/raid, just the raid setup was not faster because of cpu-limited database operations.