Why is my simple dict cache leaking data between api calls?
#1
I’m trying to implement a simple cache in my Python API to avoid hitting an external service too often. I went with a basic dictionary at first, but I’m running into issues where the data seems to persist between requests in a way I didn’t expect, almost like it’s becoming a shared global state. I’m not sure if my approach is fundamentally flawed or if I’m just missing something about how the application scope works.
Reply
#2
I had a module level dict too. It lingered across requests as long as the process stayed alive, which surprised me when I restarted the server.
Reply
#3
We added a tiny TTL on the stored items and logged hits versus misses, and we saw the data only cleared when the process died. So it’s per process, not shared across all workers.
Reply
#4
Are you sure the problem isn't that you're hitting the same endpoint with the same params every time and the result ends up being reused?
Reply
#5
Maybe the real issue is that you run multiple worker processes and each has its own memory. A distributed store would be needed if you want true sharing.
Reply


[-]
Quick Reply
Message
Type your reply to this message here.

Image Verification
Please enter the text contained within the image into the text box below it. This process is used to prevent automated spam bots.
Image Verification
(case insensitive)

Forum Jump: