Giter VIP home page Giter VIP logo

redis / redis Goto Github PK

View Code? Open in Web Editor NEW
64.8K 64.8K 23.5K 135.12 MB

Redis is an in-memory database that persists on disk. The data model is key-value, but many different kind of values are supported: Strings, Lists, Sets, Sorted Sets, Hashes, Streams, HyperLogLogs, Bitmaps.

Home Page: http://redis.io

License: Other

Makefile 0.24% Shell 0.27% C 73.87% C++ 0.07% Ruby 0.27% Tcl 24.78% Smarty 0.01% Python 0.46% JavaScript 0.01%
cache database key-value message-broker nosql redis

redis's Introduction

This README is just a fast quick start document. You can find more detailed documentation at 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.

Data structures implemented into Redis have a few special properties:

  • Redis cares to store them on disk, even if they are always served and modified into the server memory. This means that Redis is fast, but that it is also non-volatile.
  • The implementation of data structures emphasizes memory efficiency, so data structures inside Redis will likely use less memory compared to the same data structure modelled using a high-level programming language.
  • Redis offers a number of features that are natural to find in a database, like replication, tunable levels of durability, clustering, and high availability.

Another good example is to think of Redis as a more complex version of memcached, where the operations are not just SETs and GETs, but operations that work with complex data types like Lists, Sets, ordered data structures, and so forth.

If you want to know more, this is a list of selected starting points:

Building Redis

Redis 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 work as well as in Linux, OSX, and *BSD.

It is as simple as:

% make

To build with TLS support, you'll need OpenSSL development libraries (e.g. libssl-dev on Debian/Ubuntu) and run:

% make BUILD_TLS=yes

To build with systemd support, you'll need systemd development libraries (such 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:

% make PROG_SUFFIX="-alt"

You can build a 32 bit Redis binary using:

% make 32bit

After building Redis, it is a good idea to test it using:

% make test

If TLS is built, running the tests with TLS enabled (you will need tcl-tls installed):

% ./utils/gen-test-certs.sh
% ./runtest --tls

Fixing build problems with dependencies or cached build options

Redis 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.

When you update the source code with git pull or when code inside the dependencies tree is modified in any other way, make sure to use the following command in order to really clean everything and rebuild from scratch:

% make distclean

This will clean: jemalloc, lua, hiredis, linenoise and other dependencies.

Also if you force certain build options like 32bit target, no C compiler optimizations (for debugging purposes), and other similar build time options, 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 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.

In case of build errors when trying to build a 32 bit binary of Redis, try the following steps:

  • Install the package libc6-dev-i386 (also try g++-multilib).
  • Try using the following command line instead of make 32bit: 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 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.

To force compiling against libc malloc, use:

% make MALLOC=libc

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 monotonic clock source. On most modern systems, the internal processor clock can be used to improve performance. Cautions can be found here: http://oliveryang.net/2015/09/pitfalls-of-TSC-usage/

To build with support for the processor's internal instruction clock, use:

% make CFLAGS="-DUSE_PROCESSOR_CLOCK"

Verbose build

Redis will build with a user-friendly colorized output by default. If you want to see a more verbose output, use the following:

% make V=1

Running Redis

To run Redis with the default configuration, just type:

% cd src
% ./redis-server

If you want to provide your redis.conf, you have to run it using an additional parameter (the path of the configuration file):

% cd src
% ./redis-server /path/to/redis.conf

It is possible to alter the Redis configuration by passing parameters directly as options using the command line. Examples:

% ./redis-server --port 9999 --replicaof 127.0.0.1 6379
% ./redis-server /etc/redis/6379.conf --loglevel debug

All the options in redis.conf are also supported as options using the command line, with exactly the same name.

Running Redis with TLS:

Please consult the TLS.md file for more information on how to use Redis with TLS.

Playing with Redis

You can use redis-cli to play with Redis. Start a redis-server instance, then in another terminal try the following:

% cd src
% ./redis-cli
redis> ping
PONG
redis> set foo bar
OK
redis> get foo
"bar"
redis> incr mycounter
(integer) 1
redis> incr mycounter
(integer) 2
redis>

You can find the list of all the available commands at https://redis.io/commands.

Installing Redis

In order to install Redis binaries into /usr/local/bin, just use:

% make install

You can use make PREFIX=/some/other/directory install if you wish to use a different destination.

make install will just install binaries in your system, but will not configure init scripts and configuration files in the appropriate place. This is not needed if you just want to play a bit with Redis, but if you are installing it the proper way for a production system, we have a script that does this for Ubuntu and Debian systems:

% cd utils
% ./install_server.sh

Note: install_server.sh will not work on Mac OSX; it is built for Linux only.

The script will ask you a few questions and will setup everything you need to run Redis properly as a background daemon that will start again on system reboots.

You'll be able to stop and start Redis using the script named /etc/init.d/redis_<portnumber>, for instance /etc/init.d/redis_6379.

Code contributions

By contributing code to the Redis project in any form, including sending a pull request via GitHub, a code fragment or patch via private email or public discussion groups, you agree to release your code under the terms of the Redis Software Grant and Contributor License Agreement. Redis software contains contributions to the original Redis core project, which are owned by their contributors and licensed under the 3BSD license. Any copy of that license in this repository applies only to those contributions. Redis releases all Redis project versions from 7.4.x and thereafter under the RSALv2/SSPL dual-license as described in the LICENSE.txt file included in the Redis source distribution.

Please see the CONTRIBUTING.md file in this source distribution for more information. For security bugs and vulnerabilities, please see SECURITY.md.

Redis Trademarks

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 of its trademarks but it has requirements that must be followed as described in its Trademark Guidelines available at: https://redis.com/legal/trademark-guidelines/.

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 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. 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 understand more. Moreover most of the code is heavily commented and easy to follow.

Source code layout

The Redis 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 directory.

Inside the root are the following important directories:

  • src: contains the Redis 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.

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, 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 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 structure is the same. Keep in mind that all the new developments and pull requests should be performed against the unstable branch.

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.

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.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.

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. In the past it was called redisClient, now just client. The structure has many fields, here we'll just show the main ones:

struct client {
    int fd;
    sds querybuf;
    int argc;
    robj **argv;
    redisDb *db;
    int flags;
    list *reply;
    // ... many other fields ...
    char buf[PROTO_REPLY_CHUNK_BYTES];
}

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.
  • 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 are described as robj structures. The following is the full robj structure, which defines a Redis object:

struct redisObject {
    unsigned type:4;
    unsigned encoding:4;
    unsigned lru:LRU_BITS; /* LRU time (relative to global lru_clock) or
                            * LFU data (least significant 8 bits frequency
                            * and most significant 16 bits access time). */
    int refcount;
    void *ptr;
};

Basically this structure can represent all the basic Redis data types like strings, lists, sets, sorted sets and so forth. The interesting thing is that it has a type field, so that it is possible to know what type a given object has, and a refcount, so that the same object can be referenced in multiple places without allocating it multiple times. Finally the ptr field points to the actual representation of the object, which might vary even for the same type, depending on the encoding used.

Redis objects are used extensively in the Redis internals, however in order to avoid the overhead of indirect accesses, recently in many places 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 is defined. The following are the most important steps in order to startup the Redis 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.
  • aeMain() starts the event loop which listens for new connections.

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:

  • 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.
  • performEvictions() is called when a new write command should be performed but Redis is out of memory according to the maxmemory directive.
  • The global variable redisCommandTable defines all the Redis commands, specifying the name of the command, the function implementing the command, the number of arguments required, and other properties of each command.

commands.c

This file is auto generated by utils/generate-command-code.py, the content is based on the JSON files in the src/commands folder. These are meant to be the single source of truth about the Redis commands, and all the metadata about them. These JSON files are not meant to be used by anyone directly, instead that metadata can be obtained via the COMMAND command.

networking.c

This file defines all the I/O functions with clients, masters and replicas (which in Redis 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.
  • writeToClient() transmits the data pending in the output buffers to the client and is called by the writable event handler sendReplyToClient().
  • readQueryFromClient() is the readable event handler and accumulates data read from the client into the query buffer.
  • processInputBuffer() is the entry point in order to parse the client query buffer according to the Redis protocol. Once commands are ready to be processed, it calls processCommand() which is defined inside server.c in order to actually execute the command.
  • freeClient() deallocates, disconnects and removes a client.

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() 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 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.

The implementation inside aof.c has additional functions in order to implement an API that allows commands to append new commands into the AOF file as clients execute them.

The call() function defined inside server.c is responsible for calling the functions that in turn will write the commands into the AOF.

db.c

Certain Redis commands operate on specific data types; others are general. Examples of generic commands are DEL and EXPIRE. They operate on keys and not on their values specifically. All those generic commands are defined inside db.c.

Moreover db.c implements an API in order to perform certain operations on the Redis dataset without directly accessing the internal data structures.

The most important functions inside db.c which are used in many command implementations are the following:

  • lookupKeyRead() and lookupKeyWrite() are used in order to get a pointer to the value associated to a given key, or NULL if the key does not exist.
  • dbAdd() and its higher level counterpart setKey() create a new key in a Redis database.
  • dbDelete() removes a key and its associated value.
  • emptyData() removes an entire single database or all the databases defined.

The rest of the file implements the generic commands exposed to the client.

object.c

The robj structure defining Redis objects was already described. Inside object.c there are all the functions that operate with Redis objects at a basic level, like functions to allocate new objects, handle the reference counting and so forth. Notable functions inside this file:

  • incrRefCount() and decrRefCount() are used in order to increment or decrement an object reference count. When it drops to 0 the object is finally freed.
  • createObject() allocates a new object. There are also specialized functions to allocate string objects having a specific content, like createStringObjectFromLongLong() and similar functions.

This file also implements the OBJECT command.

replication.c

This is one of the most complex files inside Redis, 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.

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: this way their data set will remain synchronized with the one in the master.

This file also implements both the SYNC and PSYNC commands that are used in order to perform the first synchronization between masters and replicas, or to continue the replication after a disconnection.

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.
  • 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.
  • eval.c - contains the eval implementation using script_lua.c to invoke the Lua code.

Other C files

  • t_hash.c, t_list.c, t_set.c, t_string.c, t_zset.c and t_stream.c contains the implementation of the Redis data types. They implement both an API to access a given data type, and the client command implementations for these data types.
  • ae.c implements the Redis event loop, it's a self contained library which is simple to read and understand.
  • 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.

Anatomy of a Redis command

All the Redis commands are defined in the following way:

void foobarCommand(client *c) {
    printf("%s",c->argv[1]->ptr); /* Do something with the argument. */
    addReply(c,shared.ok); /* Reply something to the client. */
}

The command function is referenced by a JSON file, together with its metadata, see commands.c described above for details. The command flags are documented in the comment above the struct redisCommand in server.h. For other details, please refer to the COMMAND command. https://redis.io/commands/command/

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 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 :-)

Enjoy!

redis's People

Contributors

0xtonyxia avatar antirez avatar artix75 avatar badboy avatar charleschen888 avatar charsyam avatar chenyang8094 avatar devnexen avatar dvirsky avatar enjoy-binbin avatar filipecosta90 avatar geoffgarside avatar guybe7 avatar hpatro avatar huangzhw avatar hwware avatar itamarhaber avatar madolson avatar mattsta avatar meirshpilraien avatar oranagra avatar pietern avatar shooterit avatar soloestoy avatar sundb avatar tezc avatar trevor211 avatar yoav-steinberg avatar yossigo avatar zuiderkwast avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

redis's Issues

Redis Cluster: set node in error mode if after a restart there are foreign keys.

Redis Cluster nodes save the cluster configuration ASAP in a file every time a configuration change is made to the cluster schema. It may happen, especially after a resharding state, that the cluster setup is modified as we moved keys from node A to node B, for instance migrating the hash slot 100. However if A or B will not persist on disk ASAP and there is a server restart, one of the servers can start with keys that belong to hash slots no longer associated with that node.

When this happens the node should start in error state. This way redis-trib can be executed to fix the condition moving all the keys from one node to another one (the node associated with the hash slot accordingly to the current configuration).

For the same reason in Redis cluster the best persistence to use is the AOF and not RDB based persistence.

There are other alternatives. For instance when a node starts, detecting "foreing keys" (keys not associated to the node, with the current node config), it can simply update the table to map this hash slots to itself. This way we'll have the same hash slot assigned to multiple nodes in the cluster. When nodes will start propagating this information via gossip, they can automatically setup a routing table where, of N nodes having the same hash slot associated, one is the real owner, setting the slot as IMPORTING, and all the other nodes will set the table as MIGRATING, in a cascade. Like, A, B, C detect having slot 1000 associated, so they configure things like:

A: migrating to B.
B: migrating to C.
C: importing from B.

The chain can be made univoque using the node IDs in a lexicographically way sorting from smaller to bigger.
Redis-trib can later detect this setup and fix it performing migration.

The improvement with the above system is that the cluster will recover when restarted.

Definitely there is to think more about this issues.

File name prn.h in jemalloc prevents fetch on Windows

In Windows files with names that look like a device (PRN, COM, etc.) cannot be written as files, even if they have an extension.
I cannot do a successfull fetch of this file. Renaming it to prnd.h (or something else) fixes this.

BRPOPLPUSH and a single LPUSH with several items doesn't work well together

Moving here from http://code.google.com/p/redis/issues/detail?id=656, thanks to @HampusW for the original report that follows:

This is in the unstable branch (but possibly in the other ones too). Try doing the following on a clean Redis instance (in this order):

  1. Client A blocks on 'BRPOPLPUSH a b 0'
  2. Client B runs 'LPUSH a data1 data2 data3'

Result: 'a' is empty and 'b' contains only "data1". AOF and replication shows the same.

The reason is that rpoplpushHandlePush calls rewriteClientCommandVector when the first item is pushed to 'a' and that interferes with the pushing of the other items. Even if pushGenericCommand was changed so that it would push all the items regardless (using a local copy of them), the replication and AOF would only register one of the items. This seems to require some minor redesign to fix it.

Everything works when BRPOPLPUSH isn't involved, of course.

Lua scripting: documentation needed

Scripting is now part of Redis unstable branch, but we still don't have any doc, but just a few blog posts that I did and that are no longer 100% in sync with the actual implementation. Would be cool to use the blog posts in order to obtain some initial documentation of the scripting feature of Redis, otherwise new users willing to try it are a bit too much "alone".

weird pipeline() combined with watch() behavior

I understand it's intended and documented, still it's seems so inconvenient, illogical and prohibits efficient use of pipelining (not MULTI).

Why pipe suddenly breaks when I call .watch()? It is still kind of pipe, but it doesn't work as one. Maybe, old 2.2 approach is not the best, but it didn't cause this confusion. I used .pipeline(transaction=false) to make pipeline and with a flag on to make atomic pipeline. Cluttering everything in on method is questionable but concept is clear. Now, we still use .pipeline() to get atomic pipeline until we don't use .watch() when we suddenly loose pipelining and using .multi() directly to get atomic, which makes things with MULTI very inconsistent.

My use case, implemented for redis-py 2.2:

# set up pipeline not for multi, but to avoid unneeded network roundtrips
pipe = redis_conn.pipeline(transaction=False)  
pipe.watch(version_key, *conjs_keys)
# get something from redis
pipe.get(...)
pipe.sunion(...)
...
# ignore watch() result, gather all other
_, result1, result2, ... = pipe.execute() 

if <some condition involving result*>:
    redis_conn.unwatch()
else:
    try:
        txn = redis_conn.pipeline()
        # Change something in redis
        txn.delete(...)
        txn.execute()
    except WatchError:
        <Optimistic locking failed: just redo everything.>

Actual working example is here
https://github.com/Suor/django-cacheops/blob/master/cacheops/invalidation.py#L111

Port redis-check-dump to RDB version 2

the RDB file format changed, now we need to port the tool to support the new DB version and the new data types related to specially encoded values. This must be done before 2.4 final.

some tip

i found when mset ,if one value contains some space , it is not replicated correctly. why?

Feature request: No write to disk option

It would be nice to have an option to tell redis do not write object to AOF.
For example i'm using redis for php session storage. I do not need this data to be persistent.

configurable umask for unix socket

The default umask (usually 022) can be overridden by the process starting Redis (e.g. to 000 to allow any user to connect to Redis over its Unix socket), but this also changes the permission mask for the log, snapshot and AOF. I think it would be good to add a configuration directive to explicitly set the mask for a unix socket, so a permissive unix socket doesn't necessarily impacts the permission settings on the other files Redis creates.

For example:

unixsocket /tmp/redis.sock
unixsocketperm 755 # As safe default, user can specify 777

This integer can be passed directly to chmod(2).

What do you think?

Cannot assign requested address

I'm using predis and from time to time I have these messages in php.err:

PHP Fatal error: Uncaught exception 'Predis_ClientException' with message
'Cannot assign requested address' in /home/meshok/predis.php:648
Stack trace:
#0 /home/meshok/predis.php(708): Predis_Connection->connect()
#1 /home/meshok/predis.php(680): Predis_Connection->getSocket()
#2 /home/meshok/predis.php(116):

Predis_Connection->writeCommand(Object(Predis_Commands_SetAdd))
#3 /home/meshok/predis.php(124):

Predis_Client->executeCommandInternal(Object(Predis_Connection),
Object(Predis_Commands_SetAdd))
#4 /home/meshok/predis.php(108):

Predis_Client->executeCommand(Object(Predis_Commands_SetAdd))
#5 [internal function]: Predis_Client->__call('sadd', Array)
#6 /home/meshok/bounce.php(55): Predis_Client->sadd('emails_550',

'storm@radioener...')

What version of the product are you using? On what operating system?
redis 1.2.2 32 bit on 64 bit debian

What can couse that

some way to subscribe to changes to keys

Hi, I apologize if this feature already exists, but I would like to request having a way to snoop changes to keys.

So a user would subscribe to a key, for example key foo. When anybody else performs a set command on foo, all subscribers get the new value of foo.

Please let me know if this is possible, this would be a killer feature for users like me.

Best,
Matt

Refactoring of the command table

This is just to make public the working I'm doing in the command table. For now we have entries like that:

    {"setrange",setrangeCommand,4,REDIS_CMD_DENYOOM,NULL,1,1,1},

flags are the fourth argument, and are pretty verbose ones. If we would add features to Redis such as read-only slaves or other features requiring us to flag commands as read-only, or operating on sets, and so forth, the command table would look like REDIS_CMD_BLABLA | REDIS_CMD_OTHER_FLAG | REDIS_CMD_LAST_FLAG ... and so forth.

Solution

To rewrite the command table to use single chars for flags, like in:

    {"setrange",setrangeCommand,4,"wsm",NULL,1,1,1},

Where:

  • "w" == write command, this command will alter the data set.
  • "s" == set command, this command operates on sets.
  • "m" == this command may use additional memory after a successful completion (that is, deny on out of memory)
    And so forth.

After the command table is loaded into memory this chars are turned into flags so that testing for the flags is as fast as it was previously and there are no other code changes required.

mem_fragmentation_ratio:4626.92

Hello,

I am using Redis 2.2.12 in a somewhat high traffic production environment and it's memory is getting fragmented severely after running for a few days. Since the fragmentation is so severe Redis ends up sing all the memory on the server without actually using it for anything.

I have a feeling the smaller pages are not unallocated by Redis because of performance considerations and if that's the case I would be happy to test with different limits if someone could provide a patch.

The output of redis-cli info is as follows:
redis_version:2.2.12
redis_git_sha1:00000000
redis_git_dirty:0
arch_bits:64
multiplexing_api:epoll
process_id:19546
uptime_in_seconds:329059
uptime_in_days:3
lru_clock:1678726
used_cpu_sys:6435.62
used_cpu_user:4461.22
used_cpu_sys_children:0.00
used_cpu_user_children:0.00
connected_clients:10
connected_slaves:0
client_longest_output_list:0
client_biggest_input_buf:0
blocked_clients:0
used_memory:1176888
used_memory_human:1.12M
used_memory_rss:5445365760
mem_fragmentation_ratio:4626.92
use_tcmalloc:0
loading:0
aof_enabled:0
changes_since_last_save:116570410
bgsave_in_progress:0
last_save_time:1316692443
bgrewriteaof_in_progress:0
total_connections_received:6778631
total_commands_processed:215392122
expired_keys:604514
evicted_keys:0
keyspace_hits:129413799
keyspace_misses:2008094
hash_max_zipmap_entries:64
hash_max_zipmap_value:512
pubsub_channels:0
pubsub_patterns:0
vm_enabled:0
role:master
db0:keys=110,expires=110

Thanks,
Bogdan

In memory Redis objects serialization

Redis objects serialization functions currently operate only on files, we have functions in rdb.c in order to save or load objects from/to files. However the DUMP and RESTORE commands needed for Redis Cluster bring the necessity to implement faster serialization on memory buffers, otherwise in order to deserialize we need to copy stuff on a file and call the old API, that is unacceptable.

Pieter Noordhuis did some good preliminary work on that issue, in its "unstable-rdb" branch.

This issue is about reviewing and merging this branch. This work is especially needed for Redis 3.0, but I'm adding it into the 2.6 milestone as well as since 2.6 will be derived from Redis unstable branch I want to avoid having two branches that diverged too much, so it's better to fork 2.6 after the current unstable branch already implements many important internal changes needed for the cluster support. However 2.6 will NOT include cluster support.

All the further considerations about the implementation will be moved into separated comments.

"pipe"ing functionality

I dont know if this is even remotely of interest or if it could integrate well but it's just an idea :) even just with the CLI it would be cool to have xargs-like functionality, for example something like this:

  redis> keys views* | del

NKEYS - returns number of keys matching regex

Like KEYS, but only returns count. Hopefully would be much faster than KEYS (and would certainly require less data transfer).

Also, would be super nice if both this and KEYS were optimized to give faster response for prefixes.

Thanks!

Marc

vm-swap-file doesn't work with relative path

vm-swap-file etc/redis.swap gives

[22313] 07 Jun 00:26:42 * Using 'etc/redis.swap' as swap file
[22313] 07 Jun 00:26:42 # Can't open the swap file: No such file or directory. Exiting.

Add DTrace support for Redis

I've been working on adding DTrace support for Redis.

DTrace offers dynamic tracing, so we can debug programs without the need to modify its source code. A design goal of the tool was to be able to debug production systems without affecting their performance. DTrace has been used in production at Sun since 2005 and it's considered "production safe".

Using Dynamic tracing one can be able to trace system calls, see which process is accessing which files, network operations made by programs and more.

More information about it here: http://dtrace.org/blogs/brendan/ and here: http://en.wikipedia.org/wiki/DTrace.

DTrace is supported in Solaris, Mac OS X, FreeBSD and NetBSD. There's a project trying to integrate it to Linux.

Besides from dynamic tracing DTrace offers static tracing. With static tracing we can add our own "probe providers" to our software and hook from there to perform our own tracing, statistics collection and more. MySQL for example fires dtrace probes when is parsing a query and some others: http://dev.mysql.com/tech-resources/articles/getting_started_dtrace_saha.html.

I think it will be nice to have a similar integration into Redis. So for example we could do analysis like this one: Redis Commands execution speed.

There I placed probes that are fired whenever a command is executed as you can see here: videlalvaro@73acc34:

The Dtrace script to produce such output is really simple:

#!/usr/sbin/dtrace -CZs
/*
 * Sample script to calculate the avg time spent inside commands
 * Run it like this: sudo dtrace -qs commands.d -p `pgrep redis`
 * Time output is in nanoseconds.
 */
redis$target:::command-entry
{
  self->ts = timestamp;
}

redis$target:::command-return
/self->ts/
{
  @time[copyinstr(arg0)] = avg(timestamp - self->ts);
  @timeq[copyinstr(arg0)] = quantize(timestamp - self->ts);
  self->ts = 0;
}

Defining probes points is very simple. For example to fire a probe when a command is called I added this in a file called reids_dtrace.d:

provider redis {
    probe command__entry(char *cmd);

    probe command__return(char *cmd);
};

We can have many more probes, is just a matter of specifying there with that simple syntax, for example we could add:

probe client__connected(host, port);

Or simply client__connected with no arguments.

If we run the command:

dtrace -h -s redis_dtrace.d -o redis_dtrace.h

DTrace will generate a header file that defines a couple of macros that is what we use in the code like this:

 int processCommand(redisClient *c) {
    ....

    if(REDIS_COMMAND_ENTRY_ENABLED()) {
       REDIS_COMMAND_ENTRY(c->cmd->name);
    }
    call(c);
    if(REDIS_COMMAND_RETURN_ENABLED()) {
        REDIS_COMMAND_RETURN(c->cmd->name);
    }
    ...
  }

So basically to add probes to Redis we need to define them in redis_dtrace.d and then use the macros wherever we need to fire such probes.

To add this to Redis we need to define useful probes and then add them to the source code at the proper places.

I think having DTrace support can help a lot of people running Redis in production. If we define what probes we want to have I think the job of adding them to Redis shouldn't be more than one work day. Please take a look at my branch to see what I have done so far: https://github.com/videlalvaro/redis/tree/dtrace.

Sorted sets can contain elements with a NaN score under unknown conditions (was: Redis crash)

After a period of sustained load my redis-server (running on 32 bit Ubuntu 11.04) is crashing. I can't pin down the exact command that is causing it but it's probably a combination of sadd, zadd and hset. It's whilst running a batch job and doesn't seem to occur at the same point in the job each time.

The best information I can provide is probably the log at the point of the crash:

[12859] 29 Sep 14:31:15 # ==> t_zset.c:474 'deleted != 0' is not true
[12859] 29 Sep 14:31:15 # (forcing SIGSEGV in order to print the stack trace)
[12859] 29 Sep 14:31:15 # ======= Ooops! Redis 2.2.11 got signal: -11- =======
[12859] 29 Sep 14:31:15 # redis_version:2.2.11
redis_git_sha1:00000000
redis_git_dirty:0
arch_bits:32
multiplexing_api:epoll
process_id:12859
uptime_in_seconds:45
uptime_in_days:0
lru_clock:1706883
used_cpu_sys:11.44
used_cpu_user:8.41
used_cpu_sys_childrens:0.00
used_cpu_user_childrens:0.00
connected_clients:11
connected_slaves:0
client_longest_output_list:0
client_biggest_input_buf:82
blocked_clients:1
used_memory:878230144
used_memory_human:837.55M
used_memory_rss:998580224
mem_fragmentation_ratio:1.14
use_tcmalloc:0
loading:0
aof_enabled:0
changes_since_last_save:270430
bgsave_in_progress:0
last_save_time:1317303030
bgrewriteaof_in_progress:0
total_connections_received:185
total_commands_processed:774739
expired_keys:0
evicted_keys:0
keyspace_hits:463811
keyspace_misses:49547
hash_max_zipmap_entries:64
hash_max_zipmap_value:512
pubsub_channels:0
pubsub_patterns:0
vm_enabled:0
role:master
allocation_stats:2=262652,4=296824,6=6,7=12432,8=464204,9=52822,10=612662,11=472858,12=17406017,13=1867

Redis Cluster: implement the ASKING command

(Please see http://groups.google.com/group/redis-db/browse_thread/thread/67d1b0bbe7669071 for full details, here I'm just summarizing the problem with some cut&paste from that thread).

If you are familiar with the design you know that the key space is split into 4096 parts.
Every part is called an "hash slot", and every node has a routing table to map every hash slot with a cluster node.
This way if a client sends a query to a node that is not responsible for the keys mentioned in the query, it gets a -MOVED message redirecting it to the right node. However we also have the ability to reconfigure the cluster while it
is running. So for instance I've hash slot 100 that is assigned to node A. And I want to move it to node B.

This is accomplished (and redis-trib is already able to do it for you automatically) with the following steps.

  1. Node A hash slot 100 is marked as "Migrating to B" (using the CLUSTER SETSLOT MIGRATING command).
  2. Node B hash slot 100 is marked as "Importing from A" (using the
    CLUSTER SETSLOT IMPORTING command).
  3. An external client, usually redis-trib, starts using the commands
    CLUSTER GETKEYSINSLOT and the MIGRATE command to atomically move keys from A to B.

What is interesting is that while the hash slot is set as "Migrating to B", node A will reply to all the requests about this hash slot of keys that are still present in the hash slot, but if a request is about a key that is in hash slot 100 but is not
found inside the key space, it generates a "-ASK" error, that is like "-MOVED" but means: please only ask this exact query to the specified node, but don't update your table about it. Ask new queries about hash
slot 100 to me again.

This way all the new keys about hash slot 100 are created directly in B, but A handles all the queries about keys that are still in A. At the same time redis-trib moves keys from A to B. Eventually all the keys are moved and the hash slot configuration is consolidated to the new one, using other CLUSTER SETSLOT subcommands.
So far this is pretty cool. But there is a subtle problem about this.

The Problem

When the cluster is stable, that is, there no resharding in progress, a client may ask a query to a random node.
There is only one node that will reply to queries related to a specific hash slot. All the other nodes will redirect the client to
this node. However when rehashing is in progress there are two nodes that will reply to queries for a given hash slot, that is, the MIGRATING node and the IMPORTING node.

If the client is a "smart" client with an internal routing table, it starts every connection to a cluster asking for the slot->node map, and makes sure to update the table when -MOVED messages are received. But there are also clients that are not smart, without a table, or even clients that are smart but perhaps don't update the table since a lot of time since they are idle, and the cluster moved a lot of hash slots recently. But to make things simpler let's just focus on the stupid client that has no internal map. It just send queries to a random node among a list of configured nodes, expecting to get
redirected if the wrong node was selected.

Such a simple client is only able to deal with -MOVED and -ASK redirections. And the two messages are handled in the same way, that is, just asking to the node specified in the redirection message. It is easy to see how this client may create a race condition, like that:

  1. We are migrating slot 100 from A to B.
  2. Node A will only accept queries about slot 100 that are related to keys already in the key space. Otherwise it will reply with -ASK.
  3. Node B instead will accept all queries about hash slot 100.
  4. Our stupid client need to perform an LPUSH against a key in hash slot 100. It picks a random client.
  5. If it picks "C" or "D" it will be redirected to "A". "A" in turn will redirect it to "B" with -ASK if the key is not present in A key space.
  6. If it picks "B" directly the query will be accepted by "B", but what about if "A" already had that key? RACE!

The proposed fix

  1. Node B is importing hash slot 100.
  2. Node B receives a query about key "foo" in hash slot 100. If it already hash "foo" the query is served. Otherwise it issues a "-MOVED" to redirect the client to A.
  3. Node B however will serve the query if the client started the chat using the command "ASKING", that indicates that this query was issued after being redirected by a -ASK message. If the client comes from a -ASK redirection we are sure we can serve the client.

So in the case above what happens is that all the smart clients will have no problems, after a -ASK redirection they will send:

ASKING
LPUSH foo bar

ASKING sets a flag that is cleared after the command is executed. If a client is dummy (no internal routing tables caching) but still is able to remember that after a -ASK redirection it should start the next query with ASKING, everything is fine as well.
A completely stupid client that is not able to start the chat with ASKING will simply ping/pong from A to B until the hash slot migration is completed, and will finally be served.

Add 'CLIENT NAME'

Hi,

in the 2.4 branch, there is a new command names 'CLIENT LIST' that allows us to introspect all the clients connected to our Redis server, and if need be, kill a mis-behaving client.

A useful improvement would be to add a CLIENT NAME subcommand, that would allow long-lived Redis clients to name themselves and make it easier to identify the proper connection.

A suggestion for naming clients could be "TYPE APP (PID)". Examples:

  • webapp my_app (pid 2323)
  • worker email_sender (pid 34223)

Thanks,

Return and remove elements from a sorted set

I would love be able to get a range and remove the elements from a sorted list at the same time.

ZREMRANGEBYSCORE myset 0 123 WITHVALUES

I currently do this by first getting then removing. But in between that operation another key could be registered and then lost forever without me being able to process it. So having built in support for this that is atomic would be very useful.

Use LuaJIT2 instead of original Lua interpreter

In the comments under discussion "Redis and scripting" (http://www.antirez.com/post/redis-and-scripting.html) there were a lot of suggestions of using LuaJIT2 instead of standart Lua interpreter, but no response from antirez about.

Why LuaJIT2 is good for Redis?

  • API-compatible with Lua 5.1, so you shouldn't face any problems due to replacing
  • Much faster than original interpreter
  • Uses less memory
  • Have built-in Lua BitOp (I've seen It's planned to built it in by default into Redis)
  • Works everyehere Redis does (see http://luajit.org/install.html)

You'll probably say that the speedup is insignificant, because the time spent in the interpreter much less than time spent to i/o and commands processing, but I disagree. If I'll be able to process command at least 5% faster, It will make me much happier.

So, why don't you use LuaJIT2?

replication for help

when i start a redis replication with one master and two slaves,i find that mset command cannot be sync well. eg.with redis-py >>>dda='{"contact": {"mobile": 134023424, "tel": 2384242}, "name": "tom"}' >>>ddb='{"contact": {"mobile": 139763224, "tel": 6534242}, "name": "john"}' >>>master.mset({'dda':dda,'ddb':ddb}) >>>slave.get('dda') >>>'{"contact": {"mobile": 134023424, "tel": 2384242}, "name": "tom"}' >>>slave.get('ddb') >>>'{"contact":'

so mset can not be sync fully. why and some solutions?

Redis should abort when can't load the RDB file

When Redis can't load an RDB file since the RDB version is wrong or when the file signature does not match a log is emitted and the server starts without loading data, with an empty key space. It should instead abort after logging the error.
This was not intentional, and is an error introduced later, but never found since it is not common to have mismatching RDB versions or file signature.

make loglevel configurable via CONFIG SET

There is no reason that we can't change the loglevel at runtime, and it is also very useful since the default log level tends to be higher than what most people want to run in production (but it is good for a first contact with Redis).

Suboptimal Lua representation of Redis errors

Current drafts of the Redis scripting branch represent errors as {err = MESSAGE}. Considering that there are very few situations where an error is expected to be returned from Redis, directly raising a Lua error would in most cases be a better solution. Reasons for this include:

  • Circumstances in which Redis returns error replies (unsupported operations on a data type, using the wrong number or type of arguments to a command, overflowing integers and strings, and protocol syntax errors) are remarkably similar to situations in which Lua's standard libraries raise errors.
  • In most cases, a Redis error represents something that would cause the rest of the script to fail regardless. Since there are few conditions where an error reply is to be expected, it makes more sense to bail out by default and allow the programmer to suppress the error when it is expected with pcall.
  • The original stated motivation for the current error format was "[Y]ou should be able to call a Redis command without even caring about what he returned, save the reply into a var, and return it later as the user directly called Redis." Redis errors occur too rarely for this to be a common use case, and at any rate raising a Lua error will return the fact that an error occurred to the user immediately.
  • Related to the above, Redis scripting is expected to be mainly used for creating commands that manipulate or at least introspect data. If a person is expecting a multi-bulk reply, receives an error, but does not check to see whether or not it is an error, he could return confusing data to the user or make misguided modifications to the database.

That said, the {err = MESSAGE} representation could be useful if an error was returned as a multi-bulk reply. (It would be relatively simple to special-case this in the code that translates Redis responses to Lua types.) But in most cases it makes more sense to have Redis errors map directly to Lua errors.

Too many blocked clients

I have attached info command output of my production machine here. I can see there connected_clients:1007 and 374 blocked clients. Can you tell me what do these parameters mean? Does this slow down performance of redis?When such situation happens?

I am using jedis client. So please suggest me what should be the size of my threadpool to have good response for any redis command from redis server.

redis_git_sha1:00000000
redis_git_dirty:0
arch_bits:64
multiplexing_api:epoll
process_id:31204
uptime_in_seconds:38194
uptime_in_days:0
lru_clock:1595299
used_cpu_sys:12868.18
used_cpu_user:21275.57
used_cpu_sys_children:1463.77
used_cpu_user_children:240.77
connected_clients:1007
connected_slaves:0
client_longest_output_list:0
client_biggest_input_buf:0
blocked_clients:374
used_memory:11402770136
used_memory_human:10.62G
used_memory_rss:14575374336
mem_fragmentation_ratio:1.28
use_tcmalloc:0
loading:0
aof_enabled:1
changes_since_last_save:745283119
bgsave_in_progress:0
last_save_time:1316149044
bgrewriteaof_in_progress:0
total_connections_received:3533019
total_commands_processed:1778497491
expired_keys:1
evicted_keys:0
keyspace_hits:1859577302
keyspace_misses:143859847
hash_max_zipmap_entries:512
hash_max_zipmap_value:64
pubsub_channels:0
pubsub_patterns:0
vm_enabled:0
role:master
db0:keys=30185929,expires=0

Thank you.
Best regards.
Sonali

MHGETALL command to get multiple fields and values from multiple hashes

Hello,

I think it can save network traffic between client and server and make queries convenient and faster. My use case:

HMSET user:1 name User1
HMSET user:2 name User2
HMSET user:3 name User3

And now get all three users with custom delmiter between hashes (inevitably?):

MHGETALL #delimiter# user:1 user:2 user:3

  1. name
  2. User1
  3. #delimiter#
  4. name
  5. User2
  6. #delimiter#
  7. name
  8. User3

Best regards

Anton

test suite: offset port number for every worker to avoid EADDRINUSE

The aof-race integration test fails once in a while with redis-server that can't start because the port is already in use. This can be fixed by giving every test worker a different port offset (probably with some room to spare -- e.g. difference of 10 between workers -- because some tests spawn multiple Redis processes).

SORT sortedset BY score

I just found out that executing this:

SORT sortedset BY nosort

Actually doesn't return the values in the order they are in sorted set. Could we extend SORT command to support this:

SORT sortedset BY score

Or is there another way to archive this without extra keys?

Even when maxmemory is reached Redis should allow MULTI/EXEC blocks containing only commands allowed on out of memory.

Currently we have this strange behavior:

OK
redis 127.0.0.1:6379> config set maxmemory 10
OK
redis 127.0.0.1:6379> set foo2 bar2
(error) ERR command not allowed when used memory > 'maxmemory'
redis 127.0.0.1:6379> multi
OK
redis 127.0.0.1:6379> get foo
QUEUED
redis 127.0.0.1:6379> exec
(error) ERR command not allowed when used memory > 'maxmemory'
redis 127.0.0.1:6379> 

As you can see Redis is preventing us from running MULTI/EXEC even if the block only contains read-only commands.
This bug should be fixed preventing the MULTI/EXEC block execution only if the memory limit is reached AND there is at least a command flagged as REDIS_CMD_DENYOOM.

I'm fixing this issue in the unstable-ctable branch.

Feature - Implement a Fibonacci Command

The calculation of the Fibonacci value for a number is a serious issue. I request as a feature the command FIB.

Use of the command

redis-cli > FIB 3
5

redis-cli > FIB 10
55

Thanks in advance!

Redis silently continues working when RDB persistence fails

If you have RDB persistence configured to write your data, for instance, into a directory that is not writable by Redis, the background saving will not work but Redis will continue working as expected. This way the user may think everything is ok but actually no persistence at all is performed by the server: when the server goes down no data wil be available at all.

Either Redis should abort when this happens or should return an error to every client trying to write against the server if Redis was not able to persist the latest time it tried. The error will no longer be reported once a successful BGSAVE/SAVE is performed. Or something like that...

Error while compiling Redis (latest trunk)

gildo:redis/ (master) $ make [9:49:16]
cc -c -march=k8 -O2 -pipe -g -rdynamic -ggdb adlist.c
cc -c -march=k8 -O2 -pipe -g -rdynamic -ggdb ae.c
cc -c -march=k8 -O2 -pipe -g -rdynamic -ggdb anet.c
cc -c -march=k8 -O2 -pipe -g -rdynamic -ggdb dict.c
cc -c -march=k8 -O2 -pipe -g -rdynamic -ggdb redis.c
cc -c -march=k8 -O2 -pipe -g -rdynamic -ggdb sds.c
cc -c -march=k8 -O2 -pipe -g -rdynamic -ggdb zmalloc.c
cc -c -march=k8 -O2 -pipe -g -rdynamic -ggdb lzf_c.c
cc -c -march=k8 -O2 -pipe -g -rdynamic -ggdb lzf_d.c
cc -c -march=k8 -O2 -pipe -g -rdynamic -ggdb pqsort.c
cc -c -march=k8 -O2 -pipe -g -rdynamic -ggdb zipmap.c
cc -o redis-server -march=k8 -O2 -pipe -lm -pthread -g -rdynamic -ggdb adlist.o ae.o anet.o dict.o redis.o sds.o zmalloc.o lzf_c.o lzf_d.o pqsort.o zipmap.o
redis.o: In function rdbSaveDoubleValue': /home/gildo/pub/redis/redis.c:3146: undefined reference toisfinite'
collect2: ld returned 1 exit status
make: *** [redis-server] Error 1
gildo:redis/ (master) $ [9:49:19]

Redis should close the connection when the client output buffer is too big

I'm marking this as non critical as we rarely seen problems about that, but actually in theory this is a critical bug.

What happens is that if for an error a Redis client (especially a Pub/Sub client, or a slave) is not albe to consume the output produced by the server fast enough, the output buffer for that client will grow more and more at the point that could crash the server for out of memory.

After a given limit is reached we should simply close the connection?

Should Pub/Sub handle this in a different way sending warning messages to the client when we are near to the limit?

Additional points: Also close slave (and monitors) connections if the output buffer gets too big.

Plan

  • Add max-client-output-buffer <class> <hard-limit-bytes> <soft-limit-bytes> <seconds>

The semantic is:

  1. Close the connection if the client stays over the soft-limit for the specified amount of seconds.
  2. Close the connection ASAP once the client reaches the hard-limit.

The class argument is used to tell Redis what clients are affected by the limit, and can be:

  • pubsub (Clients subscribed to one or more Pub Sub channels)
  • slave (Slave instances or clients running the MONITOR command)
  • standard (Normal clients)

It will be possible to use the max-client-output-buffer statement multiple times to configure the limits for the three different classes of clients.

DECRLATER

Would be very nice to DECR or INCR at some later time, perhaps as a command option:
DECR MYCOUNTER 1 300
to decrement MYCOUNTER by 1 , 300 seconds from when the command is issued.

Alternatively
DECRLATER MYCOUNTER 1 300

I've prototyped this in the client end (rb-redis, eventmachine), but I think that would limit my scale and if a client died, would lose track.

ZREM does not work, but zrem does ...

when i try to use ZREM i get
Redis.new.ZREM 'y', 'x'
Errno::EAGAIN: Resource temporarily unavailable - Timeout reading from the socket
from /usr/local/lib/ruby/gems/1.8/gems/redis-1.0.4/lib/redis/client.rb:469:in read_reply' from /usr/local/lib/ruby/gems/1.8/gems/redis-1.0.4/lib/redis/client.rb:444:inprocess_command'
from /usr/local/lib/ruby/gems/1.8/gems/redis-1.0.4/lib/redis/client.rb:442:in map' from /usr/local/lib/ruby/gems/1.8/gems/redis-1.0.4/lib/redis/client.rb:442:inprocess_command'
from /usr/local/lib/ruby/gems/1.8/gems/redis-1.0.4/lib/redis/client.rb:431:in raw_call_command' from /usr/local/lib/ruby/gems/1.8/gems/redis-1.0.4/lib/redis/client.rb:452:incall'
from /usr/local/lib/ruby/gems/1.8/gems/redis-1.0.4/lib/redis/client.rb:452:in maybe_lock' from /usr/local/lib/ruby/gems/1.8/gems/redis-1.0.4/lib/redis/client.rb:428:inraw_call_command'
from /usr/local/lib/ruby/gems/1.8/gems/redis-1.0.4/lib/redis/client.rb:332:in call_command' from /usr/local/lib/ruby/gems/1.8/gems/redis-1.0.4/lib/redis/client.rb:381:inmethod_missing'

same call with zrem works...

Dump specially encoded types as binary blobs in AOF rewrites

Small aggregate values such as lists and sets use an internal representation that is already serialized and arch-independent. However when we rewrite the append only file using the BGREWRITEAOF command we deserialize this values into many single-value operations. For instance a three elements list will be rendered inside the rewritten AOF as:
RPUSH mylist value1
RPUSH mylist value2
RPUSH mylist value3
This is a lot of wasted work both for the AOF creation and for the AOF reloading.

Instead we should put serialized values into the AOF in the direct format using some special command, possibly only valid in the context of AOF. This will severely improve the AOF performance.

This technique is already used in the creation of RDB files.

An alternative to what proposed above would be to change format to AOF allowing for an initial section of the AOF in the form of an RDB file itself, so that we could just dump the RDB and a sequence of accumulated commands when doing the AOF rewrite operation. This is a valid alternative but with the side effect of not having an easy to process AOF.

There is definitely to think more about this but is an interesting optimization.

Lua scripting: determinism, replication, AOF

The problem

Redis scripting (currently only available in the unstable branch) replicates the script itself instead of replicating the single commands, both to slaves and to the AOF file. This is needed as often scripts are one or two order of magnitudes faster than executing commands in a normal way, so for a slave to be able to cope with the master replication link speed and number of commands per second this is the only solution available.

The solution works great in general, and Redis is also able to replicate EVALSHA commands as EVAL commands taking an hash table of original scripts. However there is a problem related to determinism.

All the non pure Redis commands such as SPOP and RANDOMKEY, together with all the non pure Lua functions like pseudo random number generation, commands related to the system time, and so forth, will have the effect of creating scripts that are not just functions of the input parameters and the data set content. Thus the slave may end with a data set different than the one of the master.

Possible solutions

Solution 1: The simplest solution to this problem is denying calls to Redis and Lua non pure functions, however this restricts what you can do with the scripting support. For instance calling RANDOMKEY from a script is not a problem as long as the script does not modify the Redis data set in any way.

Solution 2: A better solution could be to permitting the use of non pure functions only as long as no write is performed against the dataset. If a write is performed after a non pure function is called, an error is raised.

Solution 3: Another alternative would be to revert to replicating single commands every time a non pure function is called, but this requires the user to be too much aware of the internals, and may lead to the creation of applications where the master can cope with the load but the slave can't cope in any way, or where the time needed to write to the AOF file is the bottle neck.

The solution 2, that is currently my preferred, could be improved implementing a version of Lua pseudo random number generation function that has the seed reset to 0 at every new script executed, generating always the same sequence, unless you don't pass a random seed as one of the EVAL arguments. This way yo can create random functions that are replicated in a correct way. Let's call this Solution 2b.

I'm going to implement solution 2 with the "2b" feature, but this bug is needed in order to make evident what the design story was, and in the hope to get better ideas for the future.

redis-server segfault in addReply

Redis segfaults in addReply function when I run redis-benchmark against it and connect two slaves simultaneously. The cause seems to be a typo in the dupClientReplyValue function:

--- redis-1.2.1-orig/redis.c    2010-02-18 12:40:23.000000000 +0100
+++ redis-1.2.1/redis.c    2010-02-18 12:42:03.000000000 +0100
@@ -1975,7 +1975,7 @@

 static void *dupClientReplyValue(void *o) {
     incrRefCount((robj*)o);
-    return 0;
+    return o;
 }

It returned a NULL pointer that was used afterwards as a list*. The patched version ran for 24+ hours with slaves and redis-benchmark reconnecting periodically.

The patch was made for 1.2.1 but the above function is the same in 1.2.2.

EXPIRE, SETEX, should detect time overflows

Moving this issue here from Google Code -> http://code.google.com/p/redis/issues/detail?id=660

How to trigger the bug (cut & paste from google code):

pie@reactor ~ % irb
irb(main):001:0> Time.now.to_i + 1000
=> 1315722345

pie@reactor ~ % redis-cli -p 5011
redis 127.0.0.1:5011> setex foo 1315722345 1
OK
redis 127.0.0.1:5011> get foo
(nil)
redis 127.0.0.1:5011> setex foo 1000 1
OK
redis 127.0.0.1:5011> get foo
"1"

Using redis 2.2.12 on freebsd 8.2. This is a 32-bit system.

Could You make redis support File Queue Data Structure, so it can support huge message storage ;

dear antirez:
Can you make redis support file queue Data Structure?
command like it :
qpush queue_name value
qpushs queue_name value1 value2 value3

qpop queue_name
qpopn queue_name num

Data Structure like this:
push --> head[memory] [file1[n] file2[n] file3[n] fileN[n]][disk] tail[memory] --> pop
n:message number;

Queue is base module at distributed system on pipelining process task ใ€‚
[email protected]
thank you very much.

Lua sandboxing

There is to strip away many commands from the current Lua scripting support, like commands dealign with time (non determinism issues), commands accessing the filesystem/os, and so forth.

I know there are mixed feelings inside the community about restricting the Lua environment to just perform "computational" tasks in the context of Redis scripts, without a winder access to the system, but this seems like the best pick for now. It is always possible to open more, but going into the other direction is hard. Also I feel like that scripting with big interactions with the system are not a good idea in general...

Redis Cluster: propagate PUBLISH messages across all the cluster

The simplest way to implement the Cluster version of our Pub/Sub system is just to propagate all the messages using the Redis Cluster inter-node bus. This is semantically coherent with our fire and forget approach, and even if this appears to do more work then needed, actually PUBLISH is extremely cheap on Redis if there are no client subscribed, so we can surely start without any map to avoid publishing against nodes that have no subscribers for a given channel/pattern.

This way we can have a good cluster solution that can scale many consumers with just a few lines of code. We can later improve this implementation if required.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.