Why we must rewrite Magento 2 on NodeJS

Yegor Shytikov
8 min readAug 13, 2020
  • Magento is full of boilerplate code which obscures the programmers intention
  • Magento 2 has legacy ORM and framework based on outdated Zend 1 components.
  • Magento 2 teaches a lesson: papering over complexity creates more complexity
  • Magento was a “design by committee” project that covers everything of everything in the eCommerce application development, hence is exceedingly complex slow and buggy.
  • The experience of Magento programming is it’s ok until it isn’t, until that day an obscure impossible-to-understand Exception appears out of the depths of a stack trace of Aspect-oriented plugin interceptors you've never heard of requiring 3+ days just to figure out the problem. Magento 2 has huge stack traces. They go on for several screens full of abstract method this and abstract method that.
  • What is the overhead required in the framework to allow coders to write zero code?
  • You need PHP Storm IDE with custom plugins to work with Magento codebase, they are a symptom of Magento complexity
  • Node.js was the result of one guy honing and refining a vision of a lightweight event-driven architecture until Node.js revealed itself
  • The JavaScript community seems to appreciate removing boilerplate allowing the programmers intention to shine
  • The solution for Callback Hell, the async/await function, is an example of removing boilerplate so the programmers intention shines
  • Coding with Node.js is a joy
  • The npm/yarn package management system is excellent and joyful to use, versus the abomination that is Composer
  • Node.js offers excellent performance, running counter to the myth that JavaScript is slow and therefore Node.js performance must be bad
  • Node.js performance is riding the coattails of Google investment in V8 to speed up the Chrome browser
  • The intense competition between browsers is making JavaScript more and more powerful every year, benefitting Node.js

There are popular Magento 2 Data layer implementations on Node.Js

NodeJS implementation of the Magento 2 ORM and Microservice Framework components without using legacy PHP.

NodeJento it is a NodeJs service that provides an additional API surface that makes product and category and any other data retrieval faster.

PHP vs. Node.js

Earlier, PHP and JavaScript supported many essential segments of the internet. In those days, PHP handled all the server-related tasks while JavaScript managed browser-related details. Later on, with the emergence of technology, it has become possible to run JavaScript on the server. After this advancement, Node.js came into the picture. And, in the meanwhile, PHP’s popularity had been fading especially for Enterprise. The way Node.js has been evolving shows that the JavaScript framework is popular among developers. This leads to the re-designing and re-writing of the legacy Magento PHP code base. Now, PHP is not the same as it used to be in its earlier days. It has improved a lot.

Some developers are fascinated with Node.js because of JavaScript simplicity, while some others are happy with the legacy code base and long-term stability (no improvements) of PHP. What is PHP?

What is Node.js?

Node.js is a free and open-source platform that runs on JavaScript to develop web-based applications. It is built on Chrome V8 JavaScript engine (the same engine that is used by the Google Chrome web browser) that compiles the JavaScript code much faster. It uses event-driven architecture and non-blocking I/O capability that makes it suitable for building highly scalable applications.

Some facts about Node.js

  • Node.js was developed by Ryan Dahl in 2009
  • Unlike PHP, Node.js doesn’t need a separate web server(Apache, Nginx).
  • It uses npm package manager- the largest software registry in the world.
  • Tech-giants including IBM, GoDaddy, PayPal, LinkedIn and Netflix are also adopting Node.js.

PHP has a sound code base while Node.js has more modern features

Popular platforms for building websites (including Drupal, Joomla, and WordPress) are written in PHP. PHP has a deep legacy codebase. However, for better performance, you can’t optimize the Magento 2 PHP Core code. It is too bloated and overengineered using bed software development practices based on deprecated Zend Framework 1 library.

On the other hand, Node.js has modern features and interfaces. With its features like Node streams, module caching, event-driven architecture, it is capable of handling multiple client requests simultaneously with ease. It has a modern syntax that helps developers to easily configure and extend the application. But as compared to PHP, Node.js developers must have a good understanding of callback functions and closures.

Look at the given chart. It displays the total number of concurrent requests per second.

From this chart, we can see that Node.js handles multiple concurrent requests in less time as compared to PHP. However it is the performance of the Raw PHP Magento has a really slow framework and can handle just sing request at a time.

An example to understand how coding strategy differs in PHP and Node.js

Suppose you want to add new sections into your website. In Magento, you just need to open PHP tags and start writing codes in the PHTML files(called mixing code with HTML content). Due to this, it becomes difficult to maintain and extend the code in PHP.

But Node.js doesn’t follow the ‘mixing code with HTML content’ approach. It implements a Model View Controller (MVC) architecture in which the view level (user interface layer) is separated from the logical structure (business logic). This, in turn, helps developers to write a more organized and maintainable code that can be easily extendible in the future.

2. PHP/Magento is synchronous while Node.js is asynchronous

Before delving into this point, let’s understand what synchronous and asynchronous program code means.

  1. Synchronous: In this, one line of the code is executed at a time. The next line of code is executed only when the previous line of code has been executed completely.
  2. Asynchronous: In this, the entire code is executed at once. It means two threads can run parallelly and are independent of each other. They don’t interrupt each other at the time of execution.

The given image shows how execution time is faster in the asynchronous program code than the synchronous program code.

Unlike Node.js, no client app is needed in PHP (they build everything on the server)

PHP is specifically designed for simple web-based applications that includes all basic functionalities to manage servers, HTML and databases (especially MySQL). In PHP, all web pages can be easily rendered and generated on the server (reducing the overload on client side),eradicating the need to bother about Javascript in the browser. , Node.js is a great foundation for heavy server-side rendering, complex web-based apps and rich client-side PWA functionalities. It is suitable for applications that require constant client-server interaction.

Application Scenarios for PHP and Node.js

PHP

  • Simple Content Management Systems (WordPress or Drupal)
  • Applications that use LAMP stack. If you don’t know what LAMP stack is, then here is some basic information for you:

What is a LAMP stack?

  1. It is a set of open-source application, used to create web-based applications.
  2. LAMP stands for- Linux operating system, the Apache HTTP Server, the MySQL relational database management system, and the PHP programming language.

Note- PHP is the best solution if your client prefers a shared hosting service instead of full-fledged virtual cloud instance. It is a portable web application solution that can run on almost any server (Apache, Nginx) and any operating systems. Thus, it makes the deployment and integration process easier.

Node.js

  • Building PWA, single-page applications, real-time chats, collaboration apps, MAgento microservices, IoT applications, streaming apps and more.
  • Developing highly scalable server-side applications (with its non-blocking I/O and event-driven architecture).

Why NodeJS for Magento ??

>Because it is popular
Simple right? Lots are people (and companies) are interested in node js, it has the fastest-growing communities (better SO questions to copy and paste solutions) and largest library counts.

> Asynchronous IO
As told earlier, Async IO is one of the most desired features among other communities. Node is built to handle asynchronous I/O from the ground up and is a good match for a lot of common web and network-development problems. In addition to fast JavaScript execution, the real magic behind Node.js is called the Event Loop. To scale to large volumes of clients, all I/O intensive operations in Node.js are performed asynchronously.

Queries for some of the newer databases, like CouchDB, are written in JavaScript. Mixing Node.js and CouchDB requires no gear-shifting, let alone any need to remember syntax differences.

Why does it matter? Because Async IO improves the performance of a system considerably.

> Language

Node is all Javascript, so you don’t need different languages for front-end and back-end. So more dependency on “the server guy”. If you know the language and the flow, you can too fix the problem. That’s where a new term is coined, called “Full Stack Developer”, yeah, Node js is the culprit.

> Simplified Build processes
I love Magento, I am a fan of it. Magento has revolutionized eCommerce development. But there is just one issue. I have to write configurations in XML, I mean seriously, XML is was not designed to support programming logic.
With Node js, you can do it all, knowing just one language.

> JSON
When database spits out data, developers go to an extent converting those tuples into meaningful objects, remember all those ORM mappings, GraphQLs. Well, don’t need to do anything with node, the result comes in JSON format.

Clearly, Json is the first-class citizen in node js. Not quite correct when I talk about other languages, of course, there are mappers, but then are they are just mappers.

> Fast and Scalable
Who doesn’t loves speed? People love to praise the speed of Node.js. The data comes in and the answers come out like lightning. Node.js doesn’t mess around with setting up separate threads with all of the locking headaches.

There’s no overhead to slow down anything. You write simple code without redundant Magento 2 complication and Node.js takes the right step as quickly as possible.

--

--

Yegor Shytikov

True Stories about Magento 2. Melting down metal server infrastructure into cloud solutions.