javaspringmongodbcachingttl

How do i efficiently store an OAuth token (TTL) without using in-memory cache in Java?


I am using EHCache to store a dynamic TTL OAuth Token but I maintain multiple pods in Kubernetes and end up making multiple OAuth Token REST calls to get latest token and due to less traffic in Pods, the API performance(P90, P95) is getting bad.

I tried to save OAuth token to MongoDB, but due to static TTL index, I can't dynamically create TTL index and even can't use expiry seconds to calculate based on current time, as it may cause concurrent threads ending up reading/writing the token simultaneously.

Looking for a better design suggestions to handle dynamic oauth token management without using any In-Memory caches in Java environment. Looking for better solutions using programmatic approach with Java and MongoDB.


Solution

  • This can be worked out by setting Mongo TTL to "0" and set the expiry date to the TTL indexed Date field to make it expire at a specific time(at token expiry time). https://www.mongodb.com/docs/manual/tutorial/expire-data/#expire-documents-after-a-specified-number-of-seconds