Sharing a database between microservices #4275
efekarakus
started this conversation in
Show and tell
Replies: 1 comment
-
Hi, I just want to share my use-case as a data point. I'm running a typical python web stack with two services (simplified for this example):
In this case, both services are running the exact same django codebase & docker container, so they share the same schema and are expected to use the same database. Solution 1 does not make sense and would be a huge anti-pattern for this model (Solution 2 is perfect though). |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
We outlined solutions in #4273 (comment) and #2495 (comment), so I'm copying it over here in order to make it easier to discover the solution.
One microservice should run
copilot storage init
to create the RDS cluster and have direct access to the database. This service can expose an API so that internal services can retrieve data from the database. For example, if the service with the database is called "warehouse" and exposes port 80 inimage.build
, then we can add a new API in the "warehouse" service for say "GET /inventory/{productId}". Then the backend service can make a request to that path:An alternative proposal is to allow ingress from all the services in the environment by modifying the security group of the RDS cluster.
There is a
<App>-<Env>-EnvironmentSecurityGroup
that's assigned to all services. After copilot generates the addon template withstorage init
, you can modify the CloudFormation template and add this ingress rule to the cluster's security group. Here is a snippet that may work for you:With this ingress, the cluster's security group will allow access from all services deployed in the environment.
You'll need to also update the manifest of the remaining services with the database secret created in the addon stack.
For additional ways of injecting a secret from AWS Secrets Manager see the following guide.
The tradeoff is that we created tight coupling between our services because now they can impact each other. Services are all aware of the database schema, and a service A can modify data that it shouldn't. However, this setup is a lot more agile and easier to get going. This architecture is preferred if you're a smaller organization with a few teams and high trust.
Hope this helps!
Beta Was this translation helpful? Give feedback.
All reactions