Deviling Master
Deviling Master

Reputation: 3113

Objectify: how "session cache" works across instances

From the caching documentation: https://github.com/objectify/objectify/wiki/Caching

The session cache is local to the Objectify instance. If you start a new session (via ObjectifyFactory.begin()), it will have a separate cache. If you use the thread-local ObjectifyService.ofy() method, the session cache will "just work" appropriately.

and

A get-by-key operation (single or batch) for a cached entity will return the entity instance without a call to the datastore or even to the memcache

My question is:

Request 1 is served by Instance A: an Object is updated and persisted. The session cache will be updated due to the object has been modified.

Request 2 is served by Instance B (which have already the Object in session cache due to a previous request): the session cache will be differente because it is another instance. How can the request get the updated entity instead of the previous one?

The App Engine memcache is shared across instances, but the session cache is single per instance (or even Objectify instance).

Is the session-cache synchronized among all the instances in some way?

Is it possible that differente requests served by different instances can have different versions of the same object?

Upvotes: 2

Views: 379

Answers (1)

Nick
Nick

Reputation: 1822

An objectify session is short lived and defines its own scope. It prevents multiple datastore gets for the same entity in disparate parts of your code by using memory, on the assumption that extra calls are typically wasteful.

If you require read/write consistency then you must use an idempotent transaction (which won't share the session cache and also does dirty checking and automatically retries).

By reading, mutating, then writing within the transaction you always avoid the issue of what could be happening in other requests (on other instances or otherwise)

Objectify sessions are not by default shared, and definitely shouldn't be. They are not synchronized across requests or instances. You can use memcache by putting @Cache on an entity to enable the write through cache, but this is distinct from the ofy session.

Upvotes: 3

Related Questions