Reputation: 319
I have a Node service. The node service has a memory cache, it's an in-memory key-value store. The node service also running a periodic task runs every day which is rebuild cache (CPU intensive and cost a lot of time). When rebuild cache task is running, Will it block other /get request? Is there any race condition here?
/get: get the cache data by key
setInterval(()=>{ rebuildCache() ; }, 3600)
async rebuildCache(filepath: string, key: string): Promise<void> {
const obj = constructFromFile(filepath)
//load json from filepath. do some cpu intensitive work. (build schema, reference, etc)
cache[key] = obj
}
Upvotes: 0
Views: 970
Reputation: 707436
Will it block other /get request?
It depends upon what rebuildCache()
does and how it works. If it's a synchronous operation (entirely CPU), then that will block the event loop and will block all request processing.
Is there any race condition here?
It depends upon how the code that uses the cache is written and what rebuildCache()
does. If any operations that uses the cache are asynchronous and they depend upon cache consistency from before it runs an asynchronous operation to after it finishes an asynchronous operation, then a rebuildCache()
can occur in that window of time and you could indeed have a race condition.
The devil is in the details of your actual implementation for both code using the cache and the function that rebuilds the cache so we can only offer hypothetical answers without seeing the actual code.
FYI, this code in your question:
setInterval(rebuildCache())
only calls rebuildCache()
once. To actually call it on any interval, you would need something like this:
setInterval(rebuildCache, t);
Note, you pass a function reference (without the parens) and you pass a time for the timer interval.
Upvotes: 1