Senior
How would you organize the interaction between a facade service (accepts user requests) and a report generation service (a long operation)?
sobes.tech AI
Answer from AI
To organize interaction between a facade service and a report generation service, where reports are generated slowly, it is better to use an asynchronous approach:
- The facade service accepts a request from the user and immediately returns a confirmation that the request has been received.
- The report generation request is placed in a message queue (e.g., RabbitMQ, Kafka).
- The report generation service subscribes to this queue and processes tasks as they arrive.
- After completing the report, the service can notify the facade or directly inform the user (via WebSocket, push notifications, or email).
This approach allows not to block the user and to scale report processing.
Example in Java using a message queue:
// Sending a task to the queue
reportQueue.send(new ReportRequest(userId, parameters));
// Handler in the report service
public void onMessage(ReportRequest request) {
Report report = reportService.generateReport(request);
notificationService.notifyUser(request.getUserId(), report);
}