You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: guides/deployment/microservices.md
+30-31Lines changed: 30 additions & 31 deletions
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,7 @@
1
1
---
2
2
synopsis: >
3
3
A guide on deploying SAP Cloud Application Programming Model (CAP) applications as microservices to the SAP BTP Cloud Foundry environment.
4
+
status: released
4
5
---
5
6
6
7
# Microservices with CAP
@@ -10,21 +11,21 @@ A comprehensive guide on deploying your CAP application as microservices.
10
11
[[toc]]
11
12
12
13
13
-
## Create a Solution Monorepo <UnderConstruction/>
14
+
## Create a Solution Monorepo
14
15
15
16
Assumed we want to create a composite application consisting of two or more micro services, each living in a separate GitHub repository, for example:
16
-
<!-- Those links aren't working links. Why do we use those? -->
17
+
17
18
-https://github.com/capire/bookstore
18
19
-https://github.com/capire/reviews
19
20
-https://github.com/capire/orders
20
21
21
-
With some additional repos, used as dependencies in the same manner, like:
22
+
With some additional repositories, used as dependencies in the same manner, like:
22
23
23
24
-https://github.com/capire/common
24
25
-https://github.com/capire/bookshop
25
26
-https://github.com/capire/data-viewer
26
27
27
-
This guide describes a way to manage development and deployment via *[monorepos](https://en.wikipedia.org/wiki/Monorepo)* using *[NPM workspaces](https://docs.npmjs.com/cli/using-npm/workspaces)* and *[git submodules](https://git-scm.com/book/en/v2/Git-Tools-Submodules)* techniques...
28
+
This guide describes a way to manage development and deployment via *[monorepos](https://en.wikipedia.org/wiki/Monorepo)* using *[NPM workspaces](https://docs.npmjs.com/cli/using-npm/workspaces)* and *[git submodules](https://git-scm.com/book/en/v2/Git-Tools-Submodules)* techniques.
28
29
29
30
1. Create a new monorepo root directory using *NPM workspaces*:
30
31
@@ -97,7 +98,10 @@ When one of the projects is cloned in isolation, it's still possible to fetch de
97
98
98
99
## Using a Shared Database
99
100
100
-
If you have multiple CAP applications relying on the same domain model or want to split up a monolithic CAP application **on the service level only while still sharing the underlying database layer**, you can deploy your model to a single database and then share it across applications.
101
+
You can deploy your model to a single database and then share it across applications, if you have one of the following scenarios:
102
+
103
+
- multiple CAP applications relying on the same domain model
104
+
- a monolithic CAP application that you want to split up **on the service level only, while still sharing the underlying database layer**
101
105
102
106
In the following steps, we create an additional project to easily collect the relevant models from these projects, and act as a vehicle to deploy these models to SAP HANA in a controlled way.
103
107
@@ -184,9 +188,9 @@ The project structure used here is as follows:
184
188
└─ package.json
185
189
```
186
190
187
-
The `shared-db` module is simply another CAP project, with only db content. The dependencies are installed via NPM, so it's still possible to install via an NPM registry if used outside of the monorepo setup.
191
+
The `shared-db` module is simply another CAP project, with only database content. The dependencies are installed via NPM, so it's still possible to install via an NPM registry if used outside of the monorepo setup.
188
192
189
-
The db model could also be collected on root level instead of creating a separate `shared-db` module. When collecting on root level, the `cds build --ws` option can be used to collect the models of all NPM workspaces.
193
+
The database model could also be collected on root level instead of creating a separate `shared-db` module. When collecting on root level, the `cds build --ws` option can be used to collect the models of all NPM workspaces.
190
194
191
195
:::
192
196
@@ -657,7 +661,7 @@ modules:
657
661
```
658
662
:::
659
663
660
-
The _xs-app.json_ file describes how to forward incoming request to the API endpoint / OData services and is located in the app/router folder. Each exposed CAP Service endpoint needs to be directed to the corresponding application which is providing this CAP service.
664
+
The _xs-app.json_ file describes how to forward incoming request to the API endpoint / OData services and is located in the _.deploy/app-router_ folder. Each exposed CAP Service endpoint needs to be directed to the corresponding application which is providing this CAP service.
661
665
662
666
::: code-group
663
667
```json [.deploy/app-router/xs-app.json]
@@ -791,13 +795,13 @@ You can then navigate to this url and the corresponding apps
791
795
```
792
796
793
797
794
-
## Deployment as separate mta
798
+
## Deployment as Separate MTA
795
799
796
-
This is an alternative to the all-in-one deployment. Assume the applications each already have their own mta.yaml. For example by running `cds add mta` in the `reviews`, `orders` and `bookstore` folder.
800
+
This is an alternative to the all-in-one deployment. Assume the applications each already have their own _mta.yaml_. For example by running `cds add mta` in the _reviews_, _orders_ and _bookstore_ folder.
797
801
798
802
### Database
799
803
800
-
We can add the [previously created](#using-a-shared-database) `shared-db` project as its own mta deployment:
804
+
We can add the [previously created](#using-a-shared-database) `shared-db` project as its own MTA deployment:
801
805
802
806
::: code-group
803
807
```sh [shared-db/]
@@ -876,7 +880,7 @@ resources:
876
880
877
881
The only thing left to care about is to ensure all 3+1 projects are bound and connected to the same database at deployment, subscription, and runtime.
878
882
879
-
Configure the mta.yaml of the other apps to bind to the existing shared database, for example, in the reviews module:
883
+
Configure the _mta.yaml_ of the other apps to bind to the existing shared database, for example, in the reviews module:
880
884
881
885
```yaml [reviews/mta.yaml]
882
886
...
@@ -906,12 +910,7 @@ resources:
906
910
907
911
#### Subsequent updates
908
912
909
-
- TODO...
910
-
- Whenever one of the projects has changes affecting the database, that triggers a new deployment of the `shared-db` project
911
-
- `git submodules`gives you control of which versions to pull, for example by `git branches` or `git tags`
912
-
- Ensure to first deploy `shared-db` before deploying the others
913
-
914
-
913
+
Whenever one of the projects has changes affecting the database, the database artifacts need to be deployed prior to the application deployment. With a single _mta.yaml_, this is handled in the scope of the MTA deployment. When using multiple deployment units, ensure to first deploy the `shared-db` project before deploying the others.
915
914
916
915
## Late-Cut Microservices
917
916
@@ -936,7 +935,7 @@ Instead of just choosing between a monolith and microservices, these aspects can
936
935
937
936
Since each cut not only has benefits, but also drawbacks, it's important to choose which benefits actually help the overall product and which drawbacks can be accepted.
938
937
939
-

938
+

940
939
941
940
### A Late Cut
942
941
@@ -945,44 +944,44 @@ When developing a product, it may initially not be apparent where the boundaries
945
944
Keeping this in mind, an app can be developed as a modular application with use case specific CAP services.
946
945
It can first be deployed as a [monolith / modulith](#monolith-or-microservice). Once the boundaries are clear, it can then be split into multiple applications.
947
946
948
-
Generally, the semantic separation and structure can be enforced using modules. The deployment configuration is then an independent step on top. In this way, the same application can be deployed as a monolith, as microservices with shared db, as true microservices, or a combination of these, just via configuration change.
947
+
Generally, the semantic separation and structure can be enforced using modules. The deployment configuration is then an independent step on top. In this way, the same application can be deployed as a monolith, as microservices with a shared database, as true microservices, or a combination of these, just via configuration change.
949
948
950
949

951
950
952
-
### Best Practices
951
+
### Best Practices {.good}
953
952
954
953
* Prefer a late cut
955
954
* Stay flexible in where to cut
956
-
* Prefer staying loosely coupled → for example, ReviewsService → reviewed events → UPDATE avg ratings
957
-
* Leverage db-level integration selectively → Prefer referring to (public) service entities, not (private) db entities
955
+
* Prefer staying loosely coupled → for example, ReviewsService → reviewed events → UPDATE average ratings
956
+
* Leverage database-level integration selectively → Prefer referring to (public) service entities, not (private) database entities
958
957
959
958
## Appendix
960
959
961
960
### Monolith or Microservice
962
961
963
962
A monolith is a single deployment unit with a single application. This is very convenient, because every part of the app is accessible in memory.

966
965
967
966
A modulith, even though the app is separated into multiple CAP services inside multiple modules, can still be deployed as a single monolithic application.
968
967
This combines the benefit of a clear structure and distributed development while keeping a simple deployment.
969
968
970
-

969
+

971
970
972
971
True microservices each consist of their own deployment unit with their own application and their own database.
973
972
Meaning that they're truly independent of each other. And it works well if they are actually independent.
974
973
975
-

974
+

976
975
977
976
What was mentioned earlier is a simplified view. In an actual microservice deployment, there are typically shared service instances and wiring needs to be provided so that apps can talk to each other, directly or via events.
978
977
If the microservices are not cut well, the communication overhead leads to high performance losses and often the need for data replication or caching.
979
978
980
-

979
+

981
980
982
981
983
982
### Application Instances
984
983
985
-
Having only a single virtual machine or container, the application can only be scaled vertically by increasing the cpu and memory resources. This typically has an upper limit and requires a restart when scaling.
984
+
Having only a single virtual machine or container, the application can only be scaled vertically by increasing the CPU and memory resources. This typically has an upper limit and requires a restart when scaling.
986
985
987
986
To improve scalability, we can start multiple instances of the same application.
988
987
@@ -1028,13 +1027,13 @@ Drawbacks:
1028
1027
#### Resource Separation
1029
1028
1030
1029
One part of an application may do highly critical background processing, while another handles incoming requests.
1031
-
The incoming requests take cpu cycles and consume memory, which should rather be used for the background processing.
1030
+
The incoming requests take CPU cycles and consume memory, which should rather be used for the background processing.
1032
1031
To make sure that there are always enough resources for specific tasks, they can be split into their own app.
1033
1032
1034
1033
#### Independent Scaling
1035
1034
1036
1035
Similar to resource separation, different parts of the app may have different requirements and profiles for scaling.
1037
-
For some parts, a 100% cpu utilization over an extended period is accepted for efficiency, while request handling apps need spare resources to handle user requests with low latency.
1036
+
For some parts, a 100% CPU utilization over an extended period is accepted for efficiency, while request handling apps need spare resources to handle user requests with low latency.
1038
1037
1039
1038
#### Fault Tolerance
1040
1039
@@ -1059,7 +1058,7 @@ Drawbacks:
1059
1058
- Coordination between deployment units for updates with dependencies
1060
1059
- Configuration wiring to connect systems across deployment units
1061
1060
1062
-

1061
+

1063
1062
1064
1063
1065
1064
With a single deployment unit, when a fix for one part needs to be deployed, the risk of redeploying the rest of the application needs to be considered.
0 commit comments