java - Interview task: Limit number of concurrent requests with tokens -
during interview, got question.
scenario: in database table there entries made common resources. lets column name "token". in total there 500 tokens. 1 web app using token table. whenever 1 user hits web app url, 1 token assigned user. once token assigned user, can not used other users. user, if token shows taken, system try assign token , on.
problem: on given point of time, lets 600 user hits web app, how make sure 500 users token, rest 100 not.
thank you.
what need check whether there free token when request starts, take token if there free one, , release when work done. since question database, think hints @ how achieve atomicity of check , take: use transactions.
for example, have table 500 rows tokens procedure select free token database and, if there one, update row mark taken. procedure run in transaction. if ends no free token available, serving thread wait , try again after short period of time. releasing token again trivial update of row.
another option starting empty table , taking token inserting new row. again, checking there <= 500 rows , inserting new 1 must run in transaction ensure atomicity.
Comments
Post a Comment