Most Efficient way to detect duplicate http requests

Viewed 5669

I'm using service stack to accept http requests that add rows to a back-end database, fairly standard stuff. The problem I have now is that sometimes, the devices sending data to the service, send the same request milliseconds apart. This then leads to database constraint errors as the first request was still inserting the new rows, etc etc

So, I'm thinking I need to find a way to detect a duplicate request and either ignore it or throw an HttpError back to the client. The idea I have at the moment is to store the full raw POST data to a temp table, and delete it once the processing is complete. On each POST request, I'd lookup the data in there and ignore if it's a duplicate. Is there any easier way to detect duplicate http requests in ServiceStack?

1 Answers

A common way to prevent duplicate requests is for clients to send a unique code (aka noonce) like a Guid with the request and have the Server reject the request e.g. with throw HttpError.Conflict("Duplicate Request") if a request with the same code is sent.

If you have multiple app servers I'd recommend storing the unique codes in a Redis Set, otherwise for a single app server storing it in a static ConcurrentDictionary<string,bool> or ConcurrentBag<string> will work.

Related