Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Surajshetty's avatar

MongoDB or PostgreSQL or MySQL For Big scaling application in Laravel

If you are building a SaaS application with multiple table structures and complex ones, kindly let me know which one to use.

0 likes
4 replies
LaryAI's avatar
Level 58

When deciding between MongoDB and MySQL for a SaaS application in Laravel that requires scaling and has complex table structures, there are several factors to consider:

  1. Schema Flexibility: MongoDB is a NoSQL database that offers a flexible schema. This can be beneficial if your application requires the ability to store unstructured data or if your data model is evolving.

  2. Scaling: MongoDB is designed with horizontal scaling in mind, using sharding to distribute data across multiple servers. MySQL traditionally scales vertically by adding more power to the existing server, although it also supports replication and sharding with additional configuration.

  3. Transactions: MySQL, being a relational database, supports ACID (Atomicity, Consistency, Isolation, Durability) transactions out of the box. MongoDB has introduced multi-document transactions as of version 4.0, but they are not as mature as those in relational databases.

  4. Data Relationships: If your application relies heavily on JOIN operations and complex transactions with multiple related tables, MySQL might be more suitable due to its relational nature. MongoDB can reference data between collections, but it's not as straightforward as with SQL JOINs.

  5. Ecosystem and Tools: MySQL has been around for much longer and has a vast ecosystem and a variety of tools for database management, monitoring, and performance optimization. MongoDB also has a good set of tools, but the ecosystem is not as extensive.

  6. Laravel Support: Laravel has native support for both MySQL and MongoDB (through third-party packages like jenssegers/laravel-mongodb). However, Eloquent ORM is primarily built for relational databases, and while you can use it with MongoDB, you might not be able to leverage all of its features.

  7. Performance: MongoDB can offer better performance for certain workloads, especially those involving large volumes of data and simple queries. MySQL may perform better for complex queries with joins and transactions.

Ultimately, the choice between MongoDB and MySQL should be based on the specific requirements of your application. If you need a flexible schema and plan to scale horizontally, MongoDB could be the better choice. If you rely on complex relationships and transactions, MySQL might be more appropriate.

Here's a simple example of how you might configure each database in Laravel:

For MySQL, you would set up your .env file with the following:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_database
DB_USERNAME=your_username
DB_PASSWORD=your_password

For MongoDB using the jenssegers/laravel-mongodb package, your .env file would include:

DB_CONNECTION=mongodb
DB_HOST=127.0.0.1
DB_PORT=27017
DB_DATABASE=your_database
DB_USERNAME=your_username
DB_PASSWORD=your_password

And you would need to install the package and configure it according to its documentation.

Remember that your choice should be informed by the specific needs of your application, and it's often helpful to consult with a database expert or conduct performance testing to make the best decision.

Tray2's avatar
Tray2
Best Answer
Level 73

It depends on what kind of data you have in your application, if it is relational data then you should use MySQL/MariaDB or PostgreSQL, if it isn't then you could use MongoDB, but in my experience most data should be related so I suggest a RDBMS.

Which you choose doesn't really matter since they are all quite similar, the biggest difference is that PostgreSQL has some other ways of doing certain things. However pick the one you are most comfortable with.

1 like

Please or to participate in this conversation.