Backend Development 9 min read

Server‑Side Request Deduplication in Java Using Redis and MD5

The article explains how to prevent duplicate write requests on the server by using unique request IDs with Redis, computing MD5 digests of business parameters (excluding volatile fields like timestamps), and provides a reusable Java helper class with example code and test logs.

Java Architect Essentials
Java Architect Essentials
Java Architect Essentials
Server‑Side Request Deduplication in Java Using Redis and MD5

Duplicate user requests, especially write operations, can cause serious issues such as double ordering; the article first describes typical duplication scenarios like replay attacks, client‑side resubmissions, and gateway retries.

When a request carries a unique identifier, the simplest solution is to store that identifier in Redis with a short TTL; if the key already exists, the request is considered a duplicate. The following code shows the basic Redis SETNX pattern:

String KEY = "REQ12343456788"; // request unique ID
long expireTime = 1000; // 1000 ms expiration
long expireAt = System.currentTimeMillis() + expireTime;
String val = "expireAt@" + expireAt;
Boolean firstSet = stringRedisTemplate.execute((RedisCallback
) connection ->
    connection.set(KEY.getBytes(), val.getBytes(), Expiration.milliseconds(expireTime), RedisStringCommands.SetOption.SET_IF_ABSENT));
boolean isConsiderDup = (firstSet == null || !firstSet);

When a request does not contain a unique ID, the article proposes building one from business parameters. By concatenating user ID, method name, and a hash of the request payload, a deterministic key can be generated. The payload (usually JSON) is sorted, concatenated, and an MD5 digest is taken:

String KEY = "dedup:U=" + userId + "M=" + method + "P=" + reqParamMD5;

Because some fields (e.g., timestamps or GPS coordinates) change between identical logical requests, they should be excluded before hashing. An example JSON with a requestTime field demonstrates this:

// two requests differ only by requestTime
String req = "{\n" +
    "\"requestTime\" :\"20190101120001\",\n" +
    "\"requestValue\" :\"1000\",\n" +
    "\"requestKey\" :\"key\"\n}";
String req2 = "{\n" +
    "\"requestTime\" :\"20190101120002\",\n" +
    "\"requestValue\" :\"1000\",\n" +
    "\"requestKey\" :\"key\"\n}";

A reusable helper class ReqDedupHelper is provided to parse a JSON string into a TreeMap , remove excluded keys, serialize back to JSON, and compute the MD5 digest:

public class ReqDedupHelper {
    public String dedupParamMD5(final String reqJSON, String... excludeKeys) {
        TreeMap paramTreeMap = JSON.parseObject(reqJSON, TreeMap.class);
        if (excludeKeys != null) {
            List
excludeList = Arrays.asList(excludeKeys);
            for (String key : excludeList) {
                paramTreeMap.remove(key);
            }
        }
        String json = JSON.toJSONString(paramTreeMap);
        return jdkMD5(json);
    }
    private static String jdkMD5(String src) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            return DatatypeConverter.printHexBinary(md.digest(src.getBytes()));
        } catch (Exception e) { return null; }
    }
}

The article includes a main method that demonstrates the helper: without excluding requestTime the two requests produce different MD5 values; after exclusion they produce identical values, confirming the deduplication logic.

Finally, the complete solution combines the parameter‑MD5 key with Redis SETNX+expiration to achieve atomic deduplication within a configurable time window, as shown in the concluding code snippet.

javaRedisMD5Request Deduplication
Java Architect Essentials
Written by

Java Architect Essentials

Committed to sharing quality articles and tutorials to help Java programmers progress from junior to mid-level to senior architect. We curate high-quality learning resources, interview questions, videos, and projects from across the internet to help you systematically improve your Java architecture skills. Follow and reply '1024' to get Java programming resources. Learn together, grow together.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.