Php + redis in actual projects HTTP 500: Internal Server Error troubleshooting, redisinternal
Problem description
The number of users is growing rapidly, and the access volume doubles in a short period of time. Due to the relatively good capacity planning in the early stage, hardware resources can be supported, but the software system has encountered a major problem:
40% of requests will return HTTP 500: Internal Server Error
By viewing the log, the error is found in the connection processing of PHP <-> Redis.
Debugging
1st times
At the beginning, no root cause was found. You can only try a variety of Error-related methods, such:
Increase the number of PHP connections and increase the timeout time from 500 ms to 2.5 s.
Disable default_socket_timeout in PHP settings
Disable SYN cookies in the host system
Check the number of file descriptors of Redis and Webservers
Increase the mbuffer of the host system
Adjust the number of TCP backlogs
......
Tried many methods, but all of them are invalid
2nd times
I want to reproduce this problem in the pre-release environment. Unfortunately, it is still unsuccessful. It should be because the traffic is not large enough and cannot be reproduced.
3rd Times
Is the Redis connection not closed in the code?
Normally, PHP will automatically close the resource connection at the end of execution, but there will be a memory leak in the old version. For the sake of security, modify the code and manually close the connection.
The result is invalid.
4th times
Suspect target: phpredis client library
Perform A/B Test, replace the database predis, and deploy the database to 20% of the users in the data center.
Thanks to a good code structure, the replacement work is completed quickly
The result is still invalid, but there is also a good aspect, which can prove that phpredis is okay.
5th times
Check the Redis version, which is v2.6 and the latest version is v2.8.9.
Try upgrading Redis. It will not work after upgrade
It's okay. Be optimistic. This doesn't take the Redis version to the latest version by the way.
6th times
By searching for a large number of documents, we found a good debugging method in the official documents, Redis Software Watchdog, opened and executed:
$ redis-cli --latency -p 6380 -h 1.2.3.4min: 0, max: 463, avg: 2.03 (19443 samples)
View Redis logs:
...[20398] 22 May 09:20:55.351 * 10000 changes in 60 seconds. Saving...[20398] 22 May 09:20:55.759 * Background saving started by pid 41941[41941] 22 May 09:22:48.197 * DB saved on disk[20398] 22 May 09:22:49.321 * Background saving terminated with success[20398] 22 May 09:25:23.299 * 10000 changes in 60 seconds. Saving...[20398] 22 May 09:25:23.644 * Background saving started by pid 42027...
Problems found:
Data is saved to the hard disk every few minutes. Why does it take about 400 ms for fork to store one background storage? (it can be seen from the time when the above logs contain 1st and 2nd logs)
At this point, we finally found the root cause of the problem. Because there is a large amount of data in the Redis instance, it is very time-consuming to operate the fork background process persistently each time, and keys are frequently modified in their services, this also causes frequent persistence triggering, which also causes frequent congestion to Redis.
Solution: Use a separate slave for persistence
This slave does not process real traffic requests. The only function is to process persistence and transfer the persistent operations on the previous Redis instance to this slave.
The effect is very obvious and the problem is basically solved, but sometimes an error is reported.
7th times
Troubleshooting may block Redis slow queries and found that keys * is used *
Because there is more and more data in Redis, this command will naturally cause serious blocking.
You can use scan to replace
8th times
After the previous adjustments, the problem has been solved. In the next few months, even if the traffic continues to grow, the problem will be resisted.
But they are aware of the new problems:
The current method is to create a Redis connection with a single request, execute several commands, and then disconnect the connection. When the Request volume is large, this method produces a serious performance waste, more than half of the commands are used to process connection operations, which exceeds the business logic processing and slows down Redis.
Solution: Introduce proxy. They chose twitter's twemproxy and only need to install proxy on each webserver. twemproxy is responsible for persistent connection with the Redis instance, which greatly reduces connection operations.
Twemproxy has two more convenient features:
Support for memcached
Blocks time-consuming or dangerous commands, such as keys and flushall.
The effect is naturally perfect, and there is no need to worry about previous connection errors.
9th times
Continue optimization through data sharding:
Data splitting and isolation for different contexts
Consistent hash partitioning for data in the same context
Effect:
Reduces requests and loads on each machine.
Improves cache reliability without worrying about node faults
Summary
The author of the original article wrote very well and described in detail their growth history in Redis applications, which is a valuable reference for practical experience.
Original address http://tech.trivago.com/2017/01/25/learn-redis-the-hard-way-in-production