Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing a token cache

I am trying to implement a simple cache where my cache will consist of just a latest token returned by authentication server to my application. There are different worker threads which try to login simultaneously to authentication server.This increases load on authentication server and also my application becomes slow as for each authentication thread there is a round trip involved with the server. Hence by implementing a simple token cache, a token will be cached at client side and will be updated only when one of the authentication thread fails to login. Whoever fails will go and fetch a new token from authentication server.

The problem I am getting is when any authentication thread fails and updates the token cache, there might be some threads who already have read the old token and will fail eventually and they also will try to update the cache. How can I stop these threads from updating the cache once it is already updated?

like image 780
genonymous Avatar asked Sep 27 '26 09:09

genonymous


2 Answers

One possible solution is to assign a timestamp to the retrieved token. Time timestamp does not have to be "real", a sequence number suffices.

When a thread retrieves the token from the token cache, it also retrieves the sequence number associated with the token. When a thread fails to login, it first has to check the local cache again to see if the cache has been refreshed since it retrieved its token. If the sequence number of the current token is different from the one it currently has, it can try again with the token from the cache.

Updating the token cache is necessary only when login fails and the cache has not been refreshed either since the retrieval of the failing token.

There is a slight chance that the sequence number goes a full cycle wrapping around, and a task is mislead to think that the cache has not been refreshed when in fact it has a great number of times, but this is more of a theoretical possibility than a practical concern.

like image 78
xxa Avatar answered Sep 29 '26 06:09

xxa


When a worker thread fails to log in, it should check the token cache to see if the token has changed from the one it was already issued. If the cached token has changed, it should use the new cached token instead of seeking a new token.

When a worker thread does seek a new token, it can also keep the old, no longer valid, token. Then before putting its new token in the cache, it can check if the currently cached token is the same as its old token.

The token cache of course needs to be thread safe and ensure that updates from one thread are seen immediately in other threads, for example by using a volatile reference to the cached token. This is simpler than keeping a separate time stamp with each token.

like image 35
Warren Dew Avatar answered Sep 29 '26 06:09

Warren Dew



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!