Bug: first Set() overwrites later Set "updates" due to buffer propagation delay

So I’m adding many keys pretty fast into the cache, every key has the following process:

  1. Set(key, original_value) - first set for the key. As I get it, the value gets into set buffer and not added immediately into the cache, waiting for the buffer to fill up. Though it immediately adds the value into my DB storage.

  2. After some time (less than buffer propagation), I call

key := Get(k) // cache miss happens here and the value is read from storage
  ... updating value...  
Set(k, update_value) // also updates the value in storage

At this stage, as the key is not yet in hashmaps in the cache, this set operation is not treated as immediate update and just added to the buffer as new value I guess?

  1. Get(key) - at this point it all depends on timings. If the first original_value made its way into the cache already, the function returns stale original_value (cause updated_value is still in the buffer?). If the first Set did not succeed yet, the correct updated_value is returned from the storage, as no key in cache yet.

Shouldn’t we somehow check on 2nd Set() that we already have pending value in buffers and replace it with new value?