Web apps architecture: 1 or n API

2020-07-18 05:19发布

问题:

Background: I'm thinking about web application organisation. I will separate front (web site for browser) from back (API): 2 apps, 2 repository, 2 hosting. Front will call API for almost everything.

So, if I have two separate domain services with my API (example: learning context and booking context) with no direct link between them, should I build 2 API (with 2 repository, 2 build process, etc.) ? Is it a good practice to build n APIs for n needs or one "big" API ? I'm speaking about a substantial web app with trafic.

(I hope this question will not be closed as not constructive... I think it's a real question for a concrete case, sorry if not. This question and some other about architecture were not closed so there is hope for mine)

回答1:

It all depends on the application you are working on, its business needs, priorities you have and so on. Generally you have several options:

  • Stay with one monolithic application
  • Stay with one monolithic application but decouple domain model across separate modules/bundles/libraries
  • Create distributed architecture (like Service Oriented Architecture (SOA) or Event Driven Architecture (EDA))

One monolithic application

It's the easiest and the cheapest way to develop application on its beggining stage. You don't have to worry about complex architecture, complex deployment and development process. It also works better if there are no many developers around.

Once the application is growing up, this model begins to be problematic. You can't deploy modules separatly, the app is more exposed to antipatterns, spaghetti code/design (especially when a lot people working on it). QA process takes more and more time, which may make it unusable on CI basis. Introducing approaches like Continuous Integration/Delivery/Deployment is also much much harder.

Within this approach you have one repo/build process for all your APIs,

One monolithic application but decouple domain model

Within this approach you still have one big platform, but you connect logicaly separate modules on 3rd party basis. For example you may extract one module and create a library from it.

Thanks to that you are able to introduce separate processes (QA, dev) for different libraries but you still have to deploy whole application at once. It also helps you avoid antipatterns, but it may be hard to keep backward compatibility across libraries within the application lifespan.

Regarding your question, in this way you have separate API, dev process and repository for each "type of actions" as long as you move its domain logic to separate library.

Distributed architecture (SOA / EDA)

SOA has a lot profits. You can introduce completely different processes for each service: dev, QA, deploying. You can deploy just one service at once. You also can use different technolgies for different purposes. QA process gets more reliable as it involves smaller projects. You can version communication (API) between services which makes them even more independent. Moreover you have better abilitly to scale horizontaly.

On the other hand complexity of the high level architecture grows. You have much more different components you have to take care: authentication / authorisation between services, security, service discovering, distributed transactions etc. If your application is data driven (separate frontend which use APIs for consuming data) and particular services don't need to communicate to each other - it may be not as much complicate (but such assumption is IMO quite risky, sooner or letter you will need to communicate them).

In that approach you have separate API, with separate repositories and separate processes for each "type of actions" (which I understand ss separate domain model / services).


As I wrote on the beggining the way you choose depends on the applicatoin and its needs. Anyway, back to your original question, my suggestion is to keep APIs as separate as you can. Even if you have one monolithic application you should be able to version APIs separatly and keep their domain logic separate. Separating repositories and/or processes depends on the approach you choose (eg. among these I mentioned before).

If I missed your point, please describe in more detailed way what answer do you expect.

Best!