openresty / openresty/lua-resty-redis

read the same redis key, sometimes not same

Open
#130 1 comment 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

when i use redis in lua, i got this problem
here is the file structure

filename : init.lua

redis = require "resty.redis"
ssl = require "ngx.ssl"
cjson = require 'cjson'

filename: nginx.conf

init_by_lua_file /opt/nginx/conf.d/lua/init.lua;

server {

    lua_code_cache on;
    location ~ ^/lua_api/([-_a-zA-Z0-9/]+) {
        default_type "application/json"; 
        set $path $1;
        content_by_lua_file /opt/nginx/conf.d/lua/api/$path.lua;
     }
}

filename :regist_limit.lua

local json = require 'cjson';
local vip_red = redis:new()
vip_red:set_timeout(30000) -- 1 sec

vip_redis_host = 'ip address'
vip_redis_port = 'port'

function api_error(code, msg)
    ngx.say(json.encode({errcode = code,errmsg = msg}))
    return
end

local ok, err = vip_red:connect(vip_redis_host, vip_redis_port)
if not ok then
    api_error(50000, "connet redis fail in step 1")
end

local res, err = vip_red:get("regist_limit")
if not res then
    api_error(50001, "connet redis fail in step 2")
end
if res == ngx.null then
  res = 0
else
  res = tonumber(res)
end
ngx.say(json.encode({limit = res}))

the redis set regist_limit key by the value 1
but sometimes when i request the path /lua_api/regist_limit path
i got different response

the first one
{ "limit": 1 }

the second one
{ "limit": 0 }

i can not figure out where comes wrong, the lua_code_cache?

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 init.lua, nginx.conf, and regist_limit.lua, then reproduce requests to /lua_api/regist_limit while checking the Redis value for regist_limit. Trace the connection and GET result through the reported code and compare the request paths and Redis instances. Done means identifying the source of the inconsistent value and documenting or fixing it so repeated requests return the expected result.

Written by the indexing model from the issue text.

Assessment

Tech stack
lua, nginx, redis
Domain
backend, databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.