Should Two Dockerized Node.js Apps Share One PostgreSQL Instance?
Asked by
4k_pagefault
I'm planning to run two separate applications with Docker. Both apps use Node.js and PostgreSQL. If I run them at the same time, will each application consume additional CPU and memory, or can they share the same PostgreSQL instance? Is it better to run one PostgreSQL container for both applications, using separate databases or schemas, or should each application have its own PostgreSQL container? I'd also like to understand what Docker Compose creates when following a typical setup file.
Answers
For a small deployment where minimizing resource usage matters, a reasonable layout is one PostgreSQL container with two separate databases or schemas, one Node.js container per application, and optionally a reverse-proxy container in front of them. The Node.js containers donโt need to expose public ports if the proxy is the only service accepting external traffic. For production, separate databases or separate PostgreSQL users are usually preferable to putting both apps in the same schema.
Reply to this answer
Docker doesnโt decide this automaticallyโyou configure it. Both Node.js applications can connect to the same PostgreSQL server, or each can use a separate server. If they share one PostgreSQL instance, give each application its own database or schema and credentials so they canโt accidentally access or modify the other appโs data. Separate PostgreSQL containers provide stronger isolation, but they also use more memory and CPU. Running multiple containers generally adds some overhead, although the main resource usage comes from the applications and database workloads themselves.
gently__revised: Exactly. Sharing a database server is possible, but the applications still need separate database names, schemas, or users if you want their data isolated.
Reply to this answer
Add your answer
Answers are reviewed before publication.
4k_pagefault: So if I copy a Docker Compose example, will it automatically create two PostgreSQL containers? Also, is there a different kind of Docker involved here?
pocket observatory: It depends entirely on the Compose file. Each service under the services section normally becomes a separate container, so a file with two PostgreSQL services creates two database containers, while one PostgreSQL service can be shared by multiple Node.js services. Docker Compose is simply a tool for defining and running multiple Docker containers; it isnโt a different version of Docker.