https://kotlinlang.org logo
#exposed
Title
# exposed
m

maxmello

09/20/2023, 8:48 AM
Is the EntityCache in Exposed safe to be used in a microservice context where I have multiple instances of the same service, or it is possible that due to the caching, an update executed via instance A won’t be returned to a reading client connected via instance B?
y

Yang Wang

09/21/2023, 3:21 PM
Exposed's EntityCache is scoped to a transaction. Multiple instances of a microservice can't share a single transaction, so are unaffected by one another's EntityCache.
🙏 1
m

maxmello

09/21/2023, 3:29 PM
Ok great wanted to be sure. Other db libraries also have higher level/application wide caching and I wanted to make sure this isn't the same thing somehow.
y

Yang Wang

09/21/2023, 3:34 PM
due to the caching, an update executed via instance A won’t be returned to a reading client connected via instance B?
On that note, if instance A has a entity in the Entitycache, and that entity is subsequently updated in the DB (by instance B, or something else), then when you try to fetch that entity in the same transaction in instance A, it would be a cache-hit and return you the stale entity in the cache.
FYI, I ran into such a bug not too long ago: https://github.com/JetBrains/Exposed/issues/1527
m

maxmello

09/22/2023, 9:30 AM
I guess one could totally disable the EntityCache? For me, I would assume it is rarely actually used anyway the way my transactions are scoped. Thank you for the FYI
y

Yang Wang

09/25/2023, 4:44 PM
I think you could "disable" it by setting the cache size to be 0 via
maxEntitiesToStoreInCachePerEntity
. The usefulness of the entityCache comes when your entities have references to other entities via a foreign key. Ie. If the referenced entity is already in the cache, then you don't need to fire another query to fetch it. If your entity has no relationships/references, then the entityCache is probably not very useful, imo.
👍 1
3 Views