Raspberry Pi optimizations that have made a significant difference in performance

Summary

Today with the help of Gemini AI, I ran through a number optimizations that have made a significant improvement in our Kolibri server system hosted on a RPi 400. I’d like to share them for others to benefit. In the process of doing this, I discovered that Kolibri-server was not running, rather the non-optimized version was running. I fixed that and then implemented the additional tweaks. I think one of the biggest gains in the tweaks may be from Step 4 - reduce the number of workers from 16 to 5, something I knew nothing about before this.

In our situation, we can have upwards of 20 tablets accessing Kolibri hosted on an RPi400. The service speed and consistency has really increased. Using htop to see the cores performance, the average load even with 15 tablets accessing content at the same time didn’t max out and was averaging less than 30% most of the time. The memory usage is remaining well below 1MB and we’ve not used any of the swap file.

Technical details

  • Version: 0.19.3
    OS: Linux-6.1.21-v8±aarch64-with-glibc2.31
    Python: 3.9.2
    Installer: deb kolibri-server - 0.5.1-0ubuntu1
    Server: nginx/1.18.0
    Database: /home/pi/.kolibri/db.sqlite3
    Free disk space: 549 GB
  • Hardware: Raspberry Pi 400, 4GB RAM, attached USB3 SSD 2TB as boot and host storage device; connected to the network via wired LAN which is distributed via a Unifi-based WLAN.

Step 1: Make sure the RPi is running “headless”

  1. Sudo raspi-config
  2. Use your arrow keys to navigate to System Options (or Boot Options, depending on your OS version) and press Enter.
  3. Select Boot / Auto Login.
  4. Choose Console or Console Auto-login.
  5. Select Finish and agree to reboot. [1]

Step 2: Increase the Swap Space

This prevents the Pi from crashing if multiple students trigger heavy tasks at the same time.

  1. Open the swap configuration file:

    bash

    sudo nano /etc/dphys-swapfile
    
    
  2. Find the line that says CONF_SWAPSIZE=100.

  3. Change the value to 2048 (for 2GB of swap) or 1024 (for 1GB). (I did 2048 swap size)

  4. Save the file by pressing Ctrl + O, then Enter.

  5. Exit by pressing Ctrl + X.

  6. Apply the changes by restarting the swap service:

    bash

    sudo systemctl restart dphys-swapfile
    

Step 3: Set CPU Governor to Performance

This locks the Pi 400’s quad-core CPU at its maximum 1.8GHz speed, preventing it from lagging when transitioning between idle and active student use.

    1. Install the CPU frequency utility package:

      bash

      sudo apt update && sudo apt install cpufrequtils -y
      
      
    2. Open the configuration file (it will likely be blank if newly installed):

      bash

      sudo nano /etc/default/cpufrequtils
      
      
    3. Paste the following line into the file:

      text

      GOVERNOR="performance"
      
      
    4. Save (Ctrl + O, Enter) and exit (Ctrl + X).

    5. Restart the service to apply:

      bash

      sudo systemctl restart cpufrequtils
      

Step 4: Configure Kolibri Server Workers (Note mine was set to the default 16. I changed it to 5)

Because you are using the dedicated kolibri-server package, worker settings are configured system-wide rather than in a user folder. The Pi 400 has 4 cores and 4GB of RAM, so 4 to 6 workers is the sweet spot.

  1. Open the system configuration file for the Kolibri server:

    bash

    sudo nano /etc/kolibri/kolibri.tmpl
    
    

    (Note: If this file does not exist, look for /etc/kolibri/config.json or check /etc/default/kolibri).

  2. Look for the worker count variable, usually named web_workers, UWSGI_WORKERS, or workers.

  3. Set the value strictly to 4 or 5. (I set mine to 5)

  4. Save and exit.

  5. Restart Kolibri to lock it in:

    bash

    sudo systemctl restart kolibri-server
    

Hi @cjackson,

Thanks for sharing! I’ll be looking at these to integrate them into the Raspberry Pi image also.

Kind Regards,

Richard

@richard : You’re welcome! I was surprised at the performance improvement!