Consider a web-app with 5 micro-services deployed with Docker, and a MySQL container dedicated to storing data produced from other services (shared-database pattern). How should I make the connection between the MySQL container and other containers?

Currently, I am converting a monolith application software into micro-service architecture. The problem is that SQL queries (used to insert data in the database) were written within each module of the software (service) such that when a module is called or initiated, a connection to the main MySQL database from that module is also initiated inside the module. The functions used to execute queries and connect to MySQL are imported/called by each module from a custom python library that I have built.

Should I make the services communicate with the MySQL container directly (i.e. write the SQL functions into the MySQL container and make them available as API endpoints) or should I keep the current library and connect each service to the main MySQL database with the SQL functions? The second option may require to define MySQL credential information as environment variables within each service container.

  • 5
    What's the purpose of splitting into services? i.e. what measurable objective are you trying to achieve? If all the services all still use the same single database then there are no microservices and you still have a monolith (specifically a "distributed monolith" which is the worst of all possible worlds that keeps all the downsides of a monolith and sprinkles all the downsides of distributed services on top), so take a step back and look at what you want to gain since having those services all connect to the same DB is very likely to undermine whatever you're trying to achieve. Apr 12, 2023 at 6:10
  • 1
    I agree with Ben but I would rephrase to say that splitting each service into its own container might be desirable but doesn't make them microservices. If they depend on the same DB, they are all part of the same application regardless of whether each service is deployed separately or not.
    – JimmyJames
    Apr 12, 2023 at 17:20


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.