it will have race condition , for ex Thread T1 and T2 are executed concurrently ,T1 executed line 34 and moved to 24 and passed meanwhile T2 was waiting at line 34, it will again update the count to 10.
I think the logic of calculating additional token is wrong. Lets say the user request after 4 seconds, according to the logic 4*refresh rate token will be added in the bucket. Isn't it wrong? Will the token be carry forward if there is no request for some seconds? For that seconds 40 token will be added for refresh token 10 but, only 10 requests should be there for that particular second. Also in real life solution for API calls how can we implement it? Using cache?? We will need a persistent hashmap.
Don't we need to sychronize refresh bucket method? AtomicInteger does not prevent race condition, any other thread can change its value in middle of execution.
Creating Map for every user is not required right, we can create map as static.. Ideally user same userBucketCreater will be called only once for one user
Let's talk about a situation when one thread is updating the currentCapacity the other thread can read the last updatedTime so similarly vice versa. So a race condition will raise. I think we need to synchronise the refresh method;
Doesn't this code updates the lastUpdatedTime variable in refreshbucket() even if additionalToken evalutes to zero? Anyways, your LLD videos are really good. Great work!
what abt distributed instances of gateway and isolated service centers?
Do i need to use concurrent hashmap for multiple users?
Thanks for sharing. Easy to understand.
it will have race condition , for ex Thread T1 and T2 are executed concurrently ,T1 executed line 34 and moved to 24 and passed meanwhile T2 was waiting at line 34, it will again update the count to 10.
I think the logic of calculating additional token is wrong. Lets say the user request after 4 seconds, according to the logic 4*refresh rate token will be added in the bucket. Isn't it wrong? Will the token be carry forward if there is no request for some seconds? For that seconds 40 token will be added for refresh token 10 but, only 10 requests should be there for that particular second. Also in real life solution for API calls how can we implement it? Using cache?? We will need a persistent hashmap.
Don't we need to sychronize refresh bucket method?
AtomicInteger does not prevent race condition, any other thread can change its value in middle of execution.
Race condition is simulated in the example with 10 threads
Creating Map for every user is not required right, we can create map as static.. Ideally user same userBucketCreater will be called only once for one user
Let's talk about a situation when one thread is updating the currentCapacity the other thread can read the last updatedTime so similarly vice versa. So a race condition will raise. I think we need to synchronise the refresh method;
Doesn't this code updates the lastUpdatedTime variable in refreshbucket() even if additionalToken evalutes to zero?
Anyways, your LLD videos are really good. Great work!
It should be ONLY updated if additionalToken>0