PoolByte

Redis Object Caching: Keys, Expiry and Correctness

· PoolByte Technology

Redis Object Caching: Keys, Expiry and Correctness

Redis can be used to keep some data in memory that the application needs repeatedly. An object cache is not the same as a page cache, which presents the HTML page ready for the visitor. The goal here is to avoid redoing certain calculations or data reading tasks for the appropriate amount of time.

What data will you cache?

First choose an expensive and frequently read dataset. A product group's filter options or a publicly available list of services may be suitable candidates. Incorrect up-to-dateness of data such as authorization information, live stock decision and customer-specific balance produces much more serious consequences. Define in advance which event invalidates the cached data.

For example, let's say category filters are stored for ten minutes. If there is no mechanism to clear the relevant key when the editor changes the product group, the visitor will see the old options. Making the duration very short alleviates this problem, but is not always a substitute for correct cache invalidation. Check performance gains and data correctness together.

Key design is part of the application

Add the application, environment, and customer scope if necessary to the Redis keys. If the test environment and the live site use the same names, this may lead to incorrect data being shared. Redis keyspace document describes the basic behavior of keys and expiration.

Do not write the result of a client account to a shared cache key. Also take into account inputs that change the result, such as language, currency, or filter. The keys should not contain passwords, access keys or unnecessary personal information. This information may become visible when it is necessary to review the key list for support.

What happens when the memory becomes full?

Cache is not unlimited. The memory limit and eviction policy affect which keys are removed under pressure. Redis eviction description describes the different policies and metrics to observe. Putting each data type in the same Redis instance can increase the risk of losing permanent records during cache clearing.

Determine what the application does when Redis becomes unresponsive. Re-reading from the database is possible in appropriate cases; However, if all requests are directed there at the same time, this creates an additional burden. Plan the regeneration flow to avoid performing the same expensive calculation dozens of times simultaneously as a key expires.

Metrics that show success

  • Track hit and miss rate along with request volume.
  • Track memory usage and number of evicted keys.
  • Compare actual processing time with cache on and off.
  • Test when the product update is reflected to the visitor.
  • Measure errors and additional database load when Redis is turned off.

A high hit rate alone is not enough; Serving stale data very quickly can also produce a high hit rate. Verify that the Redis service, PHP extension and connection authorization are indeed offered in the hosting package. You can track which layer solves which problem by examining the differences between the page and CDN layers in the cache guide.