Duplicate Item Message Splitter

This pattern validates incoming messages containing multiple items to identify duplicate records. When duplicates are detected, the original message is automatically split into multiple messages containing only unique items. Throughout this process, the original item sequence and message order are preserved to maintain data integrity and processing consistency.

A common use case is importing data into the Dynamics 365 Finance & Operations Data Management Framework (DMF). If multiple rows contain the same value for a unique key, the import process will fail with an error similar to “The file contains duplicate rows and cannot insert data into staging due to a unique key violation.” By separating duplicate records into distinct messages, this pattern helps prevent import failures and ensures successful processing of valid records.

Another common use case is enforcing a maximum number of items per message when a downstream system cannot efficiently process large collections within a single message. In such scenarios, the message is split into smaller batches that comply with the target system’s processing limitations while preserving the original item order.

An example of this pattern is illustrated below. In this scenario, the Duplicate Splitter component detects two Item records with the same primary key value (ItemId = 1) within a single message. Upon detecting the duplicate, the component partitions the original message into two separate messages (represented by the two blue messages in the diagram), ensuring that both the original item sequence and overall message ordering are preserved. 

The resulting messages are published to a secondary queue dedicated to split-message processing. This allows the subscribing Function App to process each message independently, leveraging Azure Service Bus message completion and abandonment semantics to ensure reliable processing.

This processing model provides the following benefits: 

  • Ensures that each published message contains only unique items. 
  • Preserves the original sequence of items within the source collection. 
  • Maintains message ordering throughout the publishing process. 
  • Prevents downstream consumers from receiving messages containing duplicate primary keys. 
  • Supports reliable message processing through independent handling of each generated message. 

Design Overview:

The following diagram illustrates the Azure components involved in the solution and the flow of messages between them.

  1. The publishing Function App sends messages to an Azure Service Bus topic.
  2. The Message Splitter Function App subscribes to this topic, validates the incoming messages for duplicate records, and forwards the processed messages to a downstream Service Bus topic.
  3. When duplicate records are detected, the Message Splitter Function App generates multiple messages containing only unique records and publishes them to the destination topic.
  4. The consumer Function App subscribes to the destination topic and processes the messages using its standard processing logic, without requiring any changes to accommodate duplicate handling.

Splitter Processing Logic: 

The Splitter Function App receives a message containing a collection of items to be processed. As the collection is iterated, each item is added to an in-memory dictionary using the item’s primary key as the dictionary key. This approach enables efficient detection of duplicate records within the current message batch. 

When an item is encountered whose primary key already exists in the dictionary, a duplicate record has been identified. At this point, all items currently stored in the dictionary are published to Azure Service Bus as a single message, ensuring that the message contains only unique items. The dictionary is then cleared, and processing resumes with the duplicate item becoming the first entry in the next batch. 

This process continues until all items in the source collection have been evaluated. Once the end of the collection is reached, any remaining items held in the dictionary are published as the final message. 

Caveat:

While the splitter logic can be implemented within the Subscriber Function App, doing so introduces the risk of function timeouts if the subscribing system requires a significant amount of time to process each message.

Enjoy…

ARM Template for Provisioning an Azure Function App with a Key Vault

Late in 2018, Microsoft announced you can now store sensitive application setting values in an App Service to an Azure Key Vault without any changes to the function code. The only requirement was to update the value settings with @Microsoft.KeyVault(SecretUri=secret_uri_with_version)” to reference the Key Vault and enabling an identity account of the App Service to access the Key Vault.

This is a great leap forward having this feature baked into an App Service, however trying to create an ARM template to provision an App Service, Storage Account and a Key Vault by following these instructions https://docs.microsoft.com/en-us/azure/app-service/app-service-key-vault-references#azure-resource-manager-deployment proved to be rather challenging. After several hours of getting the versions correct and getting the dependencies in the correct order, I managed to create a working template to provision all 3 artefacts.

The template creates the App Service and uses the system-assigned managed identity account to access the Key Vault with Get only permissions. The primary key for the Storage Account and the Application Insights key are stored in Key Vault. These are then referenced by the AzureWebJobsStorage, WEBSITE_CONTENTAZUREFILECONNECTIONSTRING and APPINSIGHTS_INSTRUMENTATIONKEY names in the application settings of the Function App Service.

Updating the parameters file with the required artefact names and deploying the template, provisions the following services in Azure. Only thing left is to deploy your Function into the newly provisioned App Service.

image

If you try and view the Secrets in Key Vault, you will encounter an authorisation error shown below.  If you wish, you may update the ARM template to add your ID to the access policies collection of the Key Vault.

image

To add your account using the Azure Portal, navigate to Access Policies and then click Add new. Notice the App Svc account has already been added by the ARM template.

image

Then click on Select principal and type in you login address into the Principle blade to find your name and then click the select button.

image

Once your login name has been added, you can then select the required permissions.

image

Now you can view the keys added by the ARM template.

image

Below are the Application settings under the App Service showing references to the Key Vault for the values.

image

The code for the ARM Template can be downloaded from here: https://github.com/connectedcircuits/azappsvc. I have added comments into the template so you should be able to modify it to suit your requirements.

Enjoy…