Search code examples
mysqlspring-data-jpamicroservices

How can i handle duplicate records in MySQL database in microservice architecture?


Due to an duplicate events coming from an event-driven architecture, there are duplicate records being saved to the database.

This is causing an issue. I did add duplicate constraint to the table, but now I don't know how to handle the exception in case of saveAll.

One thought that comes to my mind is a recursive approach.. but since it is unpredictable as to how many duplicate events may occur, the no. of retries cannot be determined and obviously, I cannot use an infinite recursive loop.


Solution

  • Since your events are firstly saved to database, you can just make a unique index, then duplicate records cannot be inserted.

    For Mysql, "insert ignore ...." can just ignore duplicates.

    For Postgres "insert ... on conflict do nothing" can just ignore duplicates.