Web APIs and the N+1 Problem
infoq.com
infoq.com
> By moving away from RDBMS we have successfully freed ourselves from the restricting schema-bound limitations of RDBMS and been able to churn code very quickly. This has allowed for higher agility of teams.
The whole article is a fantastic example of why moving away from RDBMS has reduced the agility of teams. They have to bake relationships into their schema (sometimes even duplicating data into multiple different hierarchies), instead of having the flexibility to join tables together in a multitude of patterns on an ad-hoc basis. It makes sense as a performance optimisation if you have a lot of data. But otherwise it's just slowing you down.
Any change to the shape of the data in that database which could be in a backwards compatible way in a "schemaless" database can also be made in a backwards compatible way in a database with a schema (and making the change is literally a 5 minute task - less if you're just messing around in a dev environment). And with a schema you have the added advantage that you'll be notified if you accidentally make non-backwards compatible changes.
What is easy to write now, could be difficult to change or read later. What is easy for one developer, could be confusing to others.
The grass is always greener on the other side, so the lure is to go in circles over time.
This is the bread and butter anyways, as how we use computing always changes. Experts deal in complexity, so others don't have to. But even if we want to, it's hard to escape. Just thinking hard and long about these problems takes time nobody is interested in paying for.
With MySQL you get quite a few problems when your migration script fails as you can get left in a broken state that you have to sort out manually. But with Postgres schema changes being transactional it's quick and painless.
No developers in 2021 should be struggling with N+1 or thinking that not enforcing schemas in your DB makes them stop existing. You've just moved schema enforcement to your application, or maybe you've chosen not to do it at all which is even worse for just about anything more than a hackathon project.
Getting web app UX right typically requires page specific query tuning and introducing those as JSON API end points leads to a gronky, churny API. GraphQL helps in this regard, but that has some pretty serious security implications[1].
I wrote an article on this topic back a while back on the intercooler.js blog:
https://intercoolerjs.org/2016/02/17/api-churn-vs-security.h...
You can and should still create a generic JSON API for server-to-server automations, but it has a different set of design considerations when compared with your web app needs (rate limiting, generic but secure functionality, etc.)
[1] - https://twitter.com/AdamChainz/status/1392162996844212232
If you are in that situation, GraphQL is actually worse, because you end up with less control over the SQL you end up running.
I can see that idea working very well for content-driven, interactive but mostly static websites, something like HN.
In Node.JS the dataloader processes N lookups simultaneously in the next tick of the eventloop. So call(1) call(2)...call(n) would be executed as call([1,2,3,..n]) allowing you to efficiently implement it.