eventsamazon-sqsamazon-snsevent-driven

AWS SNS — How generic should topics be and when should we reuse/create topics?


We are introducing SNS + SQS to handle event production and propagation in our micro services architecture, which has so far relied on HTTPS calls to communicate with each other. We are considering connecting multiple SQS queues onto one SNS topic. The events in the queues will then be consumed by a lambda or a service running in EC2.

My question is, how generic should the topics be? When should we create new topics?

Say, we have a user domain which needs to publish two events—created and deleted. Two options we are considering are:

OPTION A: Have two topics, "user-created" and "user-deleted". Each topic guarantees a single event type.

OPTION B: Have one topic, "users", that accepts multiple event types

Does anyone have a strong preference for either of the options and why would that be?

On a related note, where would you include the cloud configuration for each of the resources? (should the queue resource creation be deployed together with the consumers, or should they live independently from any of the publishers/consumers?)


Solution

  • I think you should go with Option B and keep all events concerning a given "domain" (e.g. "user") in a single topic:

    Concerning your other question: Keep your topic/infrastructure configuration separate from your services. It's an individual piece of infrastructure (like a database) and should kept separate; especially if you introduce more consumers & producers to your system.

    EDIT: This might be an example "setup":

    You can think about keeping the SNS & SQS infra code in a single repository (the last two), but I would strongly recommend everything specific to a certain service/lambda to be kept in separate repositories.

    Generally it helps to think about your topics as a "database", this line of thinking should point you in the right direction for all your questions.