redis 到底有几个线程

2020-08-07

redis5.0 是 4 个,废话少说,上源码

1
initServer();
1
2
3
4
5
6
7
8
9
10
11
/* Ready to spawn our threads. We use the single argument the thread
* function accepts in order to pass the job ID the thread is
* responsible of. */
for (j = 0; j < BIO_NUM_OPS; j++) {
void *arg = (void*)(unsigned long) j;
if (pthread_create(&thread,&attr,bioProcessBackgroundJobs,arg) != 0) {
serverLog(LL_WARNING,"Fatal: Can't initialize Background Jobs.");
exit(1);
}
bio_threads[j] = thread;
}
1
2
3
4
5
/* Background job opcodes */
#define BIO_CLOSE_FILE 0 /* Deferred close(2) syscall. */
#define BIO_AOF_FSYNC 1 /* Deferred AOF fsync. */
#define BIO_LAZY_FREE 2 /* Deferred objects freeing. */
#define BIO_NUM_OPS 3 // 线程总数,redis主线程+3个后台线程

看一下

image-20200807232518076

把后台线程改成2看看

image-20200807232545128

image-20200807232557289

变成3个了

之所以redis是单线程其实是处理客户端请求的是单线程,但还有后台线程去处理aof,rdb等耗时的任务。

👌

Tags: redis
使用支付宝打赏
使用微信打赏

若你觉得我的文章对你有帮助,欢迎点击上方按钮对我打赏

扫描二维码,分享此文章