Dmytro Hrankin
Dmytro Hrankin

Reputation: 105

How exactly memcache removes expired data?

I am using com.google.appengine.api.memcache.MemcacheService to work with Google Memcache.

When storing a value, I set an expiration of 60 seconds. But the value is still returned from the cache even after several minutes.

The code for working with Memcache:

// Config
int expirationSeconds = 60;
Expiration expiration = Expiration.byDeltaSeconds(expirationSeconds);
MemcacheService memcache = MemcacheServiceFactory.getMemcacheService();

// Write operation
memcache.put(id, value, expiration);

// Read operation
memcache.get(id);

I am expecting the value to be absent in this case because the Memcache documentation says that An expired item is removed when someone unsuccessfully tries to retrieve it.

Why the expired value is still returned from Memcache?

Upvotes: 3

Views: 2937

Answers (1)

A.Queue
A.Queue

Reputation: 1572

The documentation uses two words evicted and removed that could be understood to be interchangeable but they aren't:

By default, all values remain in the cache as long as possible, until evicted due to memory pressure, removed explicitly by the app, or made unavailable for another reason (such as an outage).

And in the note here we can see how removal process works:

The actual removal of expired cache data is handled lazily. An expired item is removed when someone unsuccessfully tries to retrieve it.

At the same place the eviction is explained like that:

The value is evicted no later than this time, though it can be evicted earlier for other reasons. Increment the value stored for an existing key does not update its expiration time.

Eviction is something akin to soft removal where the value is unavailable but is still in the Memcache. Removal does the actual removal.

Upvotes: 2

Related Questions