6 ms·
The Performance Cost of Node.js and GraphQL
- SebastianKra 3y ago> We could write a one-shot resolver that implements the entire query. [...] This brings up the meta-question about why use GraphQL in the first place [...]. This seems like a reasonable approach to me. Give downstream-developers the opportunity to work unobstructed, then observe which queries are slow / heavily used, and implement optimised resolvers for just these cases. In the worst case, you might not save any work on your end, but at least teams that depend on your api are never blocked from iterating.
- throwaway2990 3y agoOr just stop separating front end and backend. The worst companies to work for are the ones who separate these 2 then try to patch it with junk like graphql.
- SebastianKra 3y agoWell, personally I have a bigger aversion to companies who hire dogmatic developers that discard an entire technology as junk, without even considering the scenarios in which you might prefer a horizontal separation of teams.
- 29athrowaway 3y ago"2x-3x latency increase" With respect to what?
- bastawhiz 3y ago> Instead of multiple batches of promises, we fetch the user, items, and details in one shot. This brings up the meta-question about why use GraphQL in the first place, but that’s a larger conversation for a separate time. At my current job we have a TON of methods like the one just above this paragraph, and they're terrible to work with. It's hard to write (correctly), it's hard to test (correctly), and it is simply tedious to make work. I could write four lines of declarative GQL (that fetches exactly what I ask for from the DB) or a real mess of a method. In practice, it's rarely the case that you're returning 10k of anything. To look at the author's benchmark, the difference between 20ms and 60ms for a far nicer and more maintainable dev experience (for 1000 returned items) is not a compelling argument. Hell, if I look at our stack, the biggest cost is creating parameterized queries with many hundreds of IDs to select to prevent N+1 queries. I've spent countless hours optimizing the slowest code (that uses the pattern the author suggests) to use JOINs instead. Removing the round trip and the cost of shipping the list of IDs back to the DB consumed far more time than you might expect. If you want to go down an even deeper rabbit hole, look at the cost of serializing your data to JSON and deserializing it on the client. When you're shipping tens of thousands of records to the client, that's likely hundreds of kilobytes or even megabytes of data. Serializing that is expensive!
- tango12 3y agoTotally feel the pain. Ironically, most DBs have started supported really good json aggregation functions. Combined with lateral joins, it’s possible to even push this ser/de to the DB directly and have minimal unpacking and packing in sending that data back to the client!
- ElectricalUnion 3y agoTo me those advanced json aggregation functions are to handle the fact that most DBs accept de-normalized JSON fields. Direct DB ser/de looks like a bad idea, it's often much easier to scale the application server layer that the DB layer on most cases. And in most situations you want to "expand" the data as close as possible to the final location. If you really care about having very low overhead ser/de you would probably go for protobuf/Cap'n Proto almost everywhere instead.
- tango12 3y ago> it's often much easier to scale the application server layer that the DB layer on most cases. Hm…ignoring the specific design decision here on using json agg - I think the open question is - is that specific sentiment practically still the truth in 2023? Is it worth scaling app servers or is it better to use Postgres well and scale a managed PG offering? Do we need to spend time writing boring code when we could instead just use DBs better? And then instead we can spend time where it matters in the app server. > And in most situations you want to "expand" the data as close as possible to the final location. What do you mean by “expand”? From a perf pov: Imagine you’re fetching 100 authors and each had 1000 articles, would you rather a) fetch mxn Cartesian product or b) ask the DB to send you a json aggregation where each author has a 1000 articles? There’s a significant db perf, intermediate layer ser/de, and network latency difference right?
- ElectricalUnion 3y ago> Is it worth scaling app servers or is it better to use Postgres well and scale a managed PG offering? That is a very good question indeed, it probably depends on whatever compute or the DB offering is cheaper to scale for you; from my very anecdotal experience using both, usually DB is much more expensive to scale up. > What do you mean by “expand”? Getting more data thru the pipes, getting closer to the limit of the network pipes. That doesn't sound like a realistic scenario to me - pulling Y articles from X authors. If you're not pulling directly from articles, and you're pulling from X authors, X>1, you most likely don't actually want the articles, more likely you want instead some stat of each article like article score, subject, tags or comment counts. Then I don't see neither the JOIN expansion or the JSON ser/de being a serious issue at all compared to the DB retrieval costs.
- ttfkam 3y agoAll this pain when most of the time folks could just fire up Postgraphile or Hasura, point them at their Postgres database, and go sip mai tais by the pool. I honestly don't understand why folks insist on writing their resolvers by hand in 2023. "But I don't want folks having direct knowledge of my database schema," I hear as a retort. 1. Most of the time for most projects, the GraphQL schema or REST API is a direct analog of the database. 2. You can always make a new Postgres schema with views to expose just the data features you want with annotations to show where the foreign key relationships should be. Still worlds easier than writing and maintaining resolver and data loader logic by hand.
- brycelarkin 3y agoPostgraphile just released their v5 update (still in beta) and it’s even more awesome than v4. No more crazy queries to address the n+1 problem.
- Exuma 3y agoPostgraphile looks awesome. Thx for pointing it out
- ShadowBanThis01 3y agoI looked at GraphQL and couldn't get past the fact that it doesn't provide a way to express joins. Isn't that a problem?
- alex_lav 3y agoA join is a SQL-ism. GraphQL allows you to express _relationships_, which the underlying database would fulfill via a join.
- tango12 3y agoTrue - but the important point worth discussing here is that most common GraphQL implementations make it really hard to think in joins. The resolver pattern and data loader pattern make joins really painful. eg: implement a simple top-N query (fetch 10 users and the most recent 5 articles for each) in GraphQL with a resolver tree, or even data loader.
- stephen 3y agoOne of Bun's assertions is that their AsyncLocalStorage is significantly faster than Node's, b/c it purposefully only implements the subset of async_hooks that can be implemented w/o affecting performance: https://github.com/oven-sh/bun/pull/3089 https://github.com/oven-sh/bun/pull/3089 I haven't had a chance to test it out yet, but perhaps the OP could add Bun to the mix? To compare it's promise overhead vs. Node's promise overhead. Also there is an issue in Node about performing the same optimization: https://github.com/nodejs/node/issues/46265 https://github.com/nodejs/node/issues/46265
- tango12 3y agoIMHO: The way we often approach GraphQL execution is a little dated. It’s perhaps legacy thinking from times when we had data layers that couldn’t be expected to scale even for the majority of use cases. But that has completely changed over the last 5-10 years - DBs can scale pretty massively, for specific workloads with very little work required at the app server layer. So if we used DBs to actually model as close to our domain entities as possible (and yes, we’ll need multiple DBs sooner or later), then a huge part of our work is already technically done. So - here’s another way to approach building GraphQL: 1. Build a “read graph” so that you get a highly composable API that can almost entirely be pushed down into a data layer (eg: Postgres). This would involve techniques similar to building a compiler. Which is hard, but the great news is that it’s easy to make reusable across people who share the same data layer (Eg: Postgres). There are only 2 business logic concerns at layer, both of which can be pushed down, which helps simplify the problem. 1) modelling/transformation: which can be done in the language of the data layer and pushed down (like views) 2) authz: which can also be done in the language of the data layer and pushed down (like rls) 2. The “command graph”: a set of controllers written in whatever language you please that makes changes to the data layer (or delegates to an orchestration system) and returns a reference to the read graph after having made changes. This is kind of how we approach the problem at Hasura - a connector SDK that makes it easy to build reusable query compilers for different DBs or API services. And it’s been great to see similar ideas in the ecosystem - let’s make it easy to execute a tree of functions of create a query plan, instead of executing a tree of data fetch functions (like the OP). Grafast by the postgraphile folks comes to mind.
- wdb 3y agoI only received upset emails when a (businesS) consumable API was GraphQL only.