Long-running tasks
In today's world, users expect applications or websites to be fast, and usually, a latency of around 100 milliseconds is considered reasonable. We can improve the latency of services by avoiding chatty APIs or leveraging materialised views for more expensive queries. However, for some long-running jobs like generating a PDF or performing a complex calculation, there is not much we can do to make them run faster rather than scaling up/out. In this article, I'm going to take a look at two patterns for handling such requests.
Messaging pattern
Messaging patterns help in connecting services in a loosely coupled manner. What it means is that services never talk to each other directly. Instead, a service generates and sends a message to a broker (generally a queue) and any other service that is interested in that message can pick it up and process it. We can use Bus or Queue for implementing the messaging pattern.
Throttling design pattern
Control the consumption of resources used by an instance of an application or service. This can allow the system to continue to function and meet service level agreements (SLA), even when an increase in demand places an extreme load on resources.
Strategy design pattern
With design patterns, instead of code reuse, we get experience reuse. For this pattern, we need to identify the aspects of our code that are changing with every new requirement and we need to pull it out and separate it from all other stuff that doesn't change.