Most devs don’t know how to use cache

Coding CEO
2 min readFeb 20, 2022

--

Call me what you want, but after more than 30 years of coding with a lot of different people, most of them use to cache the wrong way.

I will go through a real example to show you what people are doing wrong.

One company needs a real-time dashboard with auto-refresh of sold products group by brand.

That’s may sound easy, but we have some problems because the data is split into 2 databases and we can not do a big query to retrieve all.

There are several ways of solving this problem, not only using a cache, but I’ll explain the cache option.

  1. The first Query gets sold products, the query takes 8 seconds.
  2. For each product, we retrieve from another database brands. When having 10.000 products, it needs 1ms for each brand query, so is 10 seconds.
  3. We take all the info and build a jpg with a graph that users can send by email or share. It takes 2 seconds, because of the librery used.
  4. The whole process takes about 20seconds.

So, on dev tried to solve the problem by just adding cache to all queries. The time improved, from 20 to 10seconds. That’s and awesome improvement, but totally wrong.

Caching queries only work with slow queries, like initial 8s query, for normal queries, in most cases, calling Redis or Memcache, just gets a bit of improvement. Caching the Brand query doesn’t help too much.

Let’s go step by step on how to improve it.

Thread/Memory Cache

Caching info in Redis/Memcache is expensive, is better in a long process like this to keep the cache in memory. In PHP you can have just an array with already requested brands. When the request ends, this memory is released.

With this, we go from 10 seconds without cache, 7 with Redis, to 1 with memory cache. That’s a great improvement.

Now the process takes 3 seconds.

Caching more things

Generating the image takes 2 seconds, but we may be generating the same image, again and again, so we will cache it.

Now the process in most cases takes 1 second.

We are doing everything wrong

Now thing, what is we cache the whole request and we forget what is doing inside.

The process takes now just 0,3 seconds.

The rules for caching

  1. Try to cache upper in stack call, (that makes it more complex to invalidate cache, I know)

2. Understand how cache works, caching simple queries doesn’t improve too much.

3. This process can be done without caching in other different ways having the same results but will make the logic more complex, decide in every situation if you want easy code cached, or complex logic not cached.

--

--

Coding CEO
Coding CEO

Written by Coding CEO

I fix things. I was CEO twice, and I missed too much coding. Back to CTO again.

Responses (9)