What is the best design pattern to prevent duplication of materials?

I have a design template that I struggled with how best to prevent duplication of data publishing.

Here are the steps:

  • The client sends data using a unique guid (generated by the client guid - guaranteed unique)
  • Server-side software ensures that the client pointer does not yet exist in the database
  • starts a transaction
  • process data (may take 1 to 20 seconds depending on the payload)
  • commits a transaction

Here are the scenarios: A client sends data using guid "1", and then retransmits data using "1" before step (5) gets to the initial data transfer, then the transaction is processed twice.

What is the best design pattern to prevent this without using semaphores or blocking? The user should be able to resend the message if for some reason the first message did not work (server hardware problem, etc.).

Thank!

+3
source share
5 answers

Store the GUID in a column with a SQL constraint UNIQUE.

When you try (inside a transaction) to insert a second duplicate GUID, the operation will fail, after which you will roll back the entire transaction.

0
source

You can implement step 2 with a query that reads uncommitted data. For example, if you use MS SQL Server, you can do this:

IF NOT EXIST(SELECT * FROM SomeTable (NOLOCK) WHERE Guid = @ClienGUID)
BEGIN
   -- Insert the GUID ASAP in the transaction so that the next query will read it
   -- Do steps 3-5
END

- (NOLOCK),

+1

, , - ajax , , .

0

-, , - , ?

0

running_transactions, GUID , i.e.

  • GUID run_transactions.
  • , GUID , , - .
  • DB GUID. , " ".
  • 2 3, . , GUID , comit, 2. 5. GUID run_transactions.

. , ( 1Mrecords), . GUID.

0

Source: https://habr.com/ru/post/1769932/


All Articles