user3007447
user3007447

Reputation: 400

Accessing data between microservices when one is a console app

Let’s say I have microservice “UserManagement” and microservice “UserReportService”.

UserManagement is a web API project which exposes public endpoints. It is used to manage user information.

UserReportService is a console application that gathers each user’s info each day and generates a report — JSON data and a file. It stores it in its own database. Since this is a console application there is no http endpoint for any other service to access this data.

If my UserManagement microservice needs to expose this data via an API, how can I accomplish this?

I’ve thought of two ways:

  1. The console app saves the data to the UserManagement microservice instead of its own database via an http endpoint on the UserManagement microservice or via some kind of message queue.

  2. The console app saves the data to its own database and the data is replicated to the UserManagement microservice for consumption.

Upvotes: 1

Views: 101

Answers (1)

Mihail
Mihail

Reputation: 823

One very important thing in microservices is to each one manage its own data, and this data should be exposed through clearly defined APIs.

With that said, your 1st approach is incorrect, since one microservice would be bound to the other's database. The second one is not recommended for similar reasons.

The recommended approach would be the following:

┌──────────────┐   ┌───────────────────┐
│              │   │                   │
│UserManagement│   │ UserReportService │
│              │   │                   │
└─────┬────────┘   └────────┬──────────┘
      │                     │
      │                     │
      ▼                     ▼
┌──────────────┐      ┌────────────┐
│              │      │            │
│   UsersDb    │      │  ReportDb  │
│              │      │            │
└──────────────┘      └────────────┘
  • UserManagement is kept as is.
  • UserReportService should be converted to a Web API that follows one of the following approaches:
    1. It has a background service that generates the report and saves it in the database. This would work as a Cron Job.
    2. If you do not want a Cron Job, you can have an API that generates and saves the report when invoked.

This way the two microservices manage their own data, and can change internally as needed.

Upvotes: 0

Related Questions