DEV Community

Cover image for Scaling & DB Optimization
Jayant
Jayant

Posted on

Scaling & DB Optimization

Vertical Scaling

What

Vertical Scaling means increasing the size of your machine so that it can take more load essentially more number of req/sec.

Vertical Scaling in Single Threaded language*
In languages like NodeJS which are single threaded,

Is there any benefit of doing Vertical Scaling?

Vertical Scaling

As you can see the program is only running on single core, it's not utilizing other cores.

So it is not advised to vertically scale your NodeJS application more. However some vertical scaling is good as it will increase the memory and storage of our machine.

NodeJS is for IO intensive tasks, like Reading and writing to the DB, Reading & writing to files, making network requests.

If you want to do computational heavy work then you can use something called cluster modules & worker threads. This way your Node JS application can spawn multiple threads.

Vertical Scaling in Multi-Threaded language*

Scaling

In languages like Rust/Go/Java we can spawn multiple threads and there will be one main program that handles all that.

Capacity Estimation

Key Takeaways:

  • How would you scale your server?
  • How do you handle spikes?
  • How can you support a certain SLA given some traffic?

Start with doing some basic maths

Capacity Estimation

We would use Auto Scaling Groups for scaling our servers.
If our avg. CPU usage is 50% then we have scale up, if it is less then we need to scale down.

Capacity Estimation

Here is how, we handle the spikes, we have machines all across the regions and maybe different cloud providers also. Every 5 Minutes the server sends the no of requests it is receiving to a aggregator service which then make decision does we have scale it or not.

IN Case of persistant Connections, Like in Chess app.

Scaling

Here the no of connections a server can keep is very less as they are ws connections. So in case of Scaling up the server the process will be same.
But in case of scaling down, we need to reconnect the users as they got disconnected for sometime.

In Streaming Platform we use HLS Protocol [HTTP Live Streaming] and data is received in chunks.

In Case of Apps like Replit & Video Transcoding App.

REPLIT
In case of this we can't use a queue based approach as we need some compute instantly.
So we keep a warm pool of computers ready as they get occupied we increase our servers.

YOUTUBE
The best way is to use a queue but in this case as our queue gets very big we scale our workers/servers.

SLA [Service Level Agreement]

It is a contract between a service provider and a customer that defined the expected performance and availability of the service.

Let's take and Example

Deals between Jio Hotstar and AWS, AWS promise 99.99% uptime.

So there can only be 0.01% downtime.

Lets do some calculations

365*0.01 = 0.0365 days.

In Minutes

0.0365*24hour/day*60Minute/day

52.62 minutes.

Now Let's say IPL Happens 2 time in a year.
So we have approx 25-30 minutes to up system if they crash.

we have to use some monitoring service/ aggregator service that sees and autoscale.

Horizontal Scaling

Increasing the numer of instances based on some metric so that our application can support more load.

AWS has concept of Auto Scaling Groups that autoscale the instances based on some metrics.

Another approach would be autoscaling via containers as they spinoff very fast.

Key Terminology

  • Load Balancer : It's the entry point for the user's request after that it forward that to one of many machines [Target Groups], It is fully managed by the AWS so we don't need worry about scaling this. It's highly available.
  • Images [AMI - Amazon Machine Image] : Snapshot of a machine from which we can create more machine.
  • Target Group : A Group of EC2 Machine that a Load Balancer can send request to, when ASG increases the machines it assign that instance to a particular Target Group.
  • Launch Template : A Template that can be used to start new machine. [ Same as Docker Compose file ].
  • SSH : Its an network protocol used for securely accessing and managing remote computers.
  • Inbound Rule : They are firewall or security Group rules that control the incoming network traffic to a server.

Indexing

This of it like Index Page of a Book, with that we can easily access content of the book. Our Reads gets very Fast.

Working

When we create an Index, a new Data Structure is Created usually B-Tree that stores mapping from indexed_column to its location.
Search on Index is `Log(n)`, cuz the Indexed_column is sorted, and we just need to do a binary search.
But after doing this optimization, our writes will take much time as we have to add that to our Index also.
Enter fullscreen mode Exit fullscreen mode

Indexing

Normalization

It is a process of removing redundant data from DB. Redundant Data means data is present more than 1 place.

Normalizing Data : Decomposing table to eliminate data redundancy and data integrity.

we have several category.

  1. 1NF
  2. 2NF
  3. 3NF
  4. BCNF (Boyce-Codd Normal Form [advanced form of 3NF])
  5. 4NF
  6. 5NF

Our aim is to reach 3NF/BCNF or above.
The more we move down ,our table get more normalized. But over normalization is also not recommended cuz we have to do more joins.

1NF

  • A single cell must not hold more than one value (atomicity) ### 2NF
  • is already in 1NF
  • Has 0 partial dependency. [if table has composite primary key and some column is dependent is only one PK then it need to be resolved.]

Normal Form

3NF

  • be in 2NF
  • have no transitive partial dependency [some field in Table is indirectly related to the PK.]

Normal Form

Top comments (1)

Collapse
 
nadeem_zia_257af7e986ffc6 profile image
nadeem zia

good information provided