openresty / openresty/lua-resty-redis
Lost number precision when saving to redis
Nobody has claimed this yet.
- Dominant language
- Lua
- Stars
- 2k
- Forks
- 447
- Avg merge
- 2h 24m
- Merged PRs (30d)
- 3
Description
Hi!
I've found a vary bad thing... as i have investigated, lua-resty-redis loose 64bit number precision, when saving numbers to redis.
See examples below. Lets assume, we have a number 1824192940134586
REDIS CLI CODE
127.0.0.1:6379[5]> set BIG_NUM_AS_NUM_REDIS 1824192940134586
OK
127.0.0.1:6379[5]> get BIG_NUM_AS_NUM_REDIS
"1824192940134586"
Everything works as expected
Now lets work with number through lua-resty-redis
NGINX LUA CODE
-- first, lets save a number in redis
redis:set("BIG_NUM_AS_NUM", 1824192940134586)
redis:set("BIG_NUM_AS_STR", string.format("%16.0f",1824192940134586))
-- now lets get keys
ngx.log(ngx.ERR, "BIG_NUM_AS_NUM: ", redis:get("BIG_NUM_AS_NUM"))
-- prints: 1.8241929401346e+15
ngx.log(ngx.ERR, "BIG_NUM_AS_NUM(tostr): ", string.format("%16.0f",redis:get("BIG_NUM_AS_NUM")))
-- prints: 1824192940134600 - FAIL
ngx.log(ngx.ERR, "BIG_NUM_AS_STR: ", redis:get("BIG_NUM_AS_STR"))
-- prints: 1824192940134586 - OK
ngx.log(ngx.ERR, "BIG_NUM_AS_NUM_REDIS: ", redis:get("BIG_NUM_AS_NUM_REDIS"))
-- prints: 1824192940134586 - OK
ngx.log(ngx.ERR, "BIG_NUM_AS_NUM_REDIS(tostr): ", string.format("%16.0f",redis:get("BIG_NUM_AS_NUM_REDIS")))
-- prints: 1824192940134586 - OK
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the provided Nginx Lua reproduction, especially the redis:set and redis:get calls, and trace how numeric arguments and returned values are represented. Compare the numeric and string examples to determine where precision is lost; done means the reported 1824192940134586 value survives the client round trip without changing.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- lua, nginx, redis
- Domain
- backend, database
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100