Does Matomo work in a load balanced environment? Multiple servers high scale tracking
Many medium-high traffic websites successfully use Matomo (Piwik) to track and analyse their website behavior and performance. See this FAQ for general information about high traffic Matomo.
If you want to provide high reliability and better performance in your Matomo setup, you can load balance Matomo services on various servers. It is pretty easy to do so, but make sure you read the following documentation.
Matomo stores all tracking data, reports, users, settings, etc. in the shared mysql database. Matomo also needs to read/create files to store information, caches, temporary files:
- config/config.ini.php is used by every request to Matomo. It contains db access, plugins enabled, various settings, etc. The file is only modified when the Super User changes some settings in the Admin interface (activating plugins, changing smtp settings, etc.). This file must be synchronized on every Matomo server.
- tmp/* contains temporary and cached files such as: compiled templates, generated PDFs, tracking caches, etc. This directory tmp/ content can be safely deleted and Matomo will automatically recompile templates and recreate caches.
- by default, Matomo uses file session storage, which are usually stored in tmp/sessions, or in the global sessions directory (see php.ini). There is a config setting to enable database session storage, see this faq
When Matomo is used behind a load balancer, here are the steps to follow:
- synchronize config/config.ini.php on all servers – make sure the file config/config.ini.php is the same on all servers (using rsync for example).
- upload all the Matomo files and all plugins to all servers, as it is important that all your servers run the same codebase with all features, including any free or premium plugin from the Marketplace.
- enable database session storage (If you use 3.7.0 or later it should already be enabled by default)
- in your
config.ini.php, below the section
[General], add the following line:
multi_server_environment=1. This setting lets Matomo know you’re using multiple servers and for example it will then not allow the installation of a plugin via the UI (or the plugin would be only installed on one server), or it will not allow config file changes from the UI (as the config file would be updated on one server only).
- we highly recommend to enable SSL in Matomo
Matomo should then work as expected in a load balanced environment. For example, a common way to load balance Matomo on 3 servers: Load balancer in front of (2x) webservers which are tracking/querying the (1x) Master mysql DB server. For further optimization, a (1x) UI webserver can be added, to handle all API requests, UI requests, and run the cron archiving. This server can also be doubled and load balanced.
Typically, webserver boxes would be cheap to operate (trackers, frontends and archivers), and the Master DB server would be larger (eg. multiple cores, 16G memory, fast disk access), which could scale to a certain point (100M hits per month is achievable and some users even track around 1 billion hits per month). Get expert support and help from the Matomo team.
After Maintenance mode is enabled,
index.php endpoint will continue return
200 as HTTP status code as response instead of
Learn more: How to configure Matomo for speed.
If your load balancer is used as a SSL proxy, make sure to read our FAQ: How do I configure Matomo when the server is installed behind a proxy?