I have a list of 10 endpoints:
/v1/generate/entity1
/v1/generate/entity2
/v1/generate/entity3
and so on. My service is deployed in Kubernetes so I do not know in which pod a give request will end up.
I have Redis available and my problem is as follows:
Each endpoint will generate a totally different entity, but the starting point to generate such entity is a request to a third party service that is very expensive ( from a performance perspective ).
All endpoints will be called at the EXACT same time by different users and their responses will vary slightly.
So, my idea is that whenever one of the endpoints receives a request, it writes a key in Redis that says something like:
key: lock_userID_vendorID_traceId Value: X
So whenever an endpoint receives a request and sees that this key exists in Redis, it will wait for 300ms and then search for another key with the data from the expensive third party service. So, each time all the requests arrive to all of the endpoints, only one of them will actually retrieve the third party data and save it to cache. All of the other endpoints will actually retrieve the data from the cache after waiting for a fixed amount of time.
Is this the right approach? Is there some feature in Redis that can help me to accomplish this more easily.
I know this seems convoluted but I have no other way to go since I do not control the third party service and its performance is horrible.
Also, keep in mind that I can not cache the data I need preemptively, since it changes every few minutes and I have tons of users.