redbean super cache

236 views
Skip to first unread message

redbeanie

unread,
Mar 20, 2013, 12:36:42 PM3/20/13
to redbe...@googlegroups.com
I would like to gather some feedback for a caching method to make RedbeanPHP extremely scalable in a clustered environment.  The assumptions are as follows:
1) Most operations are read operations (90%)
2) Write operations are non-transactional
3) Minor cache inconsistencies between nodes are acceptable for a short amount of time.
4) you use the R::load() method as much as possible (this is where the cache plugin pulls from the cache it seems.  I'm not sure if internally, redbean uses R::load to load a relationship bean?)

I'm proposing a hybrid approach using PHP APC and a simple UDP server that will be used for cache invalidation.

I have created a simple APC Cache plugin for Redbean by using the existing cache plugin as the template code.  The APC cache plugin seems to be working fine.  The main downside of using the APC cache is that it is local to the server.  So in a multi-node environment, each server is unaware of the other server's cache which is a problem when an update happens.  If bean1 is updated on node1, and is already cached on node2, then node2 would not be aware of bean1's update.   In the update() of the cache plugin, I propose sending out a simple UDP packet to a list of cluster servers that have a UDP listenered.  The UDP packet would simply contain the bean type and id of the invalidated bean.  The UDP servers will then remove this bean and id from their local APC cache.

This architecture also assumes that each server running redbean is connected to a single MySQL instance or has some sort of multi-master mysql backend server.

I've played around with using CouchDB as the caching store, but I feel like it may be overkill for my usecase.

redbeanie

unread,
Mar 20, 2013, 12:44:51 PM3/20/13
to redbe...@googlegroups.com
This is just a high level discussion. The actual implementation of the UDP server may be a single standalone process that will then make HTTP requests to a cachecontroller.php on each of the node servers.

So flow would be like:
1) bean1 on node1 gets invalidated
2) udp packet sent by redbean cache plugin to UDP server running on localhost
3) udp server is configured with node2 and node3 addresses (http://node2/cachecontrol.php and http://node3/cachecontrol.php)
4) udp server makes get requests to both cachecontrol with bean1 type and id.
5) node2 and node3 clear apc cache entries for bean1 if they exist

The reason for running the cachecontrol.php is because it needs to run in the apache server context to have access to the APC shared memory.

gabor

unread,
Mar 20, 2013, 3:06:02 PM3/20/13
to redbe...@googlegroups.com

Hi,

Looks like a nice project.

Would love to see a plugin for this ;)

cheers,
Gabor

redbeanie

unread,
Mar 20, 2013, 6:58:06 PM3/20/13
to redbe...@googlegroups.com
Hi Gabor,

I've attached the barebones APC Cache plugin (without any code related to the UDP based cache syncronization solution).  Please excuse my coding style or naming conventions.  Feel free to modify it as you see fit and incorporate it into the main Redbean distribution as you see fit.  I think this code can be used fairly safely in a single server environment.  Depending on your beans and complexity, it can dramatically increase performance (particularly if you use R::load($type,$id) when loading some relations or whenever possible).  This is because the cache entry is stored with a key of "$type-$id".

Also, a helpful note, PHP comes with an apc.php script to help view and manage your APC cache.. so you can get some nice statistics about what beans are in the cache, how many cache hits and cache misses etc.
Redbean_Plugin_APCCache.php

redbeanie

unread,
Mar 20, 2013, 7:28:06 PM3/20/13
to redbe...@googlegroups.com
Also, I forgot to mention some other caveats:
1) If you update your database tables or schemas directly, then you must clear the APC cache (I just restart the apache server.  but there are other ways to clear the cache)
2) If you use PHP CLI to manipulate your redbean objects, then your database data may get out of sync with what is in the APC cache.  So this is another case you need to clear the APC cache.
Reply all
Reply to author
Forward
0 new messages