我应该先发制人地缓存数据

时间:2021-12-01 04:06:34

I have an application that receives messages from devices every few minutes. I also have clients that request the last 10 messages for a particular device.

我有一个应用程序,每隔几分钟就会从设备接收消息。我也有客户端请求特定设备的最后10条消息。

I am suffering with some database saturation and I wish to cache this list by device. The basic premise is that when a message is received from the device then the processor that receives the messages will invalidate the cache for that device.

我正在遭受一些数据库饱和,我希望按设备缓存此列表。基本前提是当从设备接收到消息时,接收消息的处理器将使该设备的高速缓存无效。

My question is whether I should just invalidate the cache and then have it rebuilt when the next client connects, or should I have the device processor rebuild the cache pre-emptively. The device processor can retrieve the current cache pop the last entry off, add the new entry and cache the new result.

我的问题是,我是否应该使缓存无效,然后在下一个客户端连接时重建它,或者我应该让设备处理器先发制人地重建缓存。设备处理器可以检索当前缓存弹出最后一个条目,添加新条目并缓存新结果。

I appreciate that this may be an it depends, answer but I would appreciate hearing peoples own experiences in this area.

我很欣赏这可能取决于它,但我很感激听到人们在这方面的经验。

3 个解决方案

#1


I think you're describing a "pre-fetch" mechanism, just to help you put a name on it. :)

我想你正在描述一个“预取”机制,只是为了帮助你在上面写一个名字。 :)

I don't have a great deal of experience in this particular area, but if you believe you can pre-fetch data and reliably predict that it's what the client wants, and you obtain a measurable and desirable performance improvement because of it, then go ahead and give it a whirl.

我在这个特定的领域没有很多经验,但是如果你相信你可以预先获取数据并可靠地预测它是客户想要的,并且你因此获得了可衡量和理想的性能提升,那就去吧前进并给它一个旋转。

Just remember to keep all the hairiness of caching in mind. How is it invalidated when the underlying data changes, etc. Good luck!

请记住要记住缓存的所有毛羽。当底层数据发生变化时,它是如何失效的。祝你好运!

#2


I don't think you can answer this question until you've quantified how many times a client on average retrieves messages for a given device. If a given device is only queried for messages once in a blue moon, then it's fine to purge the message cache on each client request. However, if a given device's message queue is queried many times, then a preemptive caching on device sync is likely the best option; given the device syncs are less frequent then the client requests.

在您量化客户端平均检索给定设备的消息的次数之前,我认为您无法回答此问题。如果在蓝色月亮中仅查询给定设备的消息,则可以在每个客户端请求上清除消息缓存。但是,如果多次查询给定设备的消息队列,则设备同步上的抢占式高速缓存可能是最佳选择;鉴于设备同步的频率低于客户端请求。

Your best might be writing a system that adaptively caches based on load. If a given device's message queue is queried often, it refreshes the cache on device sync. If a device message queue is rarely queried, you refresh the cache on client request.

您最好的可能是编写一个基于负载自适应缓存的系统。如果经常查询给定设备的消息队列,则会在设备同步时刷新缓存。如果很少查询设备消息队列,则在客户端请求上刷新缓存。

#3


I hear ya, but it really does depend. On many, many variables.

我听到了,但它确实依赖。关于许多变量。

If it were me, I would probably just invalidate the cache and let the next client rebuild it since that seems slightly simpler, but I don't see how you could possibly determine which way works better without trying them both.

如果是我,我可能只是使缓存无效并让下一个客户端重建它,因为这似乎稍微简单,但我不知道如何在不尝试它们的情况下确定哪种方式更好。

Hopefully, you can come up with a way to realistically simulate a heavy client load so you're not messing around with the live server.

希望您可以想出一种方法来逼真地模拟繁重的客户端负载,这样您就不会乱搞现场服务器。

#1


I think you're describing a "pre-fetch" mechanism, just to help you put a name on it. :)

我想你正在描述一个“预取”机制,只是为了帮助你在上面写一个名字。 :)

I don't have a great deal of experience in this particular area, but if you believe you can pre-fetch data and reliably predict that it's what the client wants, and you obtain a measurable and desirable performance improvement because of it, then go ahead and give it a whirl.

我在这个特定的领域没有很多经验,但是如果你相信你可以预先获取数据并可靠地预测它是客户想要的,并且你因此获得了可衡量和理想的性能提升,那就去吧前进并给它一个旋转。

Just remember to keep all the hairiness of caching in mind. How is it invalidated when the underlying data changes, etc. Good luck!

请记住要记住缓存的所有毛羽。当底层数据发生变化时,它是如何失效的。祝你好运!

#2


I don't think you can answer this question until you've quantified how many times a client on average retrieves messages for a given device. If a given device is only queried for messages once in a blue moon, then it's fine to purge the message cache on each client request. However, if a given device's message queue is queried many times, then a preemptive caching on device sync is likely the best option; given the device syncs are less frequent then the client requests.

在您量化客户端平均检索给定设备的消息的次数之前,我认为您无法回答此问题。如果在蓝色月亮中仅查询给定设备的消息,则可以在每个客户端请求上清除消息缓存。但是,如果多次查询给定设备的消息队列,则设备同步上的抢占式高速缓存可能是最佳选择;鉴于设备同步的频率低于客户端请求。

Your best might be writing a system that adaptively caches based on load. If a given device's message queue is queried often, it refreshes the cache on device sync. If a device message queue is rarely queried, you refresh the cache on client request.

您最好的可能是编写一个基于负载自适应缓存的系统。如果经常查询给定设备的消息队列,则会在设备同步时刷新缓存。如果很少查询设备消息队列,则在客户端请求上刷新缓存。

#3


I hear ya, but it really does depend. On many, many variables.

我听到了,但它确实依赖。关于许多变量。

If it were me, I would probably just invalidate the cache and let the next client rebuild it since that seems slightly simpler, but I don't see how you could possibly determine which way works better without trying them both.

如果是我,我可能只是使缓存无效并让下一个客户端重建它,因为这似乎稍微简单,但我不知道如何在不尝试它们的情况下确定哪种方式更好。

Hopefully, you can come up with a way to realistically simulate a heavy client load so you're not messing around with the live server.

希望您可以想出一种方法来逼真地模拟繁重的客户端负载,这样您就不会乱搞现场服务器。