Data Structures Used By Redis Internally

Posted By : Abhinav Gupta | 31-May-2018
What is Redis ?
 
          Redis is a database written in C. It is a key-value pair based database. Redis's event-driven nature is what sets it apart from other databases. Redis makes use of libae to implement its asynchronous behavior.
 
Data Structures used by Redis :
 
This is a brief introduction of few of the important data structures and their fields that are used by the Redis Database. This is by no means an exhaustive list of all the data structures that are used by Redis.
 
Redis makes use of a global struct named server which is of the type struct redisServer for defining all the server configuration and shared states. Some of the important fields in this struct are 
struct redisServer 
{ 
     ...
     redisDb *db;
     dict *commands;
     list *clients;
     client *master;
     ...
 }

where "..." signifies that there are numerous other fields
 
 
 
db : db is a pointer to the first element of an array of Redis databases. A Redis database is also a struct which is defined by.struct redisDb Each redisDb instance has a unique id, and many hashtables to monitor data that is relevant to the database.
 
commands : this field is hashtable of commands, the datatype dict comes from a header dict.h that is included in the file where is redisServer defined.
 
clients : Active clients, are stored in a linked list datatype, which comes from the adlist.h header. An active client, in redis, is represented by struct client. Some of the important fields in struct client are 
 
struct client 
{
     int fd;
     sds querybuf;
     int argc;
     robj **argv;
     redisDb *db;
     list *reply;
     char buf[PROTO_REPLY_CHUNK_BYTES];
     ...
}
 
 
 
fd : fd is the client socket file descriptor
 
 
 
argc : argc stands for the number of arguments given by the client for a command
 
 
 
argv : argv are the actual arguments that are given by the client. This is a pointer of the type robj. The datatype robj is used very extensively througout redis. It looks like this
 
struct redisObject 
{
     unsigned type:4;
     unsigned encoding:4;
     unsigned lru:LRU_BITS;
     int refcount;
     void *ptr;
} robj;
 
This data structure is very general, and it is designed in such a way that it can represent all basic datatypes used in Redis like strings, lists, sets, sorted sets etc. 
 
type : This field makes it possible to know what the given robj instance has.
 
encoding : The type of encoding used affects the ptr variable
 
 
 
refcount : This variable makes it possible to reference the same object in multiple places, without allocating memory each time the object has to be used.
 
 
 
lru : This variable represents the least recently used bit. 
 
ptr : This ptr points to the actual representation of the robj on memory.
 
 
 
querybuf : all the requests from the client, are accumulated in this buffer. The server parses these requests according to the Redis protocol, and then the implementations of the command requested by the client is executed. The type sds comes from the header sds.h which provide dynamic safe strings.
 
reply : this is a dynamic buffer which is used along with the static buffer buf, to collect the replies to be sent by the server to the client. These replies are written to the socket as soon as the file descriptor is available for writing.
 
 
 
buf : this is a static buffer
 
master : The master, in redis, is a special client, if the database instance is a slave.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

About Author

Author Image
Abhinav Gupta

Abhinav is good at developing commercial software that save time and money. He is experienced with node.js and javascript.

Request for Proposal

Name is required

Comment is required

Sending message..