I have a java project and I am trying to scale my project so want to spin up 3 instance of a single microservice.
But I have an issue
to explain
From UI when user logs in, every 10sec a api request(for the specific user) goes to backend which gives the status of spring batch job (either running or not running for a particular user who has logged in). This works fine with only 1 instance.
but when I have 3 instances (instance 1, 2 & 3) of the same application,
say first request at 10sec goes to instance 1 and job is running for the logged in user - it returns job is running. - correct second request at 20sec goes to instance 2, since no job is running in instance 2 (job is running in instance 1) it return no job is running - incorrect third request at 30sec goes to instance 3, since no job is running in instance 3 (job is running in instance 3) it return no job is running - incorrect
How do I make sure to get "job is running" as the status till the job in instance 1 gets over.
I am using spring microservices
please help in on this
Thanks in advance
Need to get job is running till job finishes in instance 1 for the particular user, for every api request
CodePudding user response:
I assume that you're using Spring Batch and its in memory job repository.
If you are wanting to scale, you should really use a separate database to keep the metadata of those jobs. As per this example.
Configure and deploy your database, add a dataSource, ensure that your jobRepository uses that dataSource. All servers will then return the same values.
If this cannot be achieved, you should at least ensure that your load balancer has sticky sessions enabled.