问题
I have successfully implemented a combination of redis and mysql. At one section of my application I thought I would reduce load on mysql server and use redis until the data gets changed, however I observe that it's still faster when the same data is fetched from Mysql than redis.
Here is scenario.
User1: 10,000 records with seldom one off change in a day or so.
What I do is whole object that fetches these 10K records, (serialized object of about 20mb in size) is saved to redis. The idea is that since subsequent 100 to 1000 requests will only be just page refreshes so why not avoid mysql hits and get this data from redis.
However, I have observed that when this object is fetched from Redis, it takes more time than when I flush redis and the mysql query is hit. I thought redis would have been faster or have the same mysql-like speed at least, but here it's different.
Also, I have observed that while fetching the 20mb object from Redis my php gives the "allowed memory exhausted" error (which I know how to fix) but it doesn't give out any error when the same data is fetched from mysql.
Could it be that Redis is not fit for caching huge objects? Or is there something else?
Thanks
回答1:
Redis is not designed/fit for storing/fetching large objects. You will suffer while getting/setting these objects in network/bandwidth. Since it is single threaded, other requests also will suffer while storing/fetching these large objects.
As it is stated in the benchmark documentation;
Speed of RAM and memory bandwidth seem less critical for global performance especially for small objects. For
large objects (>10 KB)
, it may become noticeable though. Usually, it is not really cost-effective to buy expensive fast memory modules to optimize Redis.
What you may do is, redesign your cache layer with hashes
or lists
depending on the query requirements of your application. You may check here for additional information. Also i answered a similar question in here
来源:https://stackoverflow.com/questions/62447931/laravel-7-mariadb-in-combination-with-redis-but-redis-behaves-slower-with-large