TLDR: Go with Redis!
REDIS VS MEMCACHE VS APCU
Redis and memcache are more or less interchangeable. Redis is considered the newer/faster option but real world performance hasn’t always reflected that sentiment. I naturally would try Redis first and see if it’s super fast, no weird issues. If Redis isn’t totally smooth, go straight to memcache. (Or for a totally hassle-free option, just go memcache first.) And then if you want to venture further, go with APCu. APCu is faster than both Redis/memcache but can be unstable or total memory-hog.
Existing Symfony Bundles:
- snc/redis-bundle: A Redis bundle for Symfony supporting Predis and PhpRedis
- Caching in Microservices – A Highly Effective way to Maximize Performance
Below are the 4 things to consider when deciding to use the cache.
- What to cache.
- When to cache.
- Where to cache.
- How long to cache.
- Why Redis beats Memcached for caching
Memcached or Redis? These renowned cache engines share a number of similarities, but they also have important differences. Redis, the newer and more versatile of the two, is almost always the superior choice.
- Memcached vs Redis, Which One to Pick?
The performance and memory usage of Redis compared to memcached is relatively similar. Unless you already have a large investment in memcached, going forward redis is the obvious solution.Not only Redis is better option, it enables whole new types of use cases and usage patterns.
APC (Alternative PHP Cache) is opcode cache and data store and it is discontinued in favour of APCu (APC User Cache: “userland” data caching) which is only data store.
Since Zend Opcache (is only opcode caching) was included in core PHP >5.5 we can set it up altogether with Redis.. APCu is faster than both Redis/memcache but can be .
We have to differentiate two things:
- The OPCode cache (formerly APC, xcache, etc…) : only serves the purpose of storing compiled version of PHP code into memory, and make it run faster – this has nothing to do with the later
- The applicative cache : purely business stuff that Symfony will store into a raw storage in order to avoid to do some business computations too often and be faster
OPCode cache and applicative cache are really very different things, you should in order to understand ignore the word “cache” in the “OPCode cache” term, and replace it with “compiled PHP binary” which makes much more sense.
Considering the APCu (formerly APC user cache) user cache, it’s a storage for applicative cache, you can tell Symfony to use it the same way as Redis. Main problem with APCu is that it will store cache in the PHP shared memory, which means that you need to have *a lot* of dedicated RAM to the PHP processes to storeSymfony data into it. Plus, if you multiple PHP frontends, each frontend will carry its own shared memory, data in those frontends may differs: this means that you can have a cache desync between PHP frontends; That’s why we often prefer to use a Memcache, or Redis or Mongodb.
Redis use case:
Redis is a NoSQL key-value data store (get info over here).
Pub/Sub: You can keep track of subscribers and what information they are interested in. You can use SUBSCRIBE, UNSUBSCRIBE, and PUBLISH.
Queues: You can use normal queue data structure, Redis has blocking queue commands as well.
Real time analysis of events(stats, anti spam etc) – Using Redis primitives it’s really simple to implement a real-time tracking filtering system or a spam filtering system.
Order by user votes and time: Leaderboard type functionality(Quora/Reddit/Digg), where the score is changes over time.
Transactional support: Provides transactional support. See Transactions – Redis.
Caching: Can be used as a general purpose cache. keys can strings, or more complex types (lists, sets, hashes etc). Also allows LRU eviction of items.
As a conclusion:
The microservice should include a straight forward setup for Redis: by default it contains database caching. If the dev wants to add more “areas” to cache then it is just a matter of configuring the bundle for:
- Monolog logging
- SwiftMailer spooling
- Profiler storage
That’s about data caching. For opcode caching we enable Opcache by default and set it up as explained by Symfony docs.