Real-time deduplication strategy for relational DB
Good news. There is a SQL command usually called MERGE or UPSERT that basically does this. 

See this page for more information:
https://dev.mysql.com/doc/refman/5.7/en/insert-on-duplicate.html

You probably want something like:

INSERT INTO messasges (id,version,timestamp) VALUES (1123,1,1/1/2017)
  ON DUPLICATE KEY UPDATE version=2;