Search code examples
apitokenload-balancinguser-managementstateless

User management across multiple stateless API applications


We want to make our API stateless.

Right now, the tokens for users are provided via 3rd party, upon login, and stored in the application memory.

As long as the token is in use, it is valid. Until it is idle for a configurable amount of time.

On 3rd party's side (the token provider) this token is valid for much longer (For example: A month on their side regardless of usage VS. 20 minutes of idle time on ours).

Meaning, each usage of this token updates the timestamp in the application memory.

As part of making our API stateless I've encountered a problem:

Assuming we will have more than one application and a load balancer, how do i maintain the user management between 2 applications?

I know how to restore the users profile/details if the token isn't in the application memory (but still valid on 3rd party side), but i can't know the timestamp of it's last usage.

I think that i either have to sync the cache between my applications, or manage the users on another service.

I'm hoping that my explanation is clear enough.

My questions are:

  1. What is the best practice for this issue?
  2. Where can i find useful information regarding user management across multiple applications? I think that i'm struggling with key words in this case.

Thanks in advance


Solution

  • From the architectural point of view separate user manager is preferable. In this case you will never turn to your 3rd party token provider directly but do it via your own manager that stores tokens and the timestamps. This however will probably require a serious refactoring.

    So, other solution that I can offer is probably using tool that provides sharing memory among processes and machines. For example you can use Hazelcast. It is very easy to start tool with very user-friendly API. If for example you store mapping from token to timestamp in map now the only thing you have to change is the place where you create map. Use the Hazelcast map factory instead of new HashMap<>() and your tokens will be magically distributed among your applications.