DB interaction

My Views on handling website load (Basic version ;-) )

Handling load depends upon various things. May be I am not the right guy for this but I can share my thoughts over it. I made a flowchart which depicts basic website modal:-

Wesite basic Flow

Flow:-
Requests hits LB(Load Balancer) and LB passes the request to the server which is in rotation. Server picks up the request and do some business logic which includes talking to the services and then to the DB and then somehow requests completes and user sees the webpage in the browser.

Load Balancer:-
Every request comes directly to the Load Balancer. Its a smart device that acts as a reverse proxy and distributes network or applications traffic across no. of servers. They are used to increase capacity and reliability of applications. So it basically distributes requests to various servers based on the configured algorithm. Some Algorithm are:-

  • Round Robin
  • Weighted Round Robin
  • Least Connection
  • Least Response Time

CDN:-
A content delivery network or content distribution network is a large distributed system of servers deployed in multiple data centers across the internet. The goal of CDN is to serve content to the end users with high availability and high performance. They are basically used to store static contents. For eg:- Amazon cloud, Akamai, Microsoft Azure, etc.

Handling Load:-

In my views handling load is really hard task. First of all our code should be performant. One should not write unoptimised code. One should take care of the time and space complexities while writing code. When you write code, show it to your friend for code review so that he/she can point out your mistake.

Then comes the DB interaction. Minimum calls should happen to the DB. Use optimised SQL queries is a must. In my opinion the best practise is to write all the data to the file and write a CRON job which writes the data from the file to the DB. So there will be no direct connection to the database and your database will never be choked.

Caching should be used as much as you can. THe data which is constant or does not change like static HTML should be cached. Cache dynamic content for a constant amount of time say for 5 minutes and have some mechanism to refresh the cache. There are various system for caching like memcache, PHP:APC cache, etc.

Move static contents(CSS, JS, Images, Sprites, etc.) to CDN so that the requests for these contents will not hit LB. You can see in flowchart there is a dotted line between CDN and LB which depicts if CDN fails then the requests will go to LB for the particular content.

Write tests for your Application which can detect amount of load your website can handle. Write a script which will hit your website and checks for how many concurrent hits your website can serve response. This way you can judge your website capacity.

I don’t have the 100% knowledge of it but shared my views of what I had now. Will share all the other tips and tricks once I will explore it and will get more and more into it.