Back to the blog

Redis for OpenCart: when it helps and how to set it up safely

· 7 min read

Redis is a fast in-memory data store. In an online store it is usually used as a cache: data that the store needs again and again (settings, category lists, query results) is kept in memory instead of being rebuilt or read from files on disk. Done right, it takes work off the server on every page. Done wrong, it adds one more thing to break and to secure, and it hides a problem instead of fixing it.

What Redis can speed up

  • The store’s cache: results the store would otherwise rebuild or read from cache files on disk. Depending on your OpenCart version and extensions, the cache can be switched from files to Redis; check the documentation for your version or ask a developer.
  • PHP sessions, when many visitors are on the site at the same time (this needs care, see the setup rules below).
  • Anything your extensions cache through the same cache layer.

What it does not fix

  • Slow database queries themselves. A slow query still runs every time the data is not in the cache.
  • Heavy images, too many scripts and a slow theme. That is front-end work.
  • A weak server or too little memory.
  • Bots hammering search and filters. That needs rules at Cloudflare or on the server, not a bigger cache.

Five checks before installing

  1. Measure first. Record the server response time of a few key pages (home, a category, a product) and the slowest database queries. Without a baseline you cannot tell whether Redis helped.
  2. Find where the slowness is. If every page is slow even when it is served from cache, look at the theme, the PHP version and the database before Redis.
  3. Check free memory. Redis keeps its data in RAM, so see how much memory the server has left for it.
  4. Check the PHP extension. The PHP version your store uses must have the redis extension.
  5. Prepare a way back. Write down how to switch the cache back to files in minutes.

Safe setup rules

  • Listen only on localhost (127.0.0.1) or a private network. A Redis port open to the internet is one of the classic ways servers get compromised.
  • Set a password and keep it out of any public file.
  • Set a memory limit and an eviction policy (for a cache, a policy such as allkeys-lru), so Redis never takes the memory the database and PHP need.
  • Keep sessions and cache apart (separate databases or instances), so that clearing the cache does not log every customer out.
  • Give cached data a sensible lifetime, so prices and stock do not go stale.
  • Watch it: memory used, hit rate and evictions.

Read-only checks a developer can run on the server:

redis-cli ping
redis-cli config get bind
redis-cli config get maxmemory
redis-cli config get maxmemory-policy
redis-cli info memory
ss -ltnp | grep 6379
php -m | grep -i redis

You want to see PONG from the first command, a bind that is not open to all interfaces, a maxmemory that is not 0, an ss line that shows 127.0.0.1:6379 and not 0.0.0.0:6379, and redis in the list of PHP modules.

How to measure the effect

Use the same pages and the same time of day before and after. Compare the server response time of the home, category and product pages, the number of database queries and the slowest query, and look at several days rather than one test. If the difference is small, Redis was not the bottleneck; the database and the front end are the next places to look.

Common mistakes

  • The Redis port open to the internet.
  • No memory limit.
  • Stale prices or stock because cached data lived too long.
  • Clearing the cache logs customers out because sessions share the same database.
  • Using Redis to hide a slow query that should be fixed.
  • Skipping the measurement before the change.

When to ask for help

If the store is slow and you are not sure whether the cache, the database or the theme is to blame, I can measure it and tell you. My quick check or speed sprint starts with exactly these measurements and ends with a short written report.