Scaling a MeteorJS SaaS app on AWS

46
Scaling a MeteorJS SaaS app on AWS Brett McLain Application Systems Architect PotashCorp

Transcript of Scaling a MeteorJS SaaS app on AWS

Page 1: Scaling a MeteorJS SaaS app on AWS

Scaling a MeteorJS SaaS app on AWS

Brett McLainApplication Systems Architect

PotashCorp

Page 2: Scaling a MeteorJS SaaS app on AWS

Agenda

• Overview of my application.• Overview of MeteorJS (architecture,

deployment tools, clustering).• Overview of Amazon (EC2, ELB, EBS, Route 53,

Auto Scaling).• My journey of trying to scale MeteorJS on

Amazon Web Services.

Page 3: Scaling a MeteorJS SaaS app on AWS

What should you get out of this talk?

• A better understanding of MeteorJS.• A better understanding of Amazon Web

Services.• How to deploy, cluster, and scale a MeteorJS

web app.

Page 4: Scaling a MeteorJS SaaS app on AWS

Application Overview

• SaaS application that uses Twilio to send SMS and phone calls to devices that connect to the cell phone network.

• MeteorJS (runs on Node.js)• MongoDB• Flot Charts• Twitter Bootstrap• Blah blah blah.

Page 5: Scaling a MeteorJS SaaS app on AWS

What is MeteorJS?

• "Meteor is a full-stack JavaScript platform for developing modern web and mobile applications.“

• Out of the box tight integration with MongoDB + full data reactivity.

• Install MeteorJS and have an app written in minutes with well established and easily understood patterns.

• Hot code push.• Full data reactivity.• Easily build for Android and iPhone (hot code push).

Page 6: Scaling a MeteorJS SaaS app on AWS

Meteor Architecture

• Production deployments are NodeJS applications.• Meteor has it’s own package manager called

isobuild. Extremely similar to NPM.• Meteor can also use NPM packages now on the

client or server (as of version 1.3).• Supports ECMAScript 6 (ES 2015).• Meteor Up (mup) - Meteor deployment plugin.• Meteor Cluster - Meteor clustering plugin.

Page 7: Scaling a MeteorJS SaaS app on AWS

Meteor Architecture

Page 8: Scaling a MeteorJS SaaS app on AWS

Meteor Deployments

• I use Meteor Up (mup) for deployments.• Single command to build and deploy code to an

unlimited number of servers in a rolling fashion.• Installs all necessary software automatically

(NodeJS, npm, MeteorJS, docker, nginx, mongo).• Setups up an nginx reverse proxy in front of a

docker instance for meteor and a docker instance for mongo.

• 10 seconds of downtime per instance!

Page 9: Scaling a MeteorJS SaaS app on AWS
Page 10: Scaling a MeteorJS SaaS app on AWS

Meteor Cluster

• Provides clustering capability within Meteor JS apps.

• Meteor Cluster is a regular meteor package.• Turns each meteor server into a load balancer

or a DDP instance (backend server).New nodes register with MongoDB backend and are auto-discovered. Traffic begins routing to them immediately.

Page 11: Scaling a MeteorJS SaaS app on AWS

Round Robin DNS Load Balancing

Page 12: Scaling a MeteorJS SaaS app on AWS

1 Load Balancer, 2 DDP Servers

Page 13: Scaling a MeteorJS SaaS app on AWS

2 Load Balancers, 2 DDP Servers

Page 14: Scaling a MeteorJS SaaS app on AWS

Meteor Clustering

• Load balancer servers are pointed to by DNS records.

• Once the client connects to a load balancer server, it *might* receive the address of a DDP server to share out the load.

• Supports clustering of microservices too!

Page 15: Scaling a MeteorJS SaaS app on AWS

So…now what?

• Meteor has some great deployment and clustering tools, but how do we deploy it in a scalable way?We need to pick a host that fits well with our deployment tools and utilize the clustering capabilities of Meteor.

Page 16: Scaling a MeteorJS SaaS app on AWS

Initial Objectives

• Low Cost!• Availability• Performance• Low Maintenance

Page 17: Scaling a MeteorJS SaaS app on AWS

Where to start?

• Originally hosted everything (prod, dev, mongo, reverse proxy) on a single Virtual Private Server (VPS).

• I have past experience with Amazon, it’s well documented, tons of tools.

• Google *appears* pricier, less tools.• Digital Ocean…?

Page 18: Scaling a MeteorJS SaaS app on AWS

So what does Amazon offer?

• EC2 (Elastic Compute Cloud)• Elastic Bean Stalk• Elastic Load Balancers• Route 53 (DNS)• OpsWorks• CloudFormation• Elastic Block Store• The list goes on!

Page 19: Scaling a MeteorJS SaaS app on AWS
Page 20: Scaling a MeteorJS SaaS app on AWS

Elastic Compute Cloud (EC2)

• On demand infrastructure.• Amazon Machine Image (AMI) of many popular

operating systems, platforms, community images, etc.• Reserved Instances - t2.micro = $0.006/hour - 3 year

contract. Relatively cheap.• Spot instances (bid for unused servers @ 80% of

cost).• Can apply auto-scaling groups.• Fairly basic...let's see what else Amazon has!

Page 21: Scaling a MeteorJS SaaS app on AWS

EC2 Availability Zones + Regions

• 11 Regions (i.e. US East)

• 33 Availability Zones• us-east-1a• us-east-1b, etc are

availability zones while US East (N. Virginia), US West (Oregon), Asia Pacific (Tokyo)

Page 22: Scaling a MeteorJS SaaS app on AWS

EC2 Regions

Page 23: Scaling a MeteorJS SaaS app on AWS

Elastic Beanstalk

• Elastic Beanstalk is a Platform as a Service (PaaS) that handles the complexities of your underlying infrastructure.

• Manages load balancing (via ELBs), instance scaling, instance health monitoring, and deployments.

• Supports Docker, Go, Java, .NET, Node, PHP, Python, Ruby.

Page 24: Scaling a MeteorJS SaaS app on AWS

Let's see how this goes...

• Elastic Beanstalk seemed like a great solution. Handles scaling and load balancing (with ELB).

• Ran into problems very quickly...• MeteorJS settings are a JSON file exported as an

environment variable. Beanstalk strips environment variables of a number of special characters!

• Quickly realized that it is not possible to do websockets, sticky sessions, and HTTPS with Elastic Load Balancers.

Page 25: Scaling a MeteorJS SaaS app on AWS

How about OpsWorks?

• OpsWorks is a configuration management service that helps you configure and operate applications of all shapes and sizes using Chef.

• You can define the application’s architecture and the specification of each component including package installation, software configuration and resources such as storage.

Page 26: Scaling a MeteorJS SaaS app on AWS
Page 27: Scaling a MeteorJS SaaS app on AWS
Page 28: Scaling a MeteorJS SaaS app on AWS
Page 29: Scaling a MeteorJS SaaS app on AWS
Page 30: Scaling a MeteorJS SaaS app on AWS

Nope!

• Stood up a MongoDB cluster and two separate Meteor clusters.

• Autoscaling and application stack health-checks are nice!

• Didn't bother using Chef as I have my own deployment tools.

• Ran into problems very quickly...• "running_setup" never ended. Servers would

hang. No support from Amazon!

Page 31: Scaling a MeteorJS SaaS app on AWS

Ok, uh, what else is left?CloudFormation?

• CloudFormation allows you to define infrastructure and services as a configuration template.

• Provision servers, storage, networks, NATs, relationships, run scripts, etc. all from a JSON config.

• Rapid deployment of an entire stack, extremely powerful and complicated.

Page 32: Scaling a MeteorJS SaaS app on AWS
Page 33: Scaling a MeteorJS SaaS app on AWS
Page 34: Scaling a MeteorJS SaaS app on AWS

CloudFormation

• I used the MongoDB CloudFormation template provided by Amazon (only on github, not via the CloudFormation interface).

• Over time I abandoned it as the scripts they used for deployment contained a race condition that would cause deployment to fail about 50% of the time.

• For my purposes, this was overkill.

Page 35: Scaling a MeteorJS SaaS app on AWS

So, EC2 then?

• Setup 3 layers: (1) Load Balancers, (1) DDP Servers, (3) Mongo Replica Sets (no shards).

• Created 5 DNS records for the load balancers to do round robin DNS.

• Used Route 53 latency weighted routing to failover load balancers that are offline to load balancers that are online.

• 2 auto scaling groups, one for load balancers, one for DDP servers.

• Lets Encrypt for SSL/TLS.

Page 36: Scaling a MeteorJS SaaS app on AWS

Route 53

• Route 53 is a highly available and scalable DNS.Routing policies are:– Simple Routing– Weighted Routing– Latency Routing– Failover Routing

Page 37: Scaling a MeteorJS SaaS app on AWS

Down the rabbit hole…

• Auto scaling occurs when average CPU > 70% for 2 minutes.

• When auto scaling occurs in either auto scaling group, it creates a new instance using the same image.

• That image runs a script on boot...

Page 38: Scaling a MeteorJS SaaS app on AWS

do_all_the_things.sh

• Step 1: git pull to get latest code.• Step 2: Determine if an elastic IP address is already

assigned (if not, then this is a fresh instance).• Step 3a: If fresh instance, and DDP server, use auto-

assigned public IP.• Step 3b: If fresh instance, and load balancer, check to

see which DNS elastic IP's are unused and assign one.Deploy to self (build code and deploy to localhost).

• Step 4: Done!

Page 39: Scaling a MeteorJS SaaS app on AWS
Page 40: Scaling a MeteorJS SaaS app on AWS
Page 41: Scaling a MeteorJS SaaS app on AWS
Page 42: Scaling a MeteorJS SaaS app on AWS

Testing Methodology

• Used MeteorDown for load testing. It connects to an endpoint, opens up a DDP connect and completes some action (in this case a 200kb document transfer) before closing the connection.

• Scale up connections incrementally and monitor when and how fast instances are created.

• Determine total capability of 5 load balancers and 10 DDP servers (each instance is t2.micro, 1 vCPU, 512mb ram).

Page 43: Scaling a MeteorJS SaaS app on AWS

Testing Results

• First auto scaling event: provisioning of instance, boot, git pull, build, and deploy is 2 minutes 30 seconds.

• Second+ auto scaling event: 1 minute 10 seconds.

• Concurrent subscriptions achieved: 38,000• Maximum subscriptions per instance is reduced

by approximately 1% per added instance.

Page 44: Scaling a MeteorJS SaaS app on AWS

What I learned…

• Meteor has very powerful tools out of the box, however these tools do not lend themselves well to solutions such as Elastic Beanstalk or Google App Engine.

• DNS is a finicky mistress.• Scaling load balancers is hard; scaling a web app using

sticky websockets is even harder!• Diving in head first has some serious drawbacks - it's

difficult to navigate this space without someone to guide you.

Page 45: Scaling a MeteorJS SaaS app on AWS

What did I end up with?

• One line command to do rolling deployments to all my servers.

• 10 second downtime when deploying.• Highly available (across AZ's), scalable architecture

(auto-scaling + MongoDB sharding/replicas).• 38,000 concurrent connections constantly re-

establishing websockets and sending data.• Great support for microservices.• Reactive data and UI, hot code push, mobile

deployment.

Page 46: Scaling a MeteorJS SaaS app on AWS

What was the point of this?

• Hopefully you understand that it IS possible to scale MeteorJS applications, and that it isn't that hard (I just made it hard for myself).

• High level understanding of a few of Amazon's infrastructure and deployment services.