How to deploy Node.js at scale

Updated: February 2018

When it comes to DevOps, it's all about one thing: automation. Time is money, and what is automated doesn't cost you time. Paradoxically, the smaller your team is and the fewer resources you have, the more important it is to focus your energy on the important things. You don't want to waste your time tweaking servers and setting up deployments for days or weeks. You'll want something that works out of the box and scales well, automatically. This should be the mindset to adopt when looking for a solution to deploy any application.

Deploying Node.js with a "care free" provider

To automate deployments, a lot of people immediately think about containerization and Docker. While it's nice that this will give your server detailed installation instructions, for Node.js applications this often isn't necessary. Think about it. What do you actually need to know to install most Node.js applications? When you download something from Github and want to install it, what do the installation instructions usually say? It's usually just the following two commands:

npm install
npm start

Since Node.js has strong community standards on how to setup projects, server providers can actually use those conventions to make it possible for you to "just deploy" your application without overspecifying how your project is set up. So what you'll need is some provider that knows about those conventions in the Node.js community and thus lets you deploy easily.

The provider with the most simplicity is Heroku. Their Node.js tutorial is so easy, you won't even believe that this will work. Try for yourself: https://devcenter.heroku.com/articles/getting-started-with-nodejs. You'll cover the most important aspects of deployment in about 30 minutes. After this you'll have a deployed service, HTTPS and everything included. And that's for free. It's ideal to get started.

But this tutorial wouldn't be called "deploy node.js at scale" if it wouldn't go beyond an initial setup. Heroku also outshines at actual scaling, not only at time saving in the initial deployment and configuration process. And that's because they have the possibility to autoscale your servers. It will automagically detect high loads on your server and add more servers as necessary. It'll also shut them down if not used. You can set a server limit to control costs. You'll also get notifications if you wish. If you have 10 microservices, you don't want to determine server capacity for each one, only to see them crash in peak hours. What you want is to sleep well while your tools do the work for you. And that's what the autoscaling of Heroku will do for you. What's the downside? It's expensive. To get the autoscaling features, you'll have to go with instances that cost 250$ a month, at least. Ouch. Again, what you get is a superb "care free package". Load balancing? Taken care of for you. SSL? Taken care of. Retrieving the server logs? Simply run heroku logs. Scaling? Works out of the box.

One possible alternative to Heroku is Amazon Web Services' (AWS) Elastic Beanstalk. Here you'll have to invest some more time into setting up the deployment process and server config. This is partly because the tutorials just aren't as good as the ones provided by Heroku. Compare the Heroku tutorial to the AWS tutorial. Another issue is the incredible configurability and richness of services in AWS. This is both, a blessing and a curse. At first, it's like finding your way in a maze. But once you're familiar with Elastic Beanstalk and other AWS Services, you'll save a considerable amount of money compared to Heroku to get the same server capacities. Plus Elastic Beanstalk is more conifgurable, which you might eventually need. Plus you can also have the rest of your server infrastructure on AWS, which gives you a nice "all in one experience".

Deploying Node.js apps written in TypeScript

And what about Node.js apps written in TypeScript? It's just as easy, simply add TypeScript to your npm dependencies and provide a start script like the following:

"scripts": {
  "start": "./node_modules/.bin/tsc && node dist/server.js"
},
"dependencies": {
  "typescript": "^2.6.2",
  "@types/body-parser": "^1.16.4",
  ...

What's also important is to add your @types to the npm dependencies (not devDependencies), as otherwise the compilation process will fail on Heroku.

So to summarize this part:

Furthermore it's just as easy to deploy TypeScript backed Node.js apps!

If you liked what you read, subscribe below to get more of the same.

Deploying Node.js with Docker (Swarm)

Deploying with the "care free packages" can get really expensive. Of course, hiring new people can be really expensive as well. Luckily, with Docker Swarm setting up a fully scalable jungle of Node.js microservices isn't as hard anymore as it used to be. Still, it is considerably more to learn than with Heroku.

If you're new to Docker, then you'll have to familiarize yourself with a lot of concepts first: Docker containers, Docker images, Docker services, Docker swarm, Docker stacks, worker and manager nodes, load balancers, Dockerfiles, docker-compose, the Docker-CLI, Docker Cloud and on top of that some knowledge about networking, ports, operating systems, VM's and containerization is required to really know what you're doing...

Why is there so much more to learn and understand than with Heroku? Because the people at Heroku understand those things for you and have set up a system that has some good standards preconfigured for Node.js. Docker on the other hand is quite configurable and you can deploy anything with it. If you learn how to deploy with Docker, you won't limit yourself to any language or host. This has many advantages, for example if you want to migrate e.g. from Azure to AWS, with Docker you will have no lock-in effect.

So in summary, docker gives you flexibility and cheaper infrastructure at the price of having to learn a lot. In the end it's up to you and your needs which way to go. Is it the comfort of Heroku or the flexibility of Docker? Only you can decide this.

In case you decide to go the route of dockerizing your fleet, we've made a couple of videos to get you started.

Here's the last video of the series for setting up docker swarm:

The code of the tutorial series is available on Github at https://github.com/tsmean/docker-tutorial

Conclusion

You have multiple options to deploy your node applications. I would recommend to get started with a carefree option since it's really easy to set up, but once you feel it's getting too expensive or you need more customization power, you can switch to dockerizing your applications. I hope you've learned something, don't forget to sign up for the newsletter if you wan't to stay up to date with fullstack TypeScript!

Interested in TypeScript?