openresty / openresty/lua-resty-redis

Lost number precision when saving to redis

Open
#135 5 comments 0 reactions 0 assignees View on GitHub

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

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.