The transition from cutting-edge curiosity to practical workhorse is not one that many technologies make. Yesterday’s precocious upstarts often fail to live up to their Version 0.1 promise -- not so for the technologies that make up the fiercely acronymized MEAN stack.
The answer is when the simplicity and common structure make your life easier. MongoDB offers a more flexible, accommodating layer for storing data. Node.js provides a better nexus for running your server, while Express.js helps standardize how you build your websites. On the client, AngularJS provides a clean way of adding interactive functions and AJAX-driven rich components. Put them all together and they make a clean, coherent mechanism for moving data from user to disk farm and back again.
The real explanation, however, is deeper. Here we offer nine reasons to give MEAN a shot with your next project. Not everyone has the time or budget to toss out and recode the old in the latest, trendiest framework, nor should you throw away the rock-solid reliability of battle-tested tools like Apache, MySQL, or PHP. But for green-field projects that could benefit from flexibility, simplicity, and performance, going MEAN may make your life better than you think.
MongoDB is built for the cloud
If your Web app plans include making good on the pennies-per-CPU promise of the cloud, the MEAN stack offers a compelling database layer in MongoDB. This modern database comes equipped with automatic sharding and full cluster support, right out of the box. Plug in MongoDB and it spreads across your cluster of servers to offer failover support and automatic replication. Given the ease with which apps can be developed, tested, and hosted in the cloud, there’s little reason not to consider MongoDB for your next project.
MySQL's structure is confining (and overrated)
Anyone who has developed or maintained a LAMP-based app for any amount of time knows that MySQL’s strength as a relational database can feel a bit imprisoning at times. Like all relational databases, MySQL forces you to push your data into tables. This isn't a problem if every single entry fits into exactly the same format, but how often is the world that generous? What if two people share the same address but not the same account? What if you want to have three lines to the address instead of two? Who hasn’t tried to fix a relational database by shoehorning too much data into a single column? Or else you end up adding yet another column, and the table grows unbounded.
MongoDB, on the other hand, offers a document structure that is far more flexible. Want to add a new bit of personal information to your user profiles? Simply add the field to your form, roll it up with the rest of the data in a JSON document, and shove it into your MongoDB collection. This is great for projects in flux and for dealing with data that may ultimately prove tricky to constrain in table form.
Disk space is cheap
Among the great revelations of relational databases was the JOIN command. With JOIN, we could save disk space by removing repeated fields like city, state, and ZIP code. By storing this frequently accessed and repeated data in separate tables that can be included in future results through a JOIN, we keep our database tidy and our disks slim.
But JOINs can be tricky for some and hard on RAM, and though it's still a good idea to isolate and access data in separate tables through JOINs, there's not as much need to save disk space now that disk drives are measured in multiple terabytes. The space is so cheap that some database designers end up denormalizing their data because the JOINs are too slow. Once you do that, you don't need a relational database as much. Why not use MongoDB instead?
Node.js simplifies the server layer
Navigating the various layers of the LAMP stack can be a difficult dance of many hats, one that has you shuffling through various config files with differing syntax. MEAN simplifies this through use of Node.js.
MEAN makes code isomorphic
AngularJS and MongoDB both speak JSON, as do Node.js and Express.js. The data flows neatly among all the layers without rewriting or reformatting. MySQL's native format for answering queries is, well, all its own. Yes, PHP already has the code to import MySQL data and make it easy to process in PHP, but that doesn't help the client layer. This may be a bit minor to seasoned LAMP veterans because there are so many well-tested libraries that convert the data easily, but it all seems a bit inefficient and confusing. MEAN uses the same JSON format for data everywhere, which makes it simpler and saves time reformatting as it passes through each layer. Plus, JSON’s ubiquity through the MEAN stack makes working with external APIs that much easier: GET, manipulate, present, POST, and store all with one format.
Node.js is superfast
Apache was great, but these days, Node.js is often flat-out faster. A number of benchmarks show that Node.js offers better performance, while doing much more. Perhaps it's the age of the code. Perhaps the Node.js event-driven architecture is quicker. It doesn't matter. These days, especially among impatient mobile device users, shaving even milliseconds off your app’s performance is important and Node.js can do that, while offering a Turing-complete mechanism for reprogramming it.
PHP lovers like to cling to the great libraries of code that are built for dominant platforms like WordPress or Drupal. They have good reasons to be proud, but their advantages are evaporating as Node.js catches up.
The Node.js package manager, Npm, makes it even easier to share code, and the public repositories targeting Node.js are growing quickly. While the PHP crowd may lead at this moment in time, the future may favor Node.js. Plus, incumbents often prove to be brittle in the face of shifting trends. Each attempt to modernize an entrenched platform like Drupal with a new version means that many more developers may be letting their eyes wander toward the newer, more nimble platforms built around Node.js.
AngularJS is fresh
It's not exactly fair to compare the "A" in "MEAN" with anything in the LAMP stack because LAMP doesn't include an analog. If you want to do anything on the client side, you're on your own. Sure, there are plenty of good PHP-based frameworks that work with MySQL, but each is a bit different and moving in its own direction. WordPress, Joomla, and Drupal, for example, offer differing strategies, and it's hard to switch between them, let alone port code from one to the other. Anointing one client framework adds consistency and stability.
Mix and match
Of course, if you're really picky, there's no reason why you can't mix it up a bit. Plenty of developers use MongoDB with Apache and PHP, and others prefer to use MySQL with Node.js. AngularJS works quite well with any server, even one running PHP to deliver data from MySQL. You don't have to be a slave to the acronyms.
- Free course: Get started with AngularJS
- Download: The professional programmer's business survival guide
- Download: 29 tips for succeeding as an independent developer
- Master single-page Web apps with AngularJS
- Building microservices with Node.js
- 7 reasons why frameworks are the new programming languages
- 7 timeless lessons of programming 'graybeards'
- 9 cutting-edge languages worth learning now
- 10 battles raging for the hearts and minds of developers
- Attack of the one-letter programming languages
- 15 technologies changing how developers work
- 12 predictions for the future of programming
- 15 hot programming trends -- and 15 going cold
- 10 capabilities we want to see in HTML6
This story, "MEAN vs. LAMP for the future of programming" was originally published by InfoWorld.