This README is just a fast *quick start* document. You can find more detailed documentation at [redis.io](https://redis.io).
What is Redis?
--------------
---
Redis is often referred to as a *data structures* server. What this means is that Redis provides access to mutable data structures via a set of commands, which are sent using a *server-client* model with TCP sockets and a simple protocol. So different processes can query and modify the same data structures in a shared way.
...
...
@@ -20,15 +20,25 @@ If you want to know more, this is a list of selected starting points:
* The full list of Redis commands. https://redis.io/commands
* There is much more inside the official Redis documentation. https://redis.io/documentation
Building Redis
--------------
What is Redis Community Edition?
---
Redis OSS was renamed Redis Community Edition (CE) with the v7.4 release.
Redis Ltd. also offers [Redis Software](https://redis.io/enterprise/), a self-managed software with additional compliance, reliability, and resiliency for enterprise scaling,
and [Redis Cloud](https://redis.io/cloud/), a fully managed service integrated with Google Cloud, Azure, and AWS for production-ready apps.
Read more about the differences between Redis Community Edition and Redis [here](https://redis.io/comparisons/oss-vs-enterprise/).
Redis can be compiled and used on Linux, OSX, OpenBSD, NetBSD, FreeBSD.
Building Redis Community Edition
---
Redis Community Edition can be compiled and used on Linux, OSX, OpenBSD, NetBSD, FreeBSD.
We support big endian and little endian architectures, and both 32 bit
and 64 bit systems.
It may compile on Solaris derived systems (for instance SmartOS) but our
support for this platform is *best effort* and Redis is not guaranteed to
support for this platform is *best effort* and Redis Community Edition is not guaranteed to
work as well as in Linux, OSX, and \*BSD.
It is as simple as:
...
...
@@ -45,15 +55,15 @@ as libsystemd-dev on Debian/Ubuntu or systemd-devel on CentOS) and run:
% make USE_SYSTEMD=yes
To append a suffix to Redis program names, use:
To append a suffix to Redis Community Edition program names, use:
% make PROG_SUFFIX="-alt"
You can build a 32 bit Redis binary using:
You can build a 32 bit Redis Community Edition binary using:
% make 32bit
After building Redis, it is a good idea to test it using:
After building Redis Community Edition, it is a good idea to test it using:
% make test
...
...
@@ -65,9 +75,9 @@ installed):
Fixing build problems with dependencies or cached build options
---------
---
Redis has some dependencies which are included in the `deps` directory.
Redis Community Edition has some dependencies which are included in the `deps` directory.
`make` does not automatically rebuild dependencies even if something in
the source code of dependencies changes.
...
...
@@ -85,13 +95,13 @@ those options are cached indefinitely until you issue a `make distclean`
command.
Fixing problems building 32 bit binaries
---------
---
If after building Redis with a 32 bit target you need to rebuild it
If after building Redis Community Edition with a 32 bit target you need to rebuild it
with a 64 bit target, or the other way around, you need to perform a
`make distclean` in the root directory of the Redis distribution.
`make distclean` in the root directory of the Redis Community Edition distribution.
In case of build errors when trying to build a 32 bit binary of Redis, try
In case of build errors when trying to build a 32 bit binary of Redis Community Edition, try
the following steps:
* Install the package libc6-dev-i386 (also try g++-multilib).
...
...
@@ -99,10 +109,10 @@ the following steps:
`make CFLAGS="-m32 -march=native" LDFLAGS="-m32"`
Allocator
---------
---
Selecting a non-default memory allocator when building Redis is done by setting
the `MALLOC` environment variable. Redis is compiled and linked against libc
Selecting a non-default memory allocator when building Redis Community Edition is done by setting
the `MALLOC` environment variable. Redis Community Edition is compiled and linked against libc
malloc by default, with the exception of jemalloc being the default on Linux
systems. This default was picked because jemalloc has proven to have fewer
fragmentation problems than libc malloc.
...
...
@@ -116,9 +126,9 @@ To compile against jemalloc on Mac OS X systems, use:
% make MALLOC=jemalloc
Monotonic clock
---------------
---
By default, Redis will build using the POSIX clock_gettime function as the
By default, Redis Community Edition will build using the POSIX clock_gettime function as the
monotonic clock source. On most modern systems, the internal processor clock
can be used to improve performance. Cautions can be found here:
The purpose of a trademark is to identify the goods and services of a person or company without
causing confusion. As the registered owner of its name and logo, Redis accepts certain limited uses
...
...
@@ -244,10 +254,10 @@ Redis internals
===
If you are reading this README you are likely in front of a Github page
or you just untarred the Redis distribution tar ball. In both the cases
or you just untarred the Redis Community Edition distribution tar ball. In both the cases
you are basically one step away from the source code, so here we explain
the Redis source code layout, what is in each file as a general idea, the
most important functions and structures inside the Redis server and so forth.
the Redis Community Edition source code layout, what is in each file as a general idea, the
most important functions and structures inside the Redis Community Edition server and so forth.
We keep all the discussion at a high level without digging into the details
since this document would be huge otherwise and our code base changes
continuously, but a general idea should be a good starting point to
...
...
@@ -257,26 +267,26 @@ to follow.
Source code layout
---
The Redis root directory just contains this README, the Makefile which
The Redis Community Edition root directory just contains this README, the Makefile which
calls the real Makefile inside the `src` directory and an example
configuration for Redis and Sentinel. You can find a few shell
scripts that are used in order to execute the Redis, Redis Cluster and
Redis Sentinel unit tests, which are implemented inside the `tests`
configuration for Redis Community Edition and Redis Community Edition Sentinel. You can find a few shell
scripts that are used in order to execute the Redis Community Edition, Redis Community Edition Cluster and
Redis Community Edition Sentinel unit tests, which are implemented inside the `tests`
directory.
Inside the root are the following important directories:
*`src`: contains the Redis implementation, written in C.
*`src`: contains the Redis Community Edition implementation, written in C.
*`tests`: contains the unit tests, implemented in Tcl.
*`deps`: contains libraries Redis uses. Everything needed to compile Redis is inside this directory; your system just needs to provide `libc`, a POSIX compatible interface and a C compiler. Notably `deps` contains a copy of `jemalloc`, which is the default allocator of Redis under Linux. Note that under `deps` there are also things which started with the Redis project, but for which the main repository is not `redis/redis`.
*`deps`: contains libraries Redis Community Edition uses. Everything needed to compile Redis Community Edition is inside this directory; your system just needs to provide `libc`, a POSIX compatible interface and a C compiler. Notably `deps` contains a copy of `jemalloc`, which is the default allocator of Redis Community Edition under Linux. Note that under `deps` there are also things which started with the Redis project, but for which the main repository is not `redis/redis`.
There are a few more directories but they are not very important for our goals
here. We'll focus mostly on `src`, where the Redis implementation is contained,
here. We'll focus mostly on `src`, where the Redis Community Edition implementation is contained,
exploring what there is inside each file. The order in which files are
exposed is the logical one to follow in order to disclose different layers
of complexity incrementally.
Note: lately Redis was refactored quite a bit. Function names and file
Note: lately Redis Community Edition was refactored quite a bit. Function names and file
names have been changed, so you may find that this documentation reflects the
`unstable` branch more closely. For instance, in Redis 3.0 the `server.c`
and `server.h` files were named `redis.c` and `redis.h`. However the overall
...
...
@@ -288,13 +298,13 @@ server.h
The simplest way to understand how a program works is to understand the
data structures it uses. So we'll start from the main header file of
Redis, which is `server.h`.
Redis Community Edition, which is `server.h`.
All the server configuration and in general all the shared state is
defined in a global structure called `server`, of type `struct redisServer`.
A few important fields in this structure are:
*`server.db` is an array of Redis databases, where data is stored.
*`server.db` is an array of Redis Community Edition databases, where data is stored.
*`server.commands` is the command table.
*`server.clients` is a linked list of clients connected to the server.
*`server.master` is a special client, the master, if the instance is a replica.
...
...
@@ -302,7 +312,7 @@ A few important fields in this structure are:
There are tons of other fields. Most fields are commented directly inside
the structure definition.
Another important Redis data structure is the one defining a client.
Another important Redis Community Edition data structure is the one defining a client.
In the past it was called `redisClient`, now just `client`. The structure
has many fields, here we'll just show the main ones:
```c
...
...
@@ -322,7 +332,7 @@ The client structure defines a *connected client*:
* The `fd` field is the client socket file descriptor.
*`argc` and `argv` are populated with the command the client is executing, so that functions implementing a given Redis command can read the arguments.
*`querybuf` accumulates the requests from the client, which are parsed by the Redis server according to the Redis protocol and executed by calling the implementations of the commands the client is executing.
*`querybuf` accumulates the requests from the client, which are parsed by the Redis Community Edition server according to the Redis protocol and executed by calling the implementations of the commands the client is executing.
*`reply` and `buf` are dynamic and static buffers that accumulate the replies the server sends to the client. These buffers are incrementally written to the socket as soon as the file descriptor is writable.
As you can see in the client structure above, arguments in a command
...
...
@@ -356,9 +366,9 @@ we just use plain dynamic strings not wrapped inside a Redis object.
server.c
---
This is the entry point of the Redis server, where the `main()` function
This is the entry point of the Redis Community Edition server, where the `main()` function
is defined. The following are the most important steps in order to startup
the Redis server.
the Redis Community Edition server.
*`initServerConfig()` sets up the default values of the `server` structure.
*`initServer()` allocates the data structures needed to operate, setup the listening socket, and so forth.
...
...
@@ -369,7 +379,7 @@ There are two special functions called periodically by the event loop:
1.`serverCron()` is called periodically (according to `server.hz` frequency), and performs tasks that must be performed from time to time, like checking for timed out clients.
2.`beforeSleep()` is called every time the event loop fired, Redis served a few requests, and is returning back into the event loop.
Inside server.c you can find code that handles other vital things of the Redis server:
Inside server.c you can find code that handles other vital things of the Redis Community Edition server:
*`call()` is used in order to call a given command in the context of a given client.
*`activeExpireCycle()` handles eviction of keys with a time to live set via the `EXPIRE` command.
...
...
@@ -386,7 +396,7 @@ networking.c
---
This file defines all the I/O functions with clients, masters and replicas
(which in Redis are just special clients):
(which in Redis Community Edition are just special clients):
*`createClient()` allocates and initializes a new client.
* The `addReply*()` family of functions are used by command implementations in order to append data to the client structure, that will be transmitted to the client as a reply for a given command executed.
...
...
@@ -399,9 +409,9 @@ aof.c and rdb.c
---
As you can guess from the names, these files implement the RDB and AOF
persistence for Redis. Redis uses a persistence model based on the `fork()`
persistence for Redis Community Edition. Redis Community Edition uses a persistence model based on the `fork()`
system call in order to create a process with the same (shared) memory
content of the main Redis process. This secondary process dumps the content
content of the main Redis Community Edition process. This secondary process dumps the content
of the memory on disk. This is used by `rdb.c` to create the snapshots
on disk and by `aof.c` in order to perform the AOF rewrite when the
append only file gets too big.
...
...
@@ -450,10 +460,10 @@ This file also implements the `OBJECT` command.
replication.c
---
This is one of the most complex files inside Redis, it is recommended to
This is one of the most complex files inside Redis Community Edition, it is recommended to
approach it only after getting a bit familiar with the rest of the code base.
In this file there is the implementation of both the master and replica role
of Redis.
of Redis Community Edition.
One of the most important functions inside this file is `replicationFeedSlaves()` that writes commands to the clients representing replica instances connected
to our master, so that the replicas can get the writes performed by the clients:
...
...
@@ -467,10 +477,10 @@ Script
---
The script unit is composed of 3 units:
*`script.c` - integration of scripts with Redis (commands execution, set replication/resp, ...)
*`script_lua.c` - responsible to execute Lua code, uses `script.c` to interact with Redis from within the Lua code.
*`script.c` - integration of scripts with Redis Community Edition (commands execution, set replication/resp, ...)
*`script_lua.c` - responsible to execute Lua code, uses `script.c` to interact with Redis Community Edition from within the Lua code.
*`function_lua.c` - contains the Lua engine implementation, uses `script_lua.c` to execute the Lua code.
*`functions.c` - contains Redis Functions implementation (`FUNCTION` command), uses `functions_lua.c` if the function it wants to invoke needs the Lua engine.
*`functions.c` - contains Redis Community Edition Functions implementation (`FUNCTION` command), uses `functions_lua.c` if the function it wants to invoke needs the Lua engine.
*`eval.c` - contains the `eval` implementation using `script_lua.c` to invoke the Lua code.
...
...
@@ -482,7 +492,7 @@ Other C files
*`sds.c` is the Redis string library, check https://github.com/antirez/sds for more information.
*`anet.c` is a library to use POSIX networking in a simpler way compared to the raw interface exposed by the kernel.
*`dict.c` is an implementation of a non-blocking hash table which rehashes incrementally.
*`cluster.c` implements the Redis Cluster. Probably a good read only after being very familiar with the rest of the Redis code base. If you want to read `cluster.c` make sure to read the [Redis Cluster specification][4].
*`cluster.c` implements the Redis Community Edition Cluster. Probably a good read only after being very familiar with the rest of the Redis Community Edition code base. If you want to read `cluster.c` make sure to read the [Redis Cluster specification][4].
[4]:https://redis.io/topics/cluster-spec
...
...
@@ -505,12 +515,12 @@ For other details, please refer to the `COMMAND` command. https://redis.io/comma
After the command operates in some way, it returns a reply to the client,
usually using `addReply()` or a similar function defined inside `networking.c`.
There are tons of command implementations inside the Redis source code
There are tons of command implementations inside the Redis Community Edition source code
that can serve as examples of actual commands implementations (e.g. pingCommand). Writing
a few toy commands can be a good exercise to get familiar with the code base.
There are also many other files not described here, but it is useless to
cover everything. We just want to help you with the first steps.
Eventually you'll find your way inside the Redis code base :-)
Eventually you'll find your way inside the Redis Community Edition code base :-)