Prevent Duplicate Submissions in a Web Project
Preventing duplicate submissions is a common function in a web project. In this post, I will cover the principle of the implementation of preventing duplicate submissions.
The process
- Create a Filter or Interceptor in the web project to check whether the request is a duplicate submission.
- Get the request information and determine whether it is a duplicate submission.
- If it’s a duplicate submission, return an error message to the client.
- If not, save the request information to cache (Redis) and set expiration time to 2-3 seconds.
Filter or Interceptor is a common component in a web framework of the web backend, which can check the request data before handling the request.
Details
How to save the request information to Redis
- Type:
string
- Key:
duplicate_submit:{user_id}:{request_uri}:{request_method}
- Value:
{request_query_string} + {request_body or filename}
- Expiration time: 2-3 seconds
How to determine whether the request is a duplicate submission
- Get the userId of the current login user.
- Get requestURI, requestMethod, requestQueryString, requestBody from the Request.
- Get previous request data: get the value of the key
duplicate_submit:{user_id}:{request_uri}:{request_method}
from Redis. - Compare current request data with previous request data.