Skip to content

Messaging standards ​

This document outlines some of the standards we want to follow for consistency and ease of development when it comes to asynchronous messages

Topics ​

When publishing a message, we should aim to create a separate topic for the message and have one topic per message as per the Architecture Principals. Multiple queues can subscribe to the topic (fan-out pattern).

Message Naming ​

Messages, and their respective SNS topics should be named in the past tense. They should represent the action that has happened to a domain model. It should follow the following semantic: domainModel-pastAction

Example:

andi-created
contact-updated
organisation-deleted

When actions need to happen from a messaging being published, for example, sending an email, you may use the requested word.

e.g. Rather than send-email we can use email-send-requested

Message Payload ​

We should aim to make our messages as descriptive as possible, and allow for the consumers to only consume and store the information they need. Messages should include all their information within the payload key:

{
  payload: {...}
}

Good: contact-created

{
  payload: {
    id: contactID,
    firstName: 'Sam',
    lastName: 'Smith',
    organisation: {...}
    ... // all the info about the contact
  }
}

Bad:

contact-created

{
  contactId: contactId,
  payload: {
    firstName: 'Sam'
    lastName: 'Smith'
  }
}

Consuming Messages ​

When consuming messages from SNS/SQS we need to account for the following constraints:

  • Account for the same message being consumed multiple times - SNS guarantees at least once delivery but we may receive the same message twice or more

  • Account for messages coming in random order - we are not currently using First In First Out (FIFO) queues so our messages can be received out of the order they were published

  • Account for handling errors

  • Account for correct message payload

Therefore when implementing your functionality consider:

  • Will consuming the message create/update data more than it has to?

  • What is the latest, up to date, data we want to end up with?

  • What is the correct way to error when something goes wrong? Gracefully vs DLQing the message

Idempotency ​

When consuming messages SNS guarantees at least once delivery - but not single delivery. This means the same message may come in more than once - a duplicate message.

When consuming messages we should build the functionality in such a way that it can process multiple of the same messages with grace. You should consider if the same message comes in twice:

Idempotency can be achieved in most cases by checking the timestamps of the data model within the payload of the message against the timestamp of the data model in the message handler or if the item already exists.

For creates and deletes we can check if the data exists.

For example, if we’re consuming the domainA-created message to add to domainB’s data - also known as a bulk head - we should check if domainB doesn’t have domainA data already. A code example:

if (domainA-updated.id NOT exist in domainB-domainAdata) {
  create domainB-domainAdata
} else {
  Log to say it already exists
  exit gracefully.
}

For updates we can check the timestamps.

For example, if we’re consuming the domainA-updated message to ensure the domainA bulk headed data is up to date in domainB we should check if domainA-updated message has an updatedAt that is more recent or equal to that of what we have in domainB for domainAs bulkhead. A code example:

if (domainA-updated.payload.updatedAt >= domainB-domainAdata.updatedAt) {
  // domainA-updated message data more up to date ...
  update domainB-domainAdata with domainA.payload // note updatedAt should be updated aswell to what is in the message
} else {
  // domainA-updated message data is less up to date than what we have... so don't update
  Log to say it is an older message
  exit gracefully.
}

Consumption Order ​

SNS does not guarantee an order in which messages arrive. This means that an update could be consumed before a create, or multiple updates could be consumed in the wrong order.

If we don’t have the correct logic in place, we could end up with out if sync and out of date data.

To get around this we should check timestamps of the message’s payload against the data we have. Similar to the update example above. This way, when processing the messages from the queue. We should eventually end up with up to date, consistent data.

Dead Letter Queues ​

When an error occurs in consumption of a message, we want to re-try processing the message a few times, and then send it to the Dead Letter Queue (DLQ). We should log what error has happened to help investigate the issue.

Sending the message to the DLQ allows us to re-play the message at a later point when the issue is fixed. This will ensure even if there is an error, data is kept up to date and information asynchronous processes are not lost. - Bear in mind - that the DLQ retention period is limited, meaning once that period is over, the messages are purged from the queue.

To send a message to the DLQ the Lambda function should throw an error. For example:

throw new Error('Something went wrong')

Sometimes, there may be an error processing a message, e.g. an item already exists that a message is trying to create. - in this case, we want to gracefully handle this error, and process the message without throwing an error - because in-fact we don’t want to re-create the item, and we have handled the message by not creating something that already exists.

Validation ​

Just like API requests, we should validate the payload of the message is what we expect it to be, before processing the message.

For example, if a message’s payload is missing a key field or an ID of the data model - our queue handler will error eventually.

Bulk Heading ​

In general, the goal of the bulkhead pattern is to avoid faults in one part of a system to take the entire system down. The term comes from ships where a ship is divided in separate watertight compartments to avoid a single hull breach to flood the entire ship; it will only flood one bulkhead.

Bulk Heading also helps reduce synchronous dependencies on other services by storing a copy of the dependant services data within the service that needs it.

For example:

A needs data from B that needs data from C.

A stores a copy of data from B as a bulkhead A stores a copy of data from C as a bulkhead

Thus - requests to A will work if B or C are down, and are more performant.

There are some rules of bulkheading data:

  • Keep the bulkheaded data up to date and in-sync.

    • Consume messages that indicate the bulkheaded data should be updated, e.g. created, updated or deleted messages - these should be published by the service you’re dependant on.

    • Check timestamps to ensure you are consuming and updating to the latest version of that data

  • Only bulkhead the data that you need.

    • Ensure you store the updatedAt timestamps as well, as you will need to make use of these when checking that the data is the latest data.
  • Do not directly modify the bulkheaded data

    • Instead - modify the data at the source, and let the bulkheaded data be updated - via messages, asynchronously.

Made with ❤️ by the Luna team