DEV Community

Rak
Rak

Posted on • Edited on

5 3 4 4 4

Process events once, (pub/sub) using Nitric in GO

The Idempotent Consumer pattern is essential for handling duplicate messages in microservices, ensuring that processing the same message repeatedly yields the same outcome as processing it once.

This can be achieved by tracking message IDs, either in a separate PROCESSED_MESSAGE table or within the business entities affected by the messages. By recording processed message IDs, a message handler can identify and discard duplicates, ensuring idempotent processing and data consistency across the microservices.

In this short tutorial we will example a simple snippet which uses a Subscribe method to process messages from a topic named "updates".

Our goal is to ensure that the event is processed only once by using an inbuilt function provided by the Nitric SDK to check for duplicates.

If you haven't used the Nitric SDK before, then start with this tutorial.

Pre-Requisites:

  1. Go installed on your machine.
  2. Nitric SDK for Go.
import (
  "fmt"
  "github.com/nitrictech/go-sdk/nitric"
  "github.com/nitrictech/go-sdk/nitric/faas"
)

func main() {
  updates := nitric.NewTopic("updates")

  updates.Subscribe(func(ctx *faas.EventContext, next faas.EventHandler) (*faas.EventContext, error) {
    if isDuplicate(ctx.Request) {
      return ctx, nil
    }
    // TODO: Not a duplicate, process the event
  })

  if err := nitric.Run(); err != nil {
    fmt.Println(err)
  }
}
Enter fullscreen mode Exit fullscreen mode

By invoking isDuplicate with the incoming request, it checks for duplicate messages, discarding them to maintain idempotency, while processing new or unique messages accordingly.

Learn more about idempotent subscribers and patterns here.

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay