MongoDB vs. MySQL in Symfony 3 demo application

mod_proxy_fcgi with Apache 2.4 and PHP 7.1

Posted by Lukasz D. Tulikowski on January 10, 2017


Demo of application

Here are demos of Task List application based on MySQL data storage and MongoDB database.

MySQL demo

mysql site screen 

Mongo demo

mongo site screen 

Abstraction

There are many tutorials, presenting how to create a simple application which shows possibilities and powers of given solution.

Most of them are assuming a single model approach. While in reality we often choose between technologies and connect them. However, how one solution work with another? In most cases, this is a real enigma.

If we have seven well-known and PHP frameworks Laravel Symfony CodeIgniter Yii 2. Phalcon CakePHP Zend Framework

Moreover, database solution like MySQL, MariaDB, Percona, PostgreSQL which are full of them, it gives us 35 possibilities to explore.

Why Symfony 3?

I am familiar with Symfony. It comes with Doctrine and FOSUserBundle which I used in this project. The current version I used was Symfony 3.2.1

Configuration of Virtual Hosts

<VirtualHost *:80>
    ServerName mongo-todolist.tk
    ServerAlias www.mongo-todolist.tk
    SetEnvIfNoCase ^Authorization$ "(.+)" HTTP_AUTHORIZATION=$1

    <FilesMatch \.php$>
        SetHandler proxy:fcgi://127.0.0.1:9000
    </FilesMatch>

    DocumentRoot /var/www/mongo-todolist/web
    <Directory /var/www/mongo-todolist/web>
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog /var/log/apache2/mysql-todolist_error.log
    CustomLog /var/log/apache2/mysql-todolist_access.log combined
RewriteEngine on
RewriteCond %{SERVER_NAME} =mongo-todolist.tk [OR]
RewriteCond %{SERVER_NAME} =www.mongo-todolist.tk
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]
</VirtualHost>

NoSQL database servers are claimed to be faster, much faster. It’s not a concern in most of the cases. With 10 000 records difference between MySQL and MongoDB in insignificant. To show it, I’ve populated both databases with around 500 000 records and written a simple @TODO List application [link].

Performance of MySQL

Sources are available in [@github].

The database has two tables: Users, Tasks and about ~850 000 rows.

mysql performance chart

Statistics for MySQL

Avg. total execution time = 193 ms
Avg. database queries time = 181 ms
Avg. Symfony overhead = 12 ms

Performance of Mongo

Sources are available on [@github].

The database has two collections: Users, Tasks and about ~810 000 rows.

mongo performance chart

Statistics for Mongo

Avg. total execution time = 25 ms
Avg. database queries time = 44 ms
Avg. Symfony overhead = 19 ms

Summary

In this case, the application using MySQL as data storage is about eight times slower, in favor of one handled with MongoDB. It is worth to notice that Symfony needs a little bit time to render the whole page when NoSQL storage is applied.

These results are not very accurate but measured in the conditions which reflect Live Application Environment.