Caching systems play a pretty important role in optimizing website performance, these work by storing frequently accessed data or resources in memory or disk, reducing the need to fetch them from the server or database repeatedly, this is of course on the server side, externally you can always cache with CDN (content distribution network) or other type of worker systems (these can do calculations), the combination of these both internal and external caching systems enhance website responsiveness, reduce server load, and improve scalability, contributing to a smoother user experience, and thats what we all want, right!
Im talking about this because I’ve been a big fan of Memcache/Memcached for years, but with opcache im kinda in the fence if i need to keep using both, because both do the same thing in different ways as well as different things all at the same time, in the sense that they overlap quite a bit and as we all know a caching system is useful to the point that it adds noticeable more performance for little extra resources, if it’s just adding a bit more performance, then its adding more complexity and less overall performance (since the caching system to work needs to use resources as well), so let’s check both of these caching systems, and see if you reach the same conclusion i did:
OPCache
-
- OPCache is a bytecode cache for PHP scripts. It caches compiled PHP code in memory, which helps in speeding up the execution of PHP scripts by avoiding the need for PHP to recompile the scripts on each request.
- OPCache improves PHP performance by reducing the time required for parsing and compiling PHP scripts.
- OPCache is primarily focused on optimizing the performance of PHP code execution.
Memcache
-
- Memcache, on the other hand, is a distributed memory caching system. It is used to store key-value pairs in memory across multiple servers.
- Memcache is often used to cache data that is frequently accessed or that doesn’t change frequently.
- Memcache is primarily focused on caching data from a database or expensive computation results, thus reducing the load on the database and improving overall application performance.
So now, regarding whether you should use Memcache if you’re already using OPCache, this is kinda the way i see it:
- You can still use both:
- If your application has a need for caching data that doesn’t change frequently, or if you want to reduce the load on your database by caching frequently accessed data, then using Memcache alongside OPCache can be beneficial.
- OPCache will optimize the execution of your PHP scripts, while Memcache can cache data to improve overall application performance.
- Maybe it’s not really required:
- However, if your application doesn’t have a significant need for caching data or if it’s a small-scale application where caching data in memory isn’t critical, you might not need to incorporate Memcache, also of course if you don’t use a database then Memcache has less uses.
- While OPCache alone can still provide performance benefits by caching and reusing compiled PHP scripts.
So in that sense seems like in a vacuum more often than not it’s best to use both, you use OPcache to cache php and then use Memcache to cache database data and computations, however that is in a vacuum, when you have other caching like nginx/litespeed/server side caching, local page caching, cdn’s like cloudflare, i kinda have a sense that both opcache and memcache give you just a small boost, so the more important point i guess it’s that it depends a lot on your site and application needs.
Let’s say if you have a normal well cached wordpress site (cache plugin and maybe cloudflare), thats updated daily im not sure you really need opcache and memcache, at all, but if i had to choose i would go with opcache since it seems the more light of the two and also not all requests are to the database (ie there are always way more php requests than database requests), but if i have a site or app that cant be cached often, then yes having both running seems like the best choice, even if i still caching locally or with a cdn, hope this made sense hahaha 🙂