My architecture looks like:
Each block is an individual service. There may be more upstreams and downstreams added in the future. The upstreams need to send email notifications to users. Each email is specific to a single upstream.
The question: Where should I put the code to call Email Service, in upstreams or downstreams?
I'm thinking the ideal obvious choice is in the upstreams, as the emails are related to the upstreams, not necessarily to the downstreams. Also, a single downstream endpoint may be used by multiple upstreams, so would be messy to add email logic for multiple upstreams in a single downstream endpoint.
However, in addition to upstream requests, there are background emails. For example, if something hasn't "happened" (determined by downstream) in 1 hour, then send an email. These are driven by cron jobs and queues. It would be nice to place this background email logic in the upstreams as well, however:
- Some of these queues must be consumed by downstreams for reasons unrelated to email. The downstream can produce to new queue consumed by upstream which sends email, but that's more engineering effort.
- Some downstreams already send background emails triggered by cron/queues, which would require migration to upstreams. Again, more effort.
Right now I'm thinking the approach to be:
- For emails triggered by upstream requests (synchronous flows), upstreams own the email logic.
- For emails triggered by background (async flows), downstreams own the email logic.
I feel this is the best tradeoff between design and effort.