Backend Development 5 min read

Understanding Redis ZSET Score Precision and Its Interaction with Java Double

This article examines the floating‑point nature of Redis ZSET scores, the precision limits of Java double values, how Redis stores and retrieves these scores, and practical guidelines for avoiding precision loss when using timestamps or large numeric scores in backend development.

360 Quality & Efficiency
360 Quality & Efficiency
360 Quality & Efficiency
Understanding Redis ZSET Score Precision and Its Interaction with Java Double

In server‑side development, Redis ZSET is frequently used because its elements are unique and each element carries a score, making it ideal for ordered queues and sorting based on the score value.

The score must be a floating‑point number; attempting to use non‑numeric strings results in the error "(error) value is not a valid float". Numeric types such as int, long, float, double are accepted, but floating‑point values have inherent precision limits that can affect business logic.

Java’s double type exhibits precision issues: two double variables with many identical digits may compare as equal after rounding to about 18 significant digits, and arithmetic operations often produce small errors (e.g., deviations of 0.0000…1). For commercial calculations, java.math.BigDecimal is recommended, yet the Jedis driver still uses double for ZSET operations.

When inserting a value like 3.14 into a ZSET via Jedis, redis-cli shows 3.1400000000000001 while Java reads it back as 3.14 . The discrepancy originates from Redis’s own floating‑point storage precision rather than the insertion process.

Experiments with timestamps (14‑digit long integers) used as scores show no precision loss after conversion to double, because loss only occurs when the integer exceeds 17 digits. Therefore, timestamps are safe as scores, but numbers longer than 17 digits should be avoided or pre‑processed if necessary.

In summary, developers should be aware of Redis’s floating‑point precision limits, prefer BigDecimal for critical monetary or scientific calculations, and avoid using overly large numeric scores in ZSETs to prevent subtle bugs.

JavaRedisPrecisionZsetBigDecimaldoublescore
360 Quality & Efficiency
Written by

360 Quality & Efficiency

360 Quality & Efficiency focuses on seamlessly integrating quality and efficiency in R&D, sharing 360’s internal best practices with industry peers to foster collaboration among Chinese enterprises and drive greater efficiency value.

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.