Large Web application needs to have some strategy to ensure web run smoothly. It is also intended to avoid Server down on busy hours.
Imagine thousand to million hits your Server simultaneously. Application could be down even with great hardware specs.
In Hardware point of view, you can do Load Balancing, Traffic Manager, Scale out by adding more Virtual Machine or Scale in by getting higher specs i.e CPU, Memory, HD.
The infrastructure setup is even easier using popular Cloud Portal available widely in market i.e Ms Windows Azure, Amazon Web Service, Google Cloud Platform.
You don’t need to have physical servers on your room. Cloud Portal has menu to create VMs and there are several CPU, Memory, Storage specs options. More high the specs then more costly.
Also you can do Load balancing, Auto/Manual Scaling, Traffic Manager with just some clicks on the Portal.
However considering only H/W and infrastructure sometimes is not enough.
You don’t want to be forced urgently to add more VM or increase H/W specs just because actually you are not managing your application or software properly.
Modern enterprise application use Multi Tier architecture. They use Web and Worker tier for handling users request. On back end side there are database and storage tier.
Web tier contains user interface i.e html body, event handler, etc. Worker is a background process that continuously run and usually used to process jobs which require more computational resources asynchronously .
The main purpose to have web & worker tier is to avoid requests burden application too busy and further more causing your server not responding.
Lets say we have an incident example. There is a web app which has reporting feature sent to user in PDF format. Many clients request for reports simultaneously and those requests are directly processed by database server on same time.
Also after data is retrieved, the Web tier creating many PDF based on requests and send them to clients by email.
Slowness will occur and thus Server either web tier or database will not responding. Not to mention if web tier and database is in one VM only.
You need to stop requests by killing them or just restart the server.
This case is a good example for explaining the need of Worker. So Web tier will sent user request by writing a message to Message Queue instead of sent requests directly to back end or database tier.
Message Queue is just text stored in a Message broker and it has FIFO (First In First Out) for write & read those messages. In this scenario, Web Tier write a request as message to Message Queue
Worker instance that run continuously on background reads client request or message from Message Queue and creating PDF report. The PDF report creation will be sequential based on FIFO of message queue.
So only one report processed in a time for one Worker instance. Lets say you want more report generated in time then you can generate another Worker instance. So you have two or more Workers.
You can manage how many workers you can have based on Server performance.
After Report is generated either that Worker which auto send the report or you may create another Worker to do it.
So first Worker that generate report will write a message to Queue when report is done. Second Worker read a message and send the report.
Below is the schema of multi tier application with Web & Worker Tier.
It is a common scenario and also as a tutorial example in internet since it is understandable with ease.
However this blog post source code is not presenting above schema.
This time we assume for having a Photo Web App and it has more than thousands hits every day like Instagram. This example will retrieve list of photos of followed persons / owner.
Since the Web app hits is quite heavy then we need some strategy using multi tier application.
Basic method to retrieve an image is to download it from database via Web tier and show it to browser. We won’t do that way. We will use Worker to retrieve photos from DB and store them to file storage.
Web tier will show those photos to client browser. The communication between Web & Worker tier use Message queue i.e RabbitMQ.
Please look the schema below:
The difference for this case is the Message Queue has ‘reply-to’ feature. So every request will be replied through Message Queue to ensure Web tier (client) get response immediately after photos are generated.
Many Message Queue available on internet but I use RabbitMQ because of easiness to install and easy to code also.
If you have not familiar yet with RabbitMQ then I suggest you to go to this Url: https://www.rabbitmq.com/getstarted.html and install it to your PC/laptop/dev server.
There is also nice step by step tutorials so you can go through it.
Continue reading →