skip to Main Content

What is differences between this 2 approaches for creating a background task?:

  1. Inside XYZ solution creating a class which is implementing ‘IHostedService’ or ‘BackgroundService’ interface.
  2. Creating separate "Worker Service" project template.

2

Answers


  1. Monolith approach vs Microservice approach. Same pros/cons. Worker service is microservice, IHostedService implementation can live in both Microservice/Monolith, but usually just lives in Monolith web app.

    Login or Signup to reply.
    1. Creating separate "Worker Service" project template.

    If you check out the code for the Worker Service template you will see that it actually does the same as the 1st point – it inherits from BackgroundService and adds it as a hosted service, you can even place it in the same solution as the "main" app.

    The difference is basically "in-process" and "out-of-process" (relative to the "main" app) execution of worker, and the decision which one to use depends on what the worker does and how tightly coupled it is to the "main". If the processing is loosely coupled and have different scalability requirements – then it makes a lot of sense to move it to a separate project with separate deployment (but it can be still in the same solution, if your deployment infrastructure allows it). For example in my projects I often move some external queue (Kafka, RabbitMQ, etc.) processing parts into a separate project which is deployed as part of the service in a single deployment but in a separate process (pod in k8s).

    The first approach is useful when 1) you have some "internal" in-memory processing related to concrete instance of the app (encountered it rarely but still there are cases – some custom logging/metrics or local memory cache management) 2) you don’t have enough resources to implement the "out-of-process" (it can be more time consuming to implement and can require a bit more computing resources).

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search