{"id":23651386,"url":"https://github.com/damiancipolat/cluster-servers","last_synced_at":"2026-04-29T22:33:17.432Z","repository":{"id":139319022,"uuid":"171162177","full_name":"damiancipolat/Cluster-servers","owner":"damiancipolat","description":"Examples and techniques creating http servers using nodejs cluster module.","archived":false,"fork":false,"pushed_at":"2019-02-18T01:18:35.000Z","size":271,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-05-20T01:13:39.014Z","etag":null,"topics":["api","es6","ipc","nodejs","parallelism","pm2","server"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/damiancipolat.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-02-17T19:22:57.000Z","updated_at":"2019-02-18T01:18:37.000Z","dependencies_parsed_at":"2023-03-15T00:17:16.885Z","dependency_job_id":null,"html_url":"https://github.com/damiancipolat/Cluster-servers","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/damiancipolat/Cluster-servers","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/damiancipolat%2FCluster-servers","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/damiancipolat%2FCluster-servers/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/damiancipolat%2FCluster-servers/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/damiancipolat%2FCluster-servers/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/damiancipolat","download_url":"https://codeload.github.com/damiancipolat/Cluster-servers/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/damiancipolat%2FCluster-servers/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32446663,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-29T22:27:22.272Z","status":"ssl_error","status_checked_at":"2026-04-29T22:10:49.234Z","response_time":110,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["api","es6","ipc","nodejs","parallelism","pm2","server"],"created_at":"2024-12-28T16:37:21.712Z","updated_at":"2026-04-29T22:33:17.424Z","avatar_url":"https://github.com/damiancipolat.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Cluster servers in Node.js\nExamples and techniques of how to create a nodejs http server using the cluster module, creating fork process.\n\n- **[Share ports]**\n- **[Fork server]**\n- **[PM2 \u0026 cluster]**\n- **[Docker \u0026 cluster]**\n\n[Share ports]:https://github.com/damiancipolat/Cluster-servers/blob/master/README.md#share-ports\n[Fork server]:https://github.com/damiancipolat/Cluster-servers/blob/master/README.md#forked-server\n[PM2 \u0026 cluster]:https://github.com/damiancipolat/Cluster-servers/blob/master/README.md#pm2--cluster-server\n[Docker \u0026 cluster]:https://github.com/damiancipolat/Cluster-servers/blob/master/README.md#docker--cluster-server\n\n## Share ports:\nIn this example I create a basic http server using the \"http\" module. We share the same por for all the workers, in order to share the socket to the workers.\n\n![N|Solid](https://github.com/damiancipolat/Cluster-servers/blob/master/doc/sharing-ports.png?raw=true)\n\n**To run:**\n\n```js\n$ node cluset-server/cluster-server.js\n```\n\nAnd when we run the process the console output will be:\n\n```sh\nMaster server created!!\nPID: 13232  running in PORT: 8000\nWorker created, PID: 13238  PORT: 8000\nWorker created, PID: 13243  PORT: 8000\nWorker created, PID: 13244  PORT: 8000\nWorker created, PID: 13250  PORT: 8000\n```\n\nRunning a ps-la, nodejs have created a different process:\n\n```sh\nF S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD\n0 S  1000 13232  5806  0  80   0 - 219231 ep_pol pts/21  00:00:00 node\n0 S  1000 13238 13232  0  80   0 - 218460 ep_pol pts/21  00:00:00 node\n0 S  1000 13243 13232  0  80   0 - 218460 ep_pol pts/21  00:00:00 node\n0 S  1000 13244 13232  0  80   0 - 218460 ep_pol pts/21  00:00:00 node\n0 S  1000 13250 13232  0  80   0 - 218460 ep_pol pts/21  00:00:00 node\n4 R  1000 13317 13265  0  80   0 -  7872 -      pts/35   00:00:00 ps\n```\n\nRunning lsof -i :8000, the master process PID 13232 is the only who use the port \"8000\":\n\n```sh\nCOMMAND   PID        USER   FD   TYPE DEVICE SIZE/OFF NODE NAME\nnode    13232 damianlinux   16u  IPv6  93000      0t0  TCP *:8000 (LISTEN)\n```\n\n## Forked server:\nIn this code I will create a server that use fork process to create workers and communicate between them using a simple IPC.\n\n![N|Solid](https://github.com/damiancipolat/Cluster-servers/blob/master/doc/message.png?raw=true)\n\n**To run:**\n\n```sh\n$ node fork-server/app.js\n```\n\nAll workers answer each request and using ipc send to the parent process the response to send at the client. In the master process there are a list of process reference. The way to delegate the answer of each request is using a random way. You can take a look of the message broker created by me in other project.\n\n## PM2 \u0026 cluster server:\nIn the last examples we learn how to create our custom load balancer process using the children forked process, but in a production environment, maybe is'nt the best way. I think could be a better option work with a robust process manager. One good option is PM2 http://pm2.keymetrics.io. PM2 offer us the way to work as a load process balance for us, take a look of this link http://pm2.keymetrics.io/docs/usage/cluster-mode/.\n\n**Configuration, define to pm2 the number of children process to create:**\n\n```json\n{\n  \"apps\" : [{\n    \"script\"    : \"api.js\",\n    \"instances\" : \"4\",\n    \"exec_mode\" : \"cluster\" \n  }]\n}\n```\n\n**To run:**\n\n```sh\n$ pm2 start process.json\n```\n\n**Monitoring:**\n\nTo see the list of process running:\n\n```sh\n$ pm2 list\n┌──────────┬────┬─────────┬─────────┬───────┬────────┬─────────┬────────┬─────┬───────────┬─────────────┬──────────┐\n│ App name │ id │ version │ mode    │ pid   │ status │ restart │ uptime │ cpu │ mem       │ user        │ watching │\n├──────────┼────┼─────────┼─────────┼───────┼────────┼─────────┼────────┼─────┼───────────┼─────────────┼──────────┤\n│ api      │ 0  │ N/A     │ cluster │ 26085 │ online │ 0       │ 2m     │ 0%  │ 37.3 MB   │ damianlinux │ disabled │\n│ api      │ 1  │ N/A     │ cluster │ 26091 │ online │ 0       │ 2m     │ 0%  │ 37.1 MB   │ damianlinux │ disabled │\n│ api      │ 2  │ N/A     │ cluster │ 26097 │ online │ 0       │ 2m     │ 0%  │ 36.8 MB   │ damianlinux │ disabled │\n│ api      │ 3  │ N/A     │ cluster │ 26107 │ online │ 0       │ 2m     │ 0%  │ 37.2 MB   │ damianlinux │ disabled |\n\n```\n\nTo see a console UI:\n\n```sh\n$ pm2 monit\n```\n\n(Left pm2 list / Right pm2 monit)\n![N|Solid](https://github.com/damiancipolat/Cluster-servers/blob/master/doc/pm2-monit.png?raw=true)\n\nTo finish all the process:\n\n```sh\n$ pm2 kill\n```\n\nTesting the process, using curl we can see that pm2 manage the request sending it to differents process, some like a load balancer. I'm not sure what is the distribution algorithm used.??\n\n```sh\ndamianlinux@damianlinux:~/Escritorio/repo/Cluster-servers$ curl http://127.0.0.1:8000\nHello from worker PID:27074\ndamianlinux@damianlinux:~/Escritorio/repo/Cluster-servers$ curl http://127.0.0.1:8000\nHello from worker PID:27042\ndamianlinux@damianlinux:~/Escritorio/repo/Cluster-servers$ curl http://127.0.0.1:8000\nHello from worker PID:27050\ndamianlinux@damianlinux:~/Escritorio/repo/Cluster-servers$ curl http://127.0.0.1:8000\nHello from worker PID:27060\ndamianlinux@damianlinux:~/Escritorio/repo/Cluster-servers$ curl http://127.0.0.1:8000\nHello from worker PID:27074\ndamianlinux@damianlinux:~/Escritorio/repo/Cluster-servers$ curl http://127.0.0.1:8000\nHello from worker PID:27042\ndamianlinux@damianlinux:~/Escritorio/repo/Cluster-servers$ curl http://127.0.0.1:8000\nHello from worker PID:27050\ndamianlinux@damianlinux:~/Escritorio/repo/Cluster-servers$ curl http://127.0.0.1:8000\nHello from worker PID:27060\ndamianlinux@damianlinux:~/Escritorio/repo/Cluster-servers$ curl http://127.0.0.1:8000\nHello from worker PID:27074\ndamianlinux@damianlinux:~/Escritorio/repo/Cluster-servers$ curl http://127.0.0.1:8000\nHello from worker PID:27042\n\n```\n\n**Note:**\n\nBe sure your application is stateless meaning that no local data is stored in the process, for example sessions/websocket connections, session-memory and related. Use Redis, Mongo or other databases to share states between processes.\n\nAnother resource on how to write efficient, production ready stateless application is The Twelve Factor Application manifesto. https://12factor.net\n\n## Docker \u0026 cluster server:\nTodo\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdamiancipolat%2Fcluster-servers","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdamiancipolat%2Fcluster-servers","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdamiancipolat%2Fcluster-servers/lists"}