How does a single thread run on multiple cores?


REDIS is single threaded. So what does it mean when I deploy it to machines with 2 or more cores? How does the single thread utilize these multiple cores?

In: Engineering

It doesn’t. That’s the point – each thread only runs on a single CPU core. If REDIS doesn’t support multiple threads or multiple processes, it will only use 1 core.

Other things can use the other core though. The OS itself, scheduled jobs, remote access, logging, whatever else can use the other core without impacting REDIS.

In order to process multiple threads the OS runs the thread in turns, each one gets to run on an available core for a few milliseconds so over time all threads can progress.

If a thread has no CPU affinity it can happen that on the next turn it runs on a different core, so the thread technically runs on multiple cores, but only on one core at a time. To make it clear this means that at most it’s processed as fast as a single core allows.

Because of this, when running a heavy thread on a dual core CPU the task manager may display both cores having a load around 50% giving the impression the thread runs on both cores simultaneously but this really means it runs on each one about 50% of the time.

A thread can only run on one core. But it doesn’t have to be the same core every time it runs. It can use core 1, then core 2, then core 1, then core 2 – the thread doesn’t really notice.

Sometimes other parts of the operating system have their own threads, so it might still be faster on a dual core machine than a single core machine, even though redis is only using one core at a time.