Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to guard against repeated request?

we have a button in a web game for the users to collect reward. That should only be clicked once, and upon receiving the request, we'll mark it collected in DB.

we've already blocked the buttons in the client from repeated clicking. But that won't help if people resend the package multiple times to our server in short period of time.

what I want is a method to block this from server side.

we're using Playframework 2 (2.0.3-RC2) for server side and so far it's stateless, I'm tempted to use a Set to guard like this:

if processingSet has userId then BadRequest
else put userId in processingSet and handle request
     after that remove userId from that Set

but then I'd have to face problem like Updating Scala collections thread-safely and still fail to block the user once we have more than one server behind load balancing.

one possibility I'm thinking about is to have a table in DB in place of the processingSet above, but that would incur 1+ DB operation per request, are there any better solution~?

thanks~

like image 741
Chris Avatar asked Feb 04 '26 13:02

Chris


1 Answers

Additional DB operation is relatively 'cheap' solution in that case. You should use it if you'e planning to save the buttons state permanently.

If the button is disabled only for some period of time (for an example until the game is over) you can also consider using the cache API however keep in mind that's not dedicated for solutions which should be stored for long time (it should not be considered as DB alternative).

like image 148
biesior Avatar answered Feb 06 '26 05:02

biesior