Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question on memory handling in shared_dict #2306

Closed
IlyaIndigo opened this issue Apr 24, 2024 · 5 comments
Closed

Question on memory handling in shared_dict #2306

IlyaIndigo opened this issue Apr 24, 2024 · 5 comments

Comments

@IlyaIndigo
Copy link

https://github.com/openresty/lua-nginx-module?tab=readme-ov-file#ngxshareddictset
Reading the documentation I didn't fully understand some points.
1 What happens when a key expires?
When and how and after how long is the memory replaced by this value freed and is it freed at all?
2 When memory is full, does the set method try to delete expired keys or can it delete keys that haven't expired yet?

@zhuizhuhaomeng
Copy link
Contributor

1 What happens when a key expires?
When and how and after how long is the memory replaced by this value freed and is it freed at all?
Nothing happens when a key expires. The expired key is still there if it is not replaced by another new add/set operation.

2 When memory is full, does the set method try to delete expired keys or can it delete keys that haven't expired yet?
There are different APIs.
When memory is full, the dict:set() will delete keys that is not expired yet.
And the dict:safe_set() will not delete keys that is not exipred.

@IlyaIndigo
Copy link
Author

IlyaIndigo commented Apr 27, 2024

Thank you for the answer.
I understand correctly, if I want memory not to overflow I should take care of periodic calls of flush_expired() myself ?

http {
    ,,,,
    lua_shared_dict my 1m;
    init_by_lua_block {
        local function mem_free()
            ngx.shared.my:flush_expired()
        end
        require("resty.concurrent.scheduler").every("1h", mem_free)
    }
    ....
}

P.S. The “resty.concurrent.scheduler” module is a figment of ChatGPT's imagination.

@zhuizhuhaomeng
Copy link
Contributor

Nope.
What you need to do is:

  1. config a reasonable shdict size
  2. choose the right API like set or safe_set
  3. set a reasonable expired time

If the shdict still overflows, then tweak the above steps.

@IlyaIndigo
Copy link
Author

IlyaIndigo commented Apr 27, 2024

But if I set the test key to 3600 sec.
And no one will access (:get("test")) it after expiration, it will hang in memory forever until nginx is rebooted.
And so it is with every key.

Or did I misunderstand?

@zhuizhuhaomeng
Copy link
Contributor

Table entries in shared dict are passively eliminated and remain in memory as long as they are not overwritten.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants