Redis clients in

Redis is a client server program, the server provides data storage services and so on, client connections the server and send commands to the server through, reading or writing data, in simple terms, the client is a tool that we communicate through it with Redis server and complete data manipulation.

Redis core client is not, the core is its Redis server program, the server program is complete data deposit, withdraw, persistence, and so we used frequent performer various operations. But it does not mean that the client no effect, the client throughout the Redis service system is also very important part. Benpian take a look at some of the features Redis client and the realization of the principle.

First, the basic properties of the client

redis client for abstract data structure, server.h / client architecture, I am here redis-4.0.x versions, different versions may be slightly different, each redis client successfully connected to the server after server creates a structure instance client, and in the form of a linked list of links to all successful connection of the client.

The main role is to store the current structure of the client's large number of attributes, sockets, name, logo, etc. status information, which is very important, when the server to client service, a lot of information such as the current command to be executed , parameters are available here. We understand one by one.

1, client name

By default, all connections are successful client without a name, you can send it to the service list client command to verify that it returns the current server successfully established clients as well as their basic information. E.g:

image

You can see, name field is empty by default, if you want a higher degree of recognition of your client, you can send to the server client setname named as your client, I will not do presentations, and the name of the client information stored in the client structure of the name field.

typedef struct client {
    .........
    robj *name;             /* As set by CLIENT SETNAME. */           
    .........
} client;

复制代码

2, mark

Some status flag is used to describe the current or role redis client is an integer corresponding to the data field structure.

typedef struct client {
    .........
    int flags;              /* Client flags: CLIENT_* macros. */
    .........
} client;

复制代码

Redis is defined in a lot of clients sign,

image

A flags field integer, by a binary or | () stored simultaneously over states, for example:

flags = 0000 0110 = CLIENT_MASTER | CLIENT_MONITOR
复制代码

Of course, the above values ​​that flages just cited an example, describing the current client is a master node Server (when for the master copy from the node, the master node will connect as a client transmits RDB file to the client from the node ), and is being executed MONITOR command. The former describes the role of the client, the latter describes the client state.

All in all, redis client flags field can describe the current role of the client, the client can also record the current status information of all kinds, it is the server understand the client's information is a very important field.

3, an input / output buffer

redis server to receive commands sent by client request requires a lot of steps to deal with and implement the relevant command invocation, and finally the data back to the client, then the input buffer is actually a small memory for storing client sends over command, including parameters, this memory space by default can not exceed 1GB, otherwise redis server will be forced to close the connection to the client.

typedef struct client {
    .........
    sds querybuf;           /* Buffer we use to accumulate client queries. */
    .........
} client;

复制代码

querybuf is the client buffer, which is a SDS type of field, then stated that this is a dynamically expand the input buffer.

Of course, we can also look at querybuf distribution and use of the current client by client list.

image

Wherein qbuf qbuf-free and is used to describe the state of client input buffer. I have not written here in this oversized command, so querybuf here only allocated 32,768 bytes.

ps: try not to use too much KEY, this will cause the client querybuf take up too much memory, this will lead to redis server program memory footprint is too high, if more than maxmemory limit, it will trigger the LRU KEY eliminated or program exceptions.

In addition, redis client as well as an output buffer for caching server response reply.

Output buffer, there are two, one is a fixed size, for storing the server in response to a simple example: OK, error information. There is one kind of non-fixed buffer length, its length is dynamically extensible, for storing the contents of some of the longer response.

typedef struct client {
    .........
    /* Response buffer */
    int bufpos;
    char buf[PROTO_REPLY_CHUNK_BYTES];
    .........
} client;

复制代码

PROTO_REPLY_CHUNK_BYTES equal to 16 * 1024, is also the default output buffer has a fixed 16K, bufpos record the number of bytes used in the buffer is fixed.

typedef struct client {
    .........
    list *reply;            /* List of reply objects to send to the client. */
    .........
} client;

复制代码

Dynamic buffer linked list to achieve, you can return greater key for us, for example, some of the set, list the collection and so on. We can see the use of the output buffer through the client list command.

image

obl represents a fixed buffer length, oll representatives dynamic buffer length, omem represents the fixed buffers and dynamic buffer total number of occupied bytes.

ps: the output buffer memory can limit the maximum upper limit by configuring client-output-buffer-limit, if the same abuse as causes redis server memory soared, recommended to configure a little small size of the output buffer.

Second, the three types of clients

redis client divided into three types, a common client, publish subscription client, slave client. The most common clients of our clients Needless to say, we also use.

redis client can subscribe to any number of channels, if you subscribe to a channel server, then your client will also publish a subscription client, when the channel a new message, the server will push to you about redis publish-subscribe feature, we will follow-up detail.

When redis cluster node when the deployment of a master-slave, all the nodes from the server, also known as slave clients, they regularly pull the latest data to the master, follow the detailed content description.

Next we analyze the redis server program implementation, and the mysterious serverCron timer function implementation.


Public concern is not lost, love to share a programmer.
No reply to the public "1024" author plus micro-channel to explore learning!
Each article codes used in all cases the material will be uploaded my personal github
github.com/SingleYam/o…
Welcome to tread!

No public YangAM

Guess you like

Origin juejin.im/post/5e6a3b1d6fb9a07c9f3ff291