FrankenPHP worker mode; what are the best practices regarding services resetting? #1486
-
|
Hello! I’ve been testing FrankenPHP for a few weeks, and I’m really impressed with the performance in worker mode. However, I have a question about singletons, service resetting, and statefulness when using Symfony with FrankenPHP. I understand that I need to implement the Here’s my question: Imagine 100 parallel requests hitting the application. FrankenPHP needs to reset the service after each request to ensure consistency. But if everything is stored in RAM and there’s only one instance of a service (e.g., a singleton), how does FrankenPHP handle this? Does it create a separate copy of the service for each request? Or does it synchronize concurrency - perhaps pausing execution (like Additionally, what are the best practices for managing statefulness and resetting state in FrankenPHP? Let’s say I have an I/O-bound service, like one interacting with Redis, Memcached, or another cache storage. Should I reconnect to the server within the public function reset()
{
$this->cache->reconnect();
}Or should I handle reconnection lazily, only when I actually need to use the cache, like this: class Cache
{
public function get(string $key)
{
// Reconnect logic here, e.g.,
$this->cache->reconnect();
// Then normal logic, e.g.,
return $this->cache->get($key);
}
}Which approach is recommended, and why? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
|
Also, regarding unhandled exceptions: what happens if something throws an exception inside the |
Beta Was this translation helpful? Give feedback.
-
|
Workers have a configured maximum number of threads, which limits how many requests can be run in parallel. Threads do share some memory, but in this case each thread has its own instance of the service. You can reconnect to external services after each request, but it's usually better for performance to just keep the connection open across requests. The only time this is potentially problematic is if you forgot to commit a SQL TRANSACTion after a request. But in most cases it just makes IO more efficient. Not sure how Symfony handles errors in its reset interface, but I imagine these would lead to a worker script restarting entirely, taking the performance hit to ensure safety. |
Beta Was this translation helpful? Give feedback.
-
|
Ideally you just keep the connection open. If reconnecting is absolutely necessary, it would probably be better for latency to do it after a request is finished. |
Beta Was this translation helpful? Give feedback.
Yeah you are right, it will be 4 x 10 = 40 threads. Each thread has its own instance of the service. A worker is just a script that is executed by a configured amount of threads (calling
frankenphp_handle_request). All threads run in a single process, even those of different worker scripts.The reason you are seeing 1, 2, 3, 4, 5, 6 instead of 1, 1, 1, 2, 2, 2, 3, 3, 3 is that requests are dispatching to threads in order. So if thread 1 is busy, check thread 2, if thread 2 is busy, check thread 3 and so on. This helps workers be more efficient by primarily using the 'hot' threads and minimizes connections to external services.
It's also possible to start additional threads only once they …