We have a Rest API which currently fetches a file from server as response. This file is stored in filesystem of server and is kind of static because this file gets updated only once in a month. This API is having high traffic. As improvement, we decided to move this file to server side in-memory cache (file size ~150Mb). There will be a timer event which runs at every specified time interval and check the source file for updates and if present, should update the cache. Now the problem is how to hot swap the file when there is an update. How the logic should be written in service to always fetch the latest version in memory?
Update:
The current design :- There is single file in cache named foo.dat. When ever an update happens a new file will be downloaded to same location with another name, once the download completes replace the existing foo.dat with new file and rename as foo.dat.
Current Issue: Since there are lot of traffic and the file is constantly accessed, the replace won’t happen and whenever there is new update, old version is still getting served instead of new.
To solve this we thought of implementing version of file is filesystem. So the service will always fetch the file with latest version (foo_<version#>.dat). The timer will download the latest file and give updated name so that the service will get the update file in next call.
When we try to move away from filesystem and try to keep the file is memory, the issue is how to implement this versioning since we can say the service to fetch resource using key from memory cache
I don’t get the question. Isn’t it just cache.Set(“your key”, newValue, policy)? Is there any issue I’m not aware of? I guess the update just swaps a reference, I don’t see much that can be more efficient than this.
I have updated the question with more details. Since there is high traffic, the resource is being constantly held and when there is an update to file how can I change the value of cache key without having a downtime? Since the file size ~150 Mb won’t it take some time to get swapped?
How the logic should be written in service to always fetch the latest version in memory?
I think we need to know more about how you’re storing it in memory in the first place.
I have updated the question with current design and issue we face. Currently we are not storing in memory, but in filesystem. The plan is to store in memory cache using cache.set("key",file, noExpiration)
. Since the file is constantly accessed, how to swap the resource when there is an update.