System Design — Application layer
Application layer is the service layer that is responsible for exposing business logic to clients. On the other side, web servers accept and serve static pages (i.e. html, css, images and videos) from a website.
Application servers enhance the interactive parts of the website by executing server side logic. This layer facilitates longer running processes that are very resource-intensive.
Separating out web layer and application layer allows developers to scale both layers independently. This facilitates addition of more functionalities in application layer without breaking web layer. Workers in application layer also enable Asynchronism.
Disadvantages of application layer:
- Adds additional complexity in terms of deployment and maintenance
Asynchronous workflows reduce the request time for expensive operation that would otherwise be executed in-line. Asynchronism allows the client to add a task to queue(first-in first-out) for processing, and move onto the next task while the previous task is processed at its own pace in the background.
They can also help by doing expensive time-consuming operations in advance.
A message queue is a queue of messages that are sent between applications. It could be a series of tasks that are waiting to be processed.
Message queues receive, process, store and deliver large amounts of data. If an operation is too slow to be performed synchronously, the application(client or producer) can push the job to a message queue, which gives the acknowledgement to the client of receiving the message. A consumer picks up the task from queue, processes it and delivers message back to the client.
In this way, the user isn’t blocked and the job is being processed in the background. In the meanwhile, the client can optionally do a small task to make user feel that the task is completed. For ex: when the user publishes a blog, the blog gets published on the user’s feed instantly but it takes some time before blog can reach all the followers.
Advantage of message queue: Allows client to process other tasks without having to wait for the previous task to be finished.
Disadvantage of message queue: Adds additional operational complexity. Queue needs to be created, configured and monitored. Producer and consumer need to be configured to be able to send/receive message from queue. There are several tools that manage complexity, but they do not eliminate it entirely.
To implement message queues, you need message broker that delivers messages from one service to another.
Available message brokers:
- Apache Kafka — real-time streaming data pipelines with replication and high performance
- Amazon SQS — hosted, can have high latency and messages can be delivered more than once.
Task queue receives a task, processes them serially and delivers the result. They are used to run computationally-intensive tasks on the background so client doesn’t have to wait for a long time for task to finish-up processing. They also support scheduling.
Task queue is the secondary development of the message queue. Both the queues focus on processing jobs asynchronously but message queues focus on throughput and reliability while task queues focus on task execution.
Message queues are used to send data (or jobs) between services, while task queue is used to schedule tasks internally.
Available technologies for task queues:
- Celery — supports scheduling, developer friendly
If queue size starts to become larger than allotted memory, this results in disk reads and lower performance. If the rate at which producer adds jobs to the queue is much more than the rate at which consumer processes the jobs, back pressure comes into picture.
One solution to handle job overload is by adding back pressure when overloading starts to happen.
Back pressure can help by limiting the queue size, thus maintaining high throughput and good response times for jobs already in queue. As soon as the queue fills up, the client gets http 503 status code(retry later) or gets server busy response and client wouldn’t be able to add any job to the queue. Clients can retry the request after some time.
Advantages of Asynchronism:
- Reduces dependency between services while still communicating with each other
- One application getting down doesn’t impact other services as the jobs are stored in queues and can be processed when ready or retried, if needed.
Disadvantages of Asynchronism:
- Real-time workflows might not be suitable for inexpensive tasks as introducing queues can add additional delays and complexity.
Microservices are a suite of independently deployable, small and modular services. Each service has a separate process and communicate with each other via light-weight mechanism to serve business goal.
For ex: Payments could have following independent services like authorize card, capture card, process refund and reverse authorization.
Thank you for checking out my blog!
Follow to get notified of upcoming system design blog! The upcoming blog will be about client-server communication