{"id":8351,"date":"2013-09-22T19:13:13","date_gmt":"2013-09-22T19:13:13","guid":{"rendered":"https:\/\/gosqeng.test\/?p=338"},"modified":"2019-11-28T12:28:14","modified_gmt":"2019-11-28T12:28:14","slug":"give-your-node-js-server-100-more-welly","status":"publish","type":"post","link":"https:\/\/www.gosquared.com\/blog\/give-your-node-js-server-100-more-welly","title":{"rendered":"Give your node.js server > 100% more welly"},"content":{"rendered":"<p>Typically, node.js applications run as a single process. A process can only run on a single CPU core at a time. There&#8217;s nothing more to gain if your machine only has a single core, but if it&#8217;s got a multi-core CPU, your node.js app is missing out on all the potential juice from the extra cores.<\/p>\n<p>If your node.js application is a TCP \/ HTTP \/ HTTPS server that listens on a port, you can make use of node.js&#8217; native <a href=\"http:\/\/nodejs.org\/api\/cluster.html\" target=\"_blank\" rel=\"noopener noreferrer\">cluster<\/a> module to take advantage of multiple CPU cores for &gt; 100% more processing power, depending on the CPU.<\/p>\n<h3>Clutch: a module for web server apps<\/h3>\n<p>We run many web servers written in node.js, many of them handling a large number of connections and requests, such as our API. To maximise the capacity offered by our servers, we&#8217;ve developed\u00a0<a href=\"https:\/\/npmjs.org\/package\/gs-clutch\" target=\"_blank\" rel=\"noopener noreferrer\">clutch<\/a>. Clutch is a convenience wrapper around cluster which helps you achieve the benefits of cluster in your web server app with minimal effort. It handles spawning, monitoring and replacing worker processes, as well as graceful shutdowns. All you need to do is include it in your server, tell it how many workers you want to spawn (which can be as easy as os.cpus().length) and it will handle the clustering magic.<\/p>\n<h4>Multiple processes, multiple cores<\/h4>\n<p>When clustering, your app becomes a &#8216;master&#8217; process which can spawn any number of worker processes. Incoming connections to the listen port are then efficiently load-balanced across these workers by the operating system. Since each worker is its own process, a multi-core CPU can run more than one concurrently. As a result, your server is able to process more requests than it could as a single process, as well as making use of previously un-tapped CPU capacity.<\/p>\n<h4>Multiple processes, more resilience<\/h4>\n<p>Running processes in a master \/ worker arrangement means the master process effectively becomes a watchdog and manager for its workers. If any workers crash, the remaining healthy workers will continue to accept connections while your master replaces the dead worker with a new one. While you should always work to fix any crashes, this improves the availability of your server when problems occur and is a convenient opportunity to implement logging and monitoring around application crashes. Of course, it is also advised that your master process itself is booted and supervised by a service such as upstart in Ubuntu, just in case that crashes too!<\/p>\n<h4>Controlled termination<\/h4>\n<p>In cluster mode, processes listen out for system signals (SIGTERM, SIGINT, etc) that would otherwise terminate the process right away. Instead, these signals initiate the shutdown behaviour in clutch, where the master ensures all workers have exited before it quits itself.<\/p>\n<p>A convenient feature of this is a controlled shutdown procedure before the process terminates. This gives you the opportunity to close off any open connections, finish pending work, and otherwise close off any activity that should complete before the process exits. Clutch implements a process shutDown event when it is ready to quit. Your application should listen for this event, run its shutdown procedure, and then call process.exit when it is ready to finish.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Typically, node.js applications run as a single process. A process can only run on a single CPU core at a time. There&#8217;s nothing more to gain if your machine only has a single core, but if it&#8217;s got a multi-core CPU, your node.js app is missing out on all the potential juice from the extra cores.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1452],"tags":[],"class_list":["post-8351","post","type-post","status-publish","format-standard","hentry","category-engineering"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v18.6 (Yoast SEO v19.0) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Give your node.js server &gt; 100% more welly - GoSquared Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.gosquared.com\/blog\/give-your-node-js-server-100-more-welly\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Give your node.js server &gt; 100% more welly\" \/>\n<meta property=\"og:description\" content=\"Typically, node.js applications run as a single process. A process can only run on a single CPU core at a time. There&#039;s nothing more to gain if your machine only has a single core, but if it&#039;s got a multi-core CPU, your node.js app is missing out on all the potential juice from the extra cores.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.gosquared.com\/blog\/give-your-node-js-server-100-more-welly\" \/>\n<meta property=\"og:site_name\" content=\"GoSquared Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/GoSquared\" \/>\n<meta property=\"article:published_time\" content=\"2013-09-22T19:13:13+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-11-28T12:28:14+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@TheDeveloper\" \/>\n<meta name=\"twitter:site\" content=\"@GoSquared\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Geoff Wagstaff\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.gosquared.com\/blog\/#organization\",\"name\":\"GoSquared\",\"url\":\"https:\/\/www.gosquared.com\/blog\/\",\"sameAs\":[\"https:\/\/instagram.com\/gosquaredteam\",\"https:\/\/www.linkedin.com\/company\/go-squared-ltd.\",\"https:\/\/www.facebook.com\/GoSquared\",\"https:\/\/twitter.com\/GoSquared\"],\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.gosquared.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.gosquared.com\/blog\/wp-content\/uploads\/2015\/07\/gosquared.png\",\"contentUrl\":\"https:\/\/www.gosquared.com\/blog\/wp-content\/uploads\/2015\/07\/gosquared.png\",\"width\":1270,\"height\":250,\"caption\":\"GoSquared\"},\"image\":{\"@id\":\"https:\/\/www.gosquared.com\/blog\/#\/schema\/logo\/image\/\"}},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.gosquared.com\/blog\/#website\",\"url\":\"https:\/\/www.gosquared.com\/blog\/\",\"name\":\"GoSquared Blog\",\"description\":\"Turn visitors into customers.\",\"publisher\":{\"@id\":\"https:\/\/www.gosquared.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.gosquared.com\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.gosquared.com\/blog\/give-your-node-js-server-100-more-welly#webpage\",\"url\":\"https:\/\/www.gosquared.com\/blog\/give-your-node-js-server-100-more-welly\",\"name\":\"Give your node.js server > 100% more welly - GoSquared Blog\",\"isPartOf\":{\"@id\":\"https:\/\/www.gosquared.com\/blog\/#website\"},\"datePublished\":\"2013-09-22T19:13:13+00:00\",\"dateModified\":\"2019-11-28T12:28:14+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.gosquared.com\/blog\/give-your-node-js-server-100-more-welly#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.gosquared.com\/blog\/give-your-node-js-server-100-more-welly\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.gosquared.com\/blog\/give-your-node-js-server-100-more-welly#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.gosquared.com\/blog\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Give your node.js server > 100% more welly\"}]},{\"@type\":\"Article\",\"@id\":\"https:\/\/www.gosquared.com\/blog\/give-your-node-js-server-100-more-welly#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.gosquared.com\/blog\/give-your-node-js-server-100-more-welly#webpage\"},\"author\":{\"@id\":\"https:\/\/www.gosquared.com\/blog\/#\/schema\/person\/56a3341790c8a0603f96066fb8d42448\"},\"headline\":\"Give your node.js server > 100% more welly\",\"datePublished\":\"2013-09-22T19:13:13+00:00\",\"dateModified\":\"2019-11-28T12:28:14+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.gosquared.com\/blog\/give-your-node-js-server-100-more-welly#webpage\"},\"wordCount\":529,\"commentCount\":1,\"publisher\":{\"@id\":\"https:\/\/www.gosquared.com\/blog\/#organization\"},\"articleSection\":[\"Engineering\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.gosquared.com\/blog\/give-your-node-js-server-100-more-welly#respond\"]}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.gosquared.com\/blog\/#\/schema\/person\/56a3341790c8a0603f96066fb8d42448\",\"name\":\"Geoff Wagstaff\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.gosquared.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/66792d2e4d04406697b9a5f322664691590a386bc15b7146d143bbca07aa8889?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/66792d2e4d04406697b9a5f322664691590a386bc15b7146d143bbca07aa8889?s=96&d=mm&r=g\",\"caption\":\"Geoff Wagstaff\"},\"sameAs\":[\"https:\/\/twitter.com\/TheDeveloper\"],\"url\":\"https:\/\/www.gosquared.com\/blog\/author\/echo\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Give your node.js server > 100% more welly - GoSquared Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.gosquared.com\/blog\/give-your-node-js-server-100-more-welly","og_locale":"en_US","og_type":"article","og_title":"Give your node.js server > 100% more welly","og_description":"Typically, node.js applications run as a single process. A process can only run on a single CPU core at a time. There's nothing more to gain if your machine only has a single core, but if it's got a multi-core CPU, your node.js app is missing out on all the potential juice from the extra cores.","og_url":"https:\/\/www.gosquared.com\/blog\/give-your-node-js-server-100-more-welly","og_site_name":"GoSquared Blog","article_publisher":"https:\/\/www.facebook.com\/GoSquared","article_published_time":"2013-09-22T19:13:13+00:00","article_modified_time":"2019-11-28T12:28:14+00:00","twitter_card":"summary_large_image","twitter_creator":"@TheDeveloper","twitter_site":"@GoSquared","twitter_misc":{"Written by":"Geoff Wagstaff","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Organization","@id":"https:\/\/www.gosquared.com\/blog\/#organization","name":"GoSquared","url":"https:\/\/www.gosquared.com\/blog\/","sameAs":["https:\/\/instagram.com\/gosquaredteam","https:\/\/www.linkedin.com\/company\/go-squared-ltd.","https:\/\/www.facebook.com\/GoSquared","https:\/\/twitter.com\/GoSquared"],"logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.gosquared.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.gosquared.com\/blog\/wp-content\/uploads\/2015\/07\/gosquared.png","contentUrl":"https:\/\/www.gosquared.com\/blog\/wp-content\/uploads\/2015\/07\/gosquared.png","width":1270,"height":250,"caption":"GoSquared"},"image":{"@id":"https:\/\/www.gosquared.com\/blog\/#\/schema\/logo\/image\/"}},{"@type":"WebSite","@id":"https:\/\/www.gosquared.com\/blog\/#website","url":"https:\/\/www.gosquared.com\/blog\/","name":"GoSquared Blog","description":"Turn visitors into customers.","publisher":{"@id":"https:\/\/www.gosquared.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.gosquared.com\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.gosquared.com\/blog\/give-your-node-js-server-100-more-welly#webpage","url":"https:\/\/www.gosquared.com\/blog\/give-your-node-js-server-100-more-welly","name":"Give your node.js server > 100% more welly - GoSquared Blog","isPartOf":{"@id":"https:\/\/www.gosquared.com\/blog\/#website"},"datePublished":"2013-09-22T19:13:13+00:00","dateModified":"2019-11-28T12:28:14+00:00","breadcrumb":{"@id":"https:\/\/www.gosquared.com\/blog\/give-your-node-js-server-100-more-welly#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.gosquared.com\/blog\/give-your-node-js-server-100-more-welly"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.gosquared.com\/blog\/give-your-node-js-server-100-more-welly#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.gosquared.com\/blog"},{"@type":"ListItem","position":2,"name":"Give your node.js server > 100% more welly"}]},{"@type":"Article","@id":"https:\/\/www.gosquared.com\/blog\/give-your-node-js-server-100-more-welly#article","isPartOf":{"@id":"https:\/\/www.gosquared.com\/blog\/give-your-node-js-server-100-more-welly#webpage"},"author":{"@id":"https:\/\/www.gosquared.com\/blog\/#\/schema\/person\/56a3341790c8a0603f96066fb8d42448"},"headline":"Give your node.js server > 100% more welly","datePublished":"2013-09-22T19:13:13+00:00","dateModified":"2019-11-28T12:28:14+00:00","mainEntityOfPage":{"@id":"https:\/\/www.gosquared.com\/blog\/give-your-node-js-server-100-more-welly#webpage"},"wordCount":529,"commentCount":1,"publisher":{"@id":"https:\/\/www.gosquared.com\/blog\/#organization"},"articleSection":["Engineering"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.gosquared.com\/blog\/give-your-node-js-server-100-more-welly#respond"]}]},{"@type":"Person","@id":"https:\/\/www.gosquared.com\/blog\/#\/schema\/person\/56a3341790c8a0603f96066fb8d42448","name":"Geoff Wagstaff","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.gosquared.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/66792d2e4d04406697b9a5f322664691590a386bc15b7146d143bbca07aa8889?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/66792d2e4d04406697b9a5f322664691590a386bc15b7146d143bbca07aa8889?s=96&d=mm&r=g","caption":"Geoff Wagstaff"},"sameAs":["https:\/\/twitter.com\/TheDeveloper"],"url":"https:\/\/www.gosquared.com\/blog\/author\/echo"}]}},"wps_subtitle":"Speeding up your Node apps","_links":{"self":[{"href":"https:\/\/www.gosquared.com\/blog\/wp-json\/wp\/v2\/posts\/8351","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gosquared.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gosquared.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gosquared.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gosquared.com\/blog\/wp-json\/wp\/v2\/comments?post=8351"}],"version-history":[{"count":0,"href":"https:\/\/www.gosquared.com\/blog\/wp-json\/wp\/v2\/posts\/8351\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.gosquared.com\/blog\/wp-json\/wp\/v2\/media?parent=8351"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gosquared.com\/blog\/wp-json\/wp\/v2\/categories?post=8351"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gosquared.com\/blog\/wp-json\/wp\/v2\/tags?post=8351"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}