StackExchange / StackExchange/StackExchange.Redis
Using a WeakReference to remember LUA script cache looks like a cause of server script memory exhaustion?
Nobody has claimed this yet.
- Dominant language
- C#
- Stars
- 6.2k
- Forks
- 1.6k
- Avg merge
- 1d 15h
- Merged PRs (30d)
- 43
Description
This is a kind of half-finished psychic-debugging style investigation so take my theory with a grain of salt but...
I investigated a redis server crash where the LUA script space was exhausted, and found my way to the pages on LUA scripting with SE.redis (the library being used)
https://stackexchange.github.io/StackExchange.Redis/Scripting.html
and the source for LoadedLuaScript, which is stored with a weak reference.
In this scenario they keep using (reusing) a single LUA script, but they still eventually exhausted the server LUA scripting memory.
I hear (TBV) that they are calling ScriptEvaluate and it eventually becomes EVALSHA under the hood.
This would involve calling Prepare, which seems to use WeakReference for caching under the hood.
Unfortunately, I think the problem in practice with storing loaded lua scripts with a weak reference is that those weak references WILL get garbage collected and so the prepared cached script is forgotten by the client -- leading to eventually reloading the LUA script in the server unnecessarily, right?
A possible solution here might be using something 'smarter' or 'more robust' than WeakReference to solve this same problem. For instance, I don't know, MemoryCache?
Contributor guide
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 src/StackExchange.Redis/LuaScript.cs around line 91 and read the Scripting documentation linked in the issue. Verify whether Prepare's WeakReference can be collected while the same Lua script is reused, and whether that leads to repeated server-side loading. Done means confirming or disproving the suspected cache behavior and identifying a robust caching approach if the behavior is confirmed.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp, redis
- Domain
- backend, databases
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100