Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why distributed cache is faster than database?

Assume data is stored the same in database and in distributed cache (.i.e. no join needed), is it still relevant that distributed cache much faster than accessing database directly? As far as I understand:

  • Latency for application to access distributed cache = network time + cache access (in memory) time + deserialize (from binary to object) time
  • Latency for application to access database = network time + database query time (file access or database cache) + hydration time (read database result into object)

So the difference here is in cache access time and database query time (assuming deserialization is fast with powerful server). But compared to network time, I think the cache access time and database query time should be much smaller and therefore the time should be approximately the same?

I know I have a lot of assumptions here but it would be great if someone can help to explain whether I understand correctly (even greater if have statistics data)

like image 755
Phuong Nguyen Avatar asked Mar 18 '26 16:03

Phuong Nguyen


1 Answers

Yes, all things held equal, accessing data from an in memory cache will be faster than when using a DB.

Latency for application to access distributed cache = network time + cache access (in memory) time + deserialize (from binary to object) time

correct

Latency for application to access database = network time + database query time (file access or database cache) + hydration time (read database result into object)

correct

Most DBs will keep pages in their buffer cache, which is effectively like doing an in memory lookup but on a high throughput database many pages will need to be fetched from disk which is far slower than accessing main memory.

In a cache the data is always in memory. In a DB some pieces are in memory while some are on disk.

NVMEs are super fast but still nowhere near as fast as accessing data from RAM.

If the processor is the chef, chopping up and cooking food. Your RAM is the fridge. Your hard drive is the store down the street. It's there, & it's not too far, but nowhere near as fast as if you have it in the fridge.

Some latency numbers: https://gist.github.com/jboner/2841832

like image 126
Asad Awadia Avatar answered Mar 20 '26 11:03

Asad Awadia