T086ѧϰ�� | վ��ѧԺ | �����ĵ� | ���� | Ъ���� | IP��ַ��ѯ | �������� | ���ջ��� | �����ֵ� | ���ù��� | ������Ϣ��ѯ

Redis�����ͻ������ӵ�һЩ�ڲ�ʵ�ֻ���

�� nosqlfan ����ʱ�䣺2013-01-24 | ���壺�� �� С��
[����]������Ҫ������ Redis �����ͻ������ӵ�һЩ�ڲ�ʵ�ֻ��ƣ��������Ӵ�������ʱ����������һϵ�����ݡ�Redis ͨ������һ�� TCP �˿ڻ��� Unix socket �ķ�ʽ���������Կͻ��˵����ӣ���һ�����ӽ�������Redis �ڲ�����...

������Ҫ������ Redis �����ͻ������ӵ�һЩ�ڲ�ʵ�ֻ��ƣ��������Ӵ�������ʱ����������һϵ�����ݡ�

ע�������������ݻ��� Redis2.6 �����ϰ汾��

���ӵĽ���

Redis ͨ������һ�� TCP �˿ڻ��� Unix socket �ķ�ʽ���������Կͻ��˵����ӣ���һ�����ӽ�������Redis �ڲ�����������һЩ������

  • ���ȣ��ͻ��� socket �ᱻ����Ϊ������ģʽ����Ϊ Redis �������¼������ϲ��õ��Ƿ�������·����ģ�͡�
  • Ȼ��Ϊ����socket ���� TCP_NODELAY ���ԣ����� Nagle �㷨
  • Ȼ�󴴽�һ�� readable ���ļ��¼����ڼ��������ͻ��� socket �����ݷ���

���ͻ������ӱ���ʼ������Redis ���鿴Ŀǰ����������Ȼ���Ա����úõ� maxclients ֵ������Ŀǰ�������Ѿ��ﵽ���������� maxclients �ˣ���ô˵���������Ӳ����ٽ��գ�Redis ��ֱ�ӷ��ؿͻ���һ�����Ӵ��󣬲����Ϲرյ��������ӡ�

�����˴���˳��

�����ж����ͻ��������� Redis�����Ҷ��� Redis ���������ô Redis �����˻��ȴ����ĸ��ͻ��˵������أ�������ʵ����ȷ������Ҫ�����������йأ�һ�ǿͻ��˶�Ӧ�� socket ��Ӧ�����ֵĴ�С������ kernal ���������ͻ����¼����Ⱥ�˳����

Redis ����һ���ͻ��˴������ݵIJ������£�

  • ���Դ����¼��� socket ����һ�� read()��ֻ��һ�Σ������ǰ����� socket �ϵ���Ϣ����Ϊֹ������Ϊ�˷�ֹ����ij�����ͻ��˳�������̫��������������ͻ��˵�������ʱ���ò���������������
  • ��Ȼ������һ�� read() �������ɺ������������۰������ٸ�������ᱻһ����˳����ִ�С������ͱ�֤�˶Ը����ͻ��������Ĺ�ƽ�Դ���

�������������� maxclients

�� Redis2.4 �У������������DZ�ֱ��Ӳ�����ڴ��������ģ�����2.6�汾������ֵ���ɿ����õġ�maxclients ��Ĭ��ֵ�� 10000����Ҳ������ redis.conf �ж�����ֵ�����޸ġ�

��Ȼ������ֵֻ�� Redis һ����Ը��ֵ��Redis �����չ˵�ϵͳ�����Խ���ʹ�õ��ļ����������������ơ�������ʱ Redis ������ϵͳ�� soft limit���Բ鿴�����ļ��������ĸ������ޡ�����ϵͳ���õ����֣�С������ϣ����������������32����ô���� maxclients �����ý��������ã�Redis �ᰴϵͳҪ��������������ֵ������32����Ϊ Redis �ڲ���ʹ������32���ļ�������������������ʹ�õ��൱���������õ��������ż�32����

������˵��������������ʱ��maxclients ���ú��������õ���������Redis �����������н�������Ӧ����־��¼��������������ϣ�����������ͻ�������Ϊ100000������ Redis ��Ҫ 100000+32 ���ļ�����������ϵͳ�������ļ�������������Ϊ10144������ Redis ֻ�ܽ� maxclients ����Ϊ 10144 – 32 = 10112��

$ ./redis-server --maxclients 100000
[41422] 23 Jan 11:28:33.179 # Unable to set the max number of files limit to 100032 (Invalid argument), setting the max clients configuration to 10112.

����˵���������� maxclients ֵʱ������˳���޸�һ������ϵͳ���ã���Ȼ�����ɿ���־�ĺ�ϰ��Ҳ�ܷ����������⡣

���������÷����Ϳ������˵������ˣ�������ֻ�޸Ĵ˴λỰ�����ƣ�Ҳ����ֱ��ͨ��sysctl �޸�ϵͳ��Ĭ�����á��磺

ulimit -Sn 100000 # This will only work if hard limit is big enough.
sysctl -w fs.file-max=100000

����������������

���� Redis ��������Ҳ���������ķ���ֵ����˵������С�����Dz��ɿصģ�������һ���򵥵�����ܹ����������Ӵ��ķ������ݡ�����Ҳ�п�����Ϊִ������̫�࣬�����ķ������ݵ����ʳ��������ͻ��˷��͵����ʣ���ʱҲ��������Ϣ�ѻ����Ӷ���������������Խ��Խ����ռ�ù����ڴ棬��������ϵͳ������

���� Redis ������һЩ�����������������������ij��֣���Щ���������ڲ�ͬ�����Ŀͻ��ˣ��в�ͬ��������������С���ƣ����Ʒ�ʽ�����֣�

  • һ���Ǵ�С���ƣ���ijһ���ͻ��˵Ļ���������ijһ��Сʱ��ֱ�ӹرյ������ͻ�������
  • ��һ���ǵ�ijһ���ͻ��˵Ļ���������һ��ʱ��ռ�ÿռ�����ʱ��Ҳֱ�ӹرյ��ͻ�������

���ڲ�ͬ�ͻ��˵IJ������£�

  • ����ͨ�ͻ�����˵������Ϊ0��Ҳ���Dz����ƣ���Ϊ��ͨ�ͻ���ͨ����������ʽ����ϢӦ��ģʽ���磺�������󣬵ȴ����أ��ٷ��������ٵȴ����ء�����ģʽͨ�����ᵼ�������������Ķѻ����͡�
  • ���� Pub/Sub �ͻ�����˵����С������32m������������������32mʱ�����ر����ӡ������������ǣ����ͻ��˻�������С����60�볬��8m��Ҳ�ᵼ�����ӹرա�
  • ������ Slave �ͻ�����˵����С������256m�������������ǵ��ͻ��˻�������С����60�볬��64mʱ���ر����ӡ�

�������ֹ������ǿ����õġ�����ͨ�� CONFIG SET ���������޸� redis.conf �ļ������á�

���뻺������С����

Redis �����뻺������С�����ƱȽϱ��������ͻ��˴�����������С����1Gʱ�������˻�ֱ�ӹر����ӡ����ַ�ʽ������Ч��ֹһЩ�ͻ��˻������� bug ���µ����뻺�������������⡣

Client ��ʱ

�Ե�ǰ�� Redis �汾��˵��������Ĭ���Dz����رճ��ڿ��еĿͻ��˵ġ������������޸�Ĭ��������������ϣ���ij�ʱʱ�䡣�����ͻ��˳����೤ʱ���޽�������ֱ�ӹرա�ͬ������Ҳ����ͨ�� CONFIG SET ���������޸� redis.conf �ļ������á�

ֵ��ע�����ǣ���ʱʱ�������ã�ֻ����ͨ�ͻ��������ã��� Pub/Sub �ͻ�����˵�����ڿ���״̬�������ġ�

���⣬ʵ�ʵij�ʱʱ�����ܲ������趨��������ȷ��������Ϊ Redis ���������ü�ʱ��������ѵ�����ķ����������ͻ��˳�ʱ������ͨ��һ�ֽ���ʽ�ķ�ʽ�����ɣ�ÿ�μ���һ���֡����Ե��µĽ������ǣ����������õij�ʱʱ����10s��������ʵִ�е�ʱ���dz�ʱ12s���ͻ��˲ű��رա�

CLIENT ����

Redis �� CLIENT �����ܹ�ʵ�����ֹ��ܣ��������ӵ�״̬��ɱ��ij�������Լ�Ϊ�����������֡�

CLIENT LIST �����ܹ���ȡ��ǰ���пͻ��˵�״̬��ʹ�÷������£�

redis 127.0.0.1:6379> client list
addr=127.0.0.1:52555 fd=5 name= age=855 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=32768 obl=0 oll=0 omem=0 events=r cmd=client
addr=127.0.0.1:52787 fd=6 name= age=6 idle=5 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=ping

������������������֪��Ŀǰ�� Redis �������ͻ������ӣ�ÿһ�б�ʾһ�����ӵĸ�����Ϣ��

  • addr: �ͻ��˵�TCP��ַ������IP�Ͷ˿�
  • fd: �ͻ������� socket ��Ӧ���ļ�������������
  • name: ���ӵ����֣�Ĭ��Ϊ�գ�����ͨ�� CLIENT SETNAME ����
  • age: �ͻ��˴���������
  • idle: �ͻ��˿��е�����
  • flags: �ͻ��˵����� (N ��ʾ��ͨ�ͻ��ˣ��������ͼ� http://redis.io/commands/client-list)
  • omem: �����������Ĵ�С
  • cmd: ����ִ�е���������

�����Բ鿴 CLIENT LIST ���ĵ��������鿴���������ĺ��塣

����ͨ������������ȡ���ͻ����б��󣬾Ϳ���ͨ�� CLIENT KILL ������ɱ��ָ���������ˡ�CLIENT KILL �IJ������������� addr ֵ��

�������ᵽ�� CLIENT SETNAME �� CLIENT GETNAME ��������Ϊһ����������һ�����֡�

�ο���Դ��http://redis.io/topics/clients

ԭ�ģ�http://blog.nosqlfan.com/html/4153.html

  • ת����ע����Դ��ITѧϰ�� ��ַ��http://www.t086.com/ �����������Ƽ�������
  • �ر������� ��վ�������ر�������ֹת�ص�ר�������������¿�������ת�أ���������ע��������ԭʼ���ߡ����°�Ȩ������ԭʼ�������С����ڱ���վת�����µĸ��˺���վ�����DZ�ʾ������л�⡣������վת�ص������а�Ȩ��������ϵ���ǣ����ǻᾡ�����Ը�����
������ѯ Health Check Gulangyu
© 2017 T086ѧϰ�� - T086.com(ԭitlearner.com)
RunTime:11.07ms QueryTime:7