Ruby developer and meetup organizer
Dec 2020
Scaling ... the content
Scaling ... the content
Scaling ... the intended audience
Reddit: 54 million users per day
few paying users a month
my blog
you are somewhere here
Scaling ... the intended audience
Your traffic looks like this
days of a month
no. of users
$
Scaling ... the intended audience
You need
Sudden scaling
Ad-hoc scaling
Unplanned scaling
Scaling ... the constraints
Scaling ... the constraints
The existing architecture
Puma
Nginx
Sidekiq
DB
Redis
~> Puma can't be scaled horizontally π
~> Db is separate π
~> No Heroku π
AWS EC2
RDS
Scaling ... the constraints
~> How many users are expected to arrive ? π
~> How quickly will they arrive ? π
~> When will they arrive ? π
~> Can we fail gracefully ? Shutdown some parts? Show notices like Reddit ? π
Scaling ... the constraints
How much can we afford ?
~> Not a venture funded business - costs have to be under control π
~> Incoming business allows some leeway π
Scaling ... the first strategy
Max requests we can serve at any point: p*w
The next batch of p*w requests will have to wait t seconds
No one can be made to wait more than the time out time (tx)
The Arithmetic
Scaling ... the first strategy
Peak viewers: 1500
Response time: 500ms
No. of Puma threads: 15
No. of Puma workers: 10
Β
We can handle 15 * 10 = 150 requests at a time
The next 150 wait for 500ms
Batches in waiting: 1500/150 - 1: 9
The last batch waits for 9 * 500ms: 4.5 seconds
Β
Not so bad π
The Arithmetic - a rough example
Scaling ... the first strategy
The Arithmetic - technical constraints
Β
So for the previous example -
Scaling ... the first strategy
Therefore, the architecture should become somewhat like this:
Puma
Nginx
Sidekiq
DB
Redis
AWS EC2
RDS
Puma
Puma
Puma
Puma
Scaling ... the first strategy
Puma
Nginx
Sidekiq
DB
Redis
AWS EC2
RDS
Puma
Puma
Puma
Puma
And we should be able to add Puma workers quickly as needed
Scaling ... the first strategy
This strategy failed miserably
Me at 3am: ππ’
while also questioning my life's choices
Scaling ... the first strategy
This strategy failed miserably
Because:
The last point actually worked, more on that later
Scaling ... the first strategy
Nginx
Default setting
By default -
Which means -
Scaling ... the first strategy
Nginx
Default setting
And also:
cmd$ ulimit -Sn 1024
Scaling ... the first strategy
Nginx - conclusions
Scaling ... the first strategy
The DB
Scaling ... the first strategy
The DB - connections
LEAST({DBInstanceClassMemory/9531392},5000)
Ref: https://sysadminxpert.com/aws-rds-max-connections-limit/
Scaling ... the first strategy
The request profile
unused features
Highest
Lowest
Traffic
heartbeats
admin functions
login page
suboptimal Api endpoint
Β Imaginary example
Scaling ... the first strategy
The request profile
Therefore:
Scaling ... the second strategy
Scaling ... the second strategy
The AWS Auto scale groups
The idea is -
The magic lies in the auto-scaling policy
Scaling ... the second strategy
The AWS Load Balancer
Scaling ... the second strategy
The AWS Load Balancer - ALB
Read up on these to configure ALB -
Scaling ... the second strategy
The AWS Auto Scaling
Example:
target == Puma VM
Scaling ... the second strategy
The AWS Auto Scaling - calibration/testing
To confirm that scaling actually happens when needed
Note the following for each test run:
Scaling ... the second strategy
The AWS Auto Scaling - calibration/testing
For the DB:
Scaling ... the second strategy
The AWS Auto Scaling - the results
Things worked fine π₯³
Scaling ... the aftermath
Scaling ... the conclusions/lessons