Live data from Hacker News

Make Your Back End Layer as Thin as Possible

kartick.substack.com

11–20 of 64 posts

Re: Make Your Back End Layer as Thin as Possible

#11
I strongly disagree with the advice in this article. There are many reasons. I’ll name a few.

Your DB is likely to contain internal state that has no business being sent to the client. Over time applications change and fields can change meaning, become deprecated, or become dependent on other fields through business logic.

If you do this, any business logic not captured in your backend has to be duplicated in each front end. Imagine needing to write and maintain the same business logic in a JavaScript framework, Java for Android, and Swift for your iOS app.

How do you make breaking DB changes if you take on this strategy of API design?

In most cases in my professional career, making a choice to have a thin RESTful API that corresponds closely to the DB structure has been a mistake that only time made clear. RPC-like APIs fared much better.

This advice may be good for a proof of concept, a prototype, or an early version which stands a good chance of getting rewritten. I would not recommend it for anyone wishing to build a lasting architecture.

Don’t take my word for it. Take a look at your favorite famous internet company and look at their API and try to deduce if they follow this advice or not.

Re: Make Your Back End Layer as Thin as Possible

#12

This article doesn't use the word "permission" or "validation", which is concerning. The only mention of security hand waves away the idea of access control to a "thin back end". That's the whole point of this post! The details of this are critical! > you can define an API that takes an SQL query from the client, runs it, and returns the results. This query can be run under a database user account that has only read…

While I largely share your broader concerns,

> ensuring that queries that should read only read

seems to be the purview of the DBMS, setting appropriate account permissions. The bit you quoted said "a database user account that has only read access".

Re: Make Your Back End Layer as Thin as Possible

#14

This article doesn't use the word "permission" or "validation", which is concerning. The only mention of security hand waves away the idea of access control to a "thin back end". That's the whole point of this post! The details of this are critical! > you can define an API that takes an SQL query from the client, runs it, and returns the results. This query can be run under a database user account that has only read…

While I largely share your broader concerns, > ensuring that queries that should read only read seems to be the purview of the DBMS, setting appropriate account permissions. The bit you quoted said "a database user account that has only read access" .

This assumes you only deal with non-sensitive data (e.g. user profiles).

Never mind that, how do you prevent denial of service attacks? I can run any allowed query on that database. Including 50 times cross join of the table I can read, sorted by random.

Re: Make Your Back End Layer as Thin as Possible

#15

This article doesn't use the word "permission" or "validation", which is concerning. The only mention of security hand waves away the idea of access control to a "thin back end". That's the whole point of this post! The details of this are critical! > you can define an API that takes an SQL query from the client, runs it, and returns the results. This query can be run under a database user account that has only read…

While I largely share your broader concerns, > ensuring that queries that should read only read seems to be the purview of the DBMS, setting appropriate account permissions. The bit you quoted said "a database user account that has only read access" .

On MySQL, you can run SLEEP() or BENCHMARK() as a read-only user to your heart's content. You cannot restrict access to these functions, and letting the wide internets play with them will bring your server to its knees by clogging up all connections and hogging up its CPUs.

Re: Make Your Back End Layer as Thin as Possible

#16
post #10

Does the article mention business logic? Even at MVP stage, many backends do quite a bit more than CRUD. This is why I wouldn't pick e.g. https://github.com/PostgREST/postgrest even when it's a perfectly fine (and probably well thought-out) project.

Business logic should not happen at API boundaries (regardless of what kinds of technologies or transport those interface boundaries are facilitating). Interfaces should be a validation and translation layer that receive incoming data, hand off to business logic, receive outgoing data and perform whatever translation and serialization required to satisfy the return/response interface. Business logic should be completely isolated from that responsibility, and ideally transport-agnostic.

In a traditional “MVC” style architecture this would be described as “thin controller”. But that kind of architecture often tightly couples other things that shouldn’t be, because they hide other transport/interface boundaries.

The way I approach this is pretty simple:

1. Interfaces between user input and “programs” of any kind are used for validation, sanitization, deserialization and interface translation. Only. No state changes can occur here. In the language of the article, this is the “thin backend”.

2. Any runtime business logic, using any technology for that runtime, clearly defines its input and output types. This layer is allowed to make local state change as appropriate, but any external side effects are forbidden.

3. If the layer described in #2 needs to communicate with an outside network, system, storage or service layer, recurse to #1. Contracts between these boundaries are only that and imply no implementation details.

4. If you implement #3 in the service providing #2, those responsibilities are explicitly separated.

5. Do business logic. Return data consistent with the business logic’s contract.

6. Rewrap the onion and eventually serialize the result for the outside client according to that layer’s expectation, at whatever interface boundary you’re at.

Re: Make Your Back End Layer as Thin as Possible

#17
The thinner your backend, the thicker your frontend. I much prefer writing in backend languages (e.g. python) than in frontend languages (e.g. javascript).

And as someone else pointed out, you can't enforce security on the frontend.

The line is getting blurrier between front and back these days with new frameworks, e.g. React. Personally, I'm not ready to move away from mature languages generating HTML on the backend, even though it means giving up some interactivity on the frontend.

Re: Make Your Back End Layer as Thin as Possible

#19
> You can bypass this whole inefficiency by making the backend a pass-through. That gives you one fewer thing to worry about, which any engineer will welcome.

Um, yeah, and now you have front end security issues, since all the expressive power that you have just given to your front end engineers is also available to any damned fool who can fire up a browser console.

I strongly agree that you should make the distance between the front end and the data as thin as possible, but this article comes at that from the front-end developer perspective.

There is another option: make the front end as thin as possible. Eliminate the huge front end framework and go with something closer to the HTML.

This keeps the expressiveness on the server side, which is a trusted computing environment, rather than exposing it on the client side, in an untrusted computing environment, where security concerns become much more complicated.

Re: Make Your Back End Layer as Thin as Possible

#20
> In the three-tier frontend-backend-database architecture, try to have as thin a middle layer as possible. Or even eliminate the backend layer altogether by directly exposing the database to the frontend.

Wat...

This article reads like a jumbled mess of ideas. But, the above is a total wat...

And it also assumes that your backend doesn't have to consume and tie together several resources or that you need a caching layer and so many other naive assumptions - and I'm not even addressing the security implications.

Take this article with a heavy cup of salt.

To me it just reads like something someone would write because they want full control and does't like working with and/or being dependant on other people/teams.

Post reply on HN