Redis with PHP - Implementing Data Caching

I installed redis on my server and implemented object caching for the data returned in a PHP based web application. The php model essentially performs a rather complex query and returns a detailed array of data. I checked the caching and found that everything was working as expected. First I check if the key exists in redis. If this happens, redis will return the data, the model will be uneserialized and return the previously saved data. If the cache expires, the model executes the sql query, returns the data, and sets the key and serialized value to redis.

So here are my questions.

  • I'm not sure how to really rate this, as it is based on all browsers. What tools exist there that will allow me to get a reasonable benchmark for comparing caching rather than caching. I think maybe a php script that calls api 1000 times through curl.

  • I implemented this in redis because I once read that caching with redis will work through multiple sessions or IP addresses accessing the site. For example, if the api gets access 1000 times per hour by several IP addresses / users, I assume that this approach will reduce the load on the mysql server and allow redis to do the work of returning the cached data. Can someone shed some light on this? Are my assumptions valid?

All comments are welcome!

Thanks!

Dave

+4
source share
2 answers

To compare a website, I would use something like Siege instead of writing a specific PHP script.

Regarding the use of Redis, caching things in memory stores such as memcached or Redis is now very common. Both memcached and Redis are suitable for this purpose, although memcached may be easier to configure for pure caching. 1000 times per hour represents only 3.6 TPS - any data storage (including MySQL) will support such traffic without any problems. Now multiply this traffic by 100 or 1000, and the caching level (memcached or Redis) will become mandatory to protect your database.

To use Redis for caching, you can check the EXPIRE command and see maxmemory-policy in the configuration file.

+7
source

I did extensive testing for caching for the Zend_Cache library. Tests were conducted using several php-cli processes and randomized data, and also read read performance, write performance, and cache clear performance. If you only test the cache server, the performance of the web server is not relevant, so I recommend testing through the CLI to make testing easier. In addition, testing with only one process will not give you an accurate picture of the backend performance under heavy load.

MySQL is very fast itself, and if you make indexed queries with a single record, then MySQL's own query cache will be very fast. I would recommend adding an extra caching layer for slow (aggregated results of multiple queries or creating HTML snippets). You can use Zend_Cache without including the entire Zend structure, so I highly recommend that you check both Cm_Cache_Backend_Redis and Cm_Cache_Backend_File .

0
source

Source: https://habr.com/ru/post/1393472/


All Articles