Goal of this article
This is a not so technical comparison (you can find more specific tech comparison elsewhere), i just want to outline how does it feel to develop web NodeJS applications when you are a Java developer by trade.
So please remember, this article is full of personal opinions.
Before starting, i must do a few premises:
- i’m a java software developer. I’ve been developing java based software for at least 15 years
What am i trying to compare
I would like to highlight some of the differences that are perceived when developing an application based on the NodeJs/ExpressJS stack compared to the one based on SpringBoot.
Why am i comparing these
TLDR; after completing a contracted project, i decided to test another ecosystem to check if it can avoid some of Java mostly criticized points
One of my last customer was a company that was creating a crypto exchange (yes, quite a common thing in 2018, but not so common for an italian based company). They contracted me to join their teams (3 different developing teams) and help them build their platform. I mainly developed microservices for authorization and authentication, core transactions processing and other stuffs like customers’ KYC microservices and code libraries shared among different microservices.
That was a big and interesting project.
But during confrontations with other teams and people i often heard critics toward java web development in favor of python or GO. Some of the critics to Java that the other languages seemed to not suffer of were:
- Java code is verbose
- Everything is interface-tangled in Java
- Memory consumption of Java apps is overwhelming
- Same as 3. but talking about disk space
- Code development requires much time
(3. and 4. should be considered when spinning hundreds of docker based microservices but i think that this scale problem may be a non-sense since if you have hundreds of microservices running then you should already be earning a lot of money and you can afford also “expensive” instances to support memory-greedy java-applications).
I must be honest, sometimes i think that in 2019 all of the five points above are really reasonable critics so i wanted to experiment during this year i’m taking to develop my self-funded projects, to test some other technologies.
Since i needed to do web development and not necessarily microservices based projects, after a quick look (really quick) at GO i decided to not deepen that language. I think it is a great language (from what i read) but it’s not the right tool for my current projects.
I leave to you all the observations regarding dynamic typing that in my opinion isn’t a big deal.
Ok, this is one of the most “shocking” thing for a Java developer. But the shock vanishes after a few moments. You should consider that everything runs on a single thread (on any java web app you have multiple threads) and that callback functions (asynchronous functions) are queued on a queue and executed when it is better to execute them but all the stuffs run on a single thread (the key to NodeJS’ speed and low memory consumption). From a java (but also non-java) developer point of view this means:
- don’t run cpu-time intensive code or everything will wait for cpu to be free before executing a new queued function
- if something goes wrong and NodeJS crashes then everything crashes: in the case of a web application serving multiple concurrent requests, all the requests crash. You don’t have the isolation that you have with a java web application
The js equivalent to SpringBoot ecosystem: ExpressJS, PassportJS, Sequelize
If we limit the comparison to the MVC web application part only, SpringBoot is definitely fabulous: light and fast, complete and extremely configurable. From this point of view a Java developer does not feel any major shortcomings compared to what the counterpart of ExpressJS offers.
ExpressJS also offers the same potential. One thing that depending on personal taste can be more appreciated or not is that the routing instead of being defined at the level of java annotation is definable at the level of routing files.
More generally, SpringBoot indicates a very precise way on how to organize the code in packages (models, services, controllers) while in Expressjs context there are no such guidelines. Nevertheless, it is possible to re-apply a similar code structure and there are often projects where the code is structured in a similar way to a SpringBoot project.
ORM in java has always been, from my point of view, the Achilles heel of java applications. The java standard is roughly Hibernate (despite the various alternatives, however widespread, such as Jooq and MyBatis), while for the JS world related to relational dbs, the most popular library is Sequelize. I’ll dedicate a specific chapter to the comparison between the two.
Hibernate vs Sequelize
TLDR; Hibernate is still the most complete, mature and versatile solution…at a very high cost! Sequelize may cover 90% of your use cases.
I don’t hate Hibernate but surely i don’t love it. It’s over engineered, slow and complicate. It’s like an elephant. And it can do anything with any supported db. On the contrary Sequelize is small and simple but can’t manage all the cases.
Some of the things i discovered through the use of Sequelize:
- you’ll have a not so hard but definitely not so easy time trying to force snake-case for table’s fields. You can manually specify them one by one (but that is otrageous) or you can use some hack to convert names to snake case. That is a simple solution but it has drawback that it will break the migration command line tool. Anyhow it is unacceptable that all the requests to introduce flexibility in naming convention definition were discarded and ignored.
- it doesn’t fully support composite key: as clearly stated here, “While it is possible to create composite primary keys in Sequelize […] Sequelize doesn’t currently support composite foreign keys, so there is no way to reference a model/table which has composite primary keys.”. Again, from my point of view this is immaturity.
- you’ll have to do some nice trick to manage date’s fields due to timezone interpretation (really…that was really unexpected)
- when adding instances to the fields that are related among two entities (suche as Book and Author for example), entities are immediately saved. This is not a big deal but shows that Sequelize is far less sophisticated than Hibernate which has internal mechanisms to decide when to flush data.
There are also some things that i love in Sequelize like the easiness to create queries at runtime (that’s a breeze, you can compose a json object at runtime and pass it to the query engine). Try to do it creating a JPQL query or consider how overcomplicated it is to do with Criterias. Honestly, using Hibernate and SpringData JPA trying to dynamically filter a query at runtime by some fields is a pain in the ass while it is a really easy task (as it should be with any framework/language) to do in Sequelize.
Another aspect where Sequelize shines respect to Hibernate is when you hit some hard cases and need to do native query: both of them let you execute native queries but honestly it is a lot simpler to convert the results to their models in Sequelize than in SpringData JPA/Hibernate.
And i’m not talking about startup time: introducing hibernate adds seconds to boot time, while Sequelize is quite immediate.
As a final consideration, it results quite obvious that Sequelize is a lot more immature than Hibernate, that Hibernate is capable of doing anything while Sequelize is a lot less abstract and engineered and definitely easier to work with but covers only 90% of the cases: this can be a great advantage especially when you own the db schema and don’t have to adapt to legacy dbs and if you don’t plan to migrate db engine one day (to be honest i’ve seen only one case of db migration in all my life and it was when two banks decided to merge and so it was decided to keep only one IT system and rewrite the code of the abandoned one to the other platform: anyhow there were thousands of store procedures to be rewritten so code portability, in my opinion when talking about ORMs, is a useless feature).