Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: use hot worker threads when possible #1126

Merged
merged 10 commits into from
Nov 4, 2024
Merged

Conversation

AlliBalliBaba
Copy link
Collaborator

This PR is a first small step towards auto-scaling worker threads. The main idea is to re-use 'hot threads'.

Workers are long running processes, they often persist objects or external connections across multiple requests. Dispatching requests Round-Robin style will sometimes cause the opening and closing of unnecessary connections.

This is easily optimized by dispatching to all threads in a specified order. This way we are mainly using a number of 'hot' worker threads for requests, while 'cold' threads can stay idle for potential latency spikes.

While it seems like this approach will cause additional overhead, it actually reduces overhead slightly (especially on a high number of threads).

Benchmarks

wrk -t4 -c150 -d60 http://localhost (done locally)

20 CPU cores 40 threads 'Hello world'
main branch: ~72000 RPS
this branch: ~75000 RPS

20 CPU cores 100 threads 'IO simuation'
main branch: ~28000 RPS
this branch: ~33000 RPS

The 'IO simulation' does this in addition to the 'Hello world':

// do 10_000 assignments
for($i=0; $i<10_000; $i++) {
    $a = $i;
}
// sleep 1 millisecond
usleep(1000); 

@AlliBalliBaba
Copy link
Collaborator Author

Not sure if this would still be called 'Round Robin'. We could also keep it as a 'dispatch_mode' configuration, like Swoole does. I'm not sure though if regular Round Robin would ever be preferred.

@dunglas
Copy link
Owner

dunglas commented Oct 30, 2024

Let's keep only the best mode, unless there is a use case for others.

Copy link
Owner

@dunglas dunglas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea!

worker.go Outdated Show resolved Hide resolved
@AlliBalliBaba AlliBalliBaba marked this pull request as ready for review November 1, 2024 15:48
@AlliBalliBaba
Copy link
Collaborator Author

AlliBalliBaba commented Nov 2, 2024

Was hoping for my actions to be back this month 😞 , guess it's a bug then

@dunglas dunglas changed the title perf: Re-use hot worker threads perf: use hot worker threads when possible Nov 4, 2024
@dunglas dunglas merged commit 1c3ce11 into main Nov 4, 2024
45 checks passed
@dunglas dunglas deleted the perf/reuse-hot-worker-threads branch November 4, 2024 15:18
@dunglas
Copy link
Owner

dunglas commented Nov 4, 2024

Thank you!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants