Rather simplistic but how about just using upserts keyed against the message id?
(You can then also catch the returned value to check if an insert was performed)