Technology · Performance

How to cache a slow read that can be slightly old

A cache is a copy. Use it when a slightly old answer is acceptable, and say how old.

By OdooMatrix Published 2 min read

Part of a complete guide

This task is one section of Eight software practices that keep business apps reliable, with 7 related tasks on one page.

1. Pick a read that can be old

Choose something expensive and safe to delay, such as a public article or a translated label. Do not start with a stock quantity or a payable balance.

2. Set an expiry

Store the answer with a time limit you can explain, such as one minute or one hour. An entry with no expiry is a bug.

3. Prove it expires

Read once, change the source, and confirm the old answer disappears after the expiry. If it never changes, the expiry is not applied.

4. Leave money and stock alone

Read price, tax, and on-hand quantity from the current record when the business will act on them. A cached price that undercharges is not a performance win.

5. Frequently asked questions

Will a cache fix a slow query by itself?

It hides the query until the expiry. Fix the query if the miss is still too slow.

Can two servers have different copies?

Yes, until each one expires. Do not use that for a value that must be the same everywhere right now.

Should I cache logged-in pages?

Only when the page does not contain that person’s private data. Private pages are easy to leak through a shared cache.

6. Related articles and next steps

How to judge it

Treat the checks below as the acceptance test for the change. Names of libraries can vary; the behavior to prove does not.

Continue learning

Need this built?

See software development or email OdooMatrix.

← Back to all articles