suggestion for possible optimization: you can store and sort messages within 2 seconds delay period. Messages with older values you can sort and write.