My favorites | Sign in
Logo
                
Search
for
Updated Dec 04 (5 days ago) by antirez
CommandReference  

Redis Command Reference

Every command name links to a specific wiki page describing the behavior of the command.

Connection handling

  • QUIT close the connection
  • AUTH simple password authentication if enabled

Commands operating on all the kind of values

  • EXISTS key test if a key exists
  • DEL key delete a key
  • TYPE key return the type of the value stored at key
  • KEYS pattern return all the keys matching a given pattern
  • RANDOMKEY return a random key from the key space
  • RENAME oldname newname rename the old key in the new one, destroing the newname key if it already exists
  • RENAMENX oldname newname rename the old key in the new one, if the newname key does not already exist
  • DBSIZE return the number of keys in the current db
  • EXPIRE set a time to live in seconds on a key
  • TTL get the time to live in seconds of a key
  • SELECT index Select the DB having the specified index
  • MOVE key dbindex Move the key from the currently selected DB to the DB having as index dbindex
  • FLUSHDB Remove all the keys of the currently selected DB
  • FLUSHALL Remove all the keys from all the databases

Commands operating on string values

  • SET key value set a key to a string value
  • GET key return the string value of the key
  • GETSET key value set a key to a string returning the old value of the key
  • MGET key1 key2 ... keyN multi-get, return the strings values of the keys
  • SETNX key value set a key to a string value if the key does not exist
  • MSET key1 value1 key2 value2 ... keyN valueN set a multiple keys to multiple values in a single atomic operation
  • MSETNX key1 value1 key2 value2 ... keyN valueN set a multiple keys to multiple values in a single atomic operation if none of the keys already exist
  • INCR key increment the integer value of key
  • INCRBY key integer increment the integer value of key by integer
  • DECR key decrement the integer value of key
  • DECRBY key integer decrement the integer value of key by integer

Commands operating on lists

  • RPUSH key value Append an element to the tail of the List value at key
  • LPUSH key value Append an element to the head of the List value at key
  • LLEN key Return the length of the List value at key
  • LRANGE key start end Return a range of elements from the List at key
  • LTRIM key start end Trim the list at key to the specified range of elements
  • LINDEX key index Return the element at index position from the List at key
  • LSET key index value Set a new value as the element at index position of the List at key
  • LREM key count value Remove the first-N, last-N, or all the elements matching value from the List at key
  • LPOP key Return and remove (atomically) the first element of the List at key
  • RPOP key Return and remove (atomically) the last element of the List at key
  • RPOPLPUSH srckey dstkey Return and remove (atomically) the last element of the source List stored at _srckey_ and push the same element to the destination List stored at _dstkey_

Commands operating on sets

  • SADD key member Add the specified member to the Set value at key
  • SREM key member Remove the specified member from the Set value at key
  • SPOP key Remove and return (pop) a random element from the Set value at key
  • SMOVE srckey dstkey member Move the specified member from one Set to another atomically
  • SCARD key Return the number of elements (the cardinality) of the Set at key
  • SISMEMBER key member Test if the specified value is a member of the Set at key
  • SINTER key1 key2 ... keyN Return the intersection between the Sets stored at key1, key2, ..., keyN
  • SINTERSTORE dstkey key1 key2 ... keyN Compute the intersection between the Sets stored at key1, key2, ..., keyN, and store the resulting Set at dstkey
  • SUNION key1 key2 ... keyN Return the union between the Sets stored at key1, key2, ..., keyN
  • SUNIONSTORE dstkey key1 key2 ... keyN Compute the union between the Sets stored at key1, key2, ..., keyN, and store the resulting Set at dstkey
  • SDIFF key1 key2 ... keyN Return the difference between the Set stored at key1 and all the Sets key2, ..., keyN
  • SDIFFSTORE dstkey key1 key2 ... keyN Compute the difference between the Set key1 and all the Sets key2, ..., keyN, and store the resulting Set at dstkey
  • SMEMBERS key Return all the members of the Set value at key
  • SRANDMEMBER key Return a random member of the Set value at key

Commands operating on sorted sets (zsets, Redis version >= 1.1)

  • ZADD key score member Add the specified member to the Set value at key or update the score if it already exist
  • ZREM key member Remove the specified member from the Set value at key
  • ZRANGE key start end Return a range of elements from the sorted set at key
  • ZREVRANGE key start end Return a range of elements from the sorted set at key, exactly like ZRANGE, but the sorted set is ordered in traversed in reverse order, from the greatest to the smallest score
  • ZRANGEBYSCORE key min max Return all the elements with score >= min and score <= max (a range query) from the sorted set
  • ZCARD key Return the cardinality (number of elements) of the sorted set at key
  • ZSCORE key element Return the score associated with the specified element of the sorted set at key
  • ZREMRANGEBYSCORE key min max Remove all the elements with score >= min and score <= max from the sorted set

Sorting

  • SORT key BY pattern LIMIT start end GET pattern ASC|DESC ALPHA Sort a Set or a List accordingly to the specified parameters

Persistence control commands

  • SAVE Synchronously save the DB on disk
  • BGSAVE Asynchronously save the DB on disk
  • LASTSAVE Return the UNIX time stamp of the last successfully saving of the dataset on disk
  • SHUTDOWN Synchronously save the DB on disk, then shutdown the server

Remote server control commands

  • INFO Provide information and statistics about the server
  • MONITOR Dump all the received requests in real time
  • SLAVEOF Change the replication settings

Comment by bddemir, Mar 22, 2009

I believe second INCR before DECRBY under section Commands operating on string values should be DECR actually. A minor copy-pasto.

Comment by antirez, Apr 21, 2009

@bddemir thanks fixed

Comment by occmailbox, Apr 29, 2009

The Lists and Sets commands are fantastic!

Got one question: is there a lock command like "ACQUIRELOCK db1", "RELEASELOCK db1"?

This is very important for multiple data manipulations and every programming language and database supports it,

java got "synchronized" syntax, python got "thread.allocate_lock().acquire()", even php which does not support thread got "sem_acquire()".

In my project, we are using 2 languages, java and php, we used the mysql's "LOCK TABLES" instead the languages' locks.

If Redis got a support for this, or you have a good alternative way to do this. I would try Redis in my next project.

Thanks for the good work!

Comment by occmailbox, Apr 29, 2009

The "EXISTS" command cannot do what LOCK can do. Sometimes 2 different threads may call the "EXISTS" command at the same time, both returned the result "not exists" then both call the command "SET" to store the value, that's what exactly the php example "Retwis" register.php did (register.php used "GET" command but it's the same). I know that's only a example, and that can be fixed using php's own "sem_acquire()", I know, but now you are doing the cloud things, you cannot use a single language's lock on a single machine. Right? I suggest Redis bring 2 different level locks: the database lock and the key lock.

Comment by occmailbox, Apr 30, 2009

Now I figured out a way, use "SETNX" to acquire a lock, then after the manipulation, clear the lock. Is it the best way?

Comment by antirez, May 01, 2009

@occmailbox: Hello! The idea is that you can mount locking free algorithms on top of the atomic operation Redis provides. This is most of the times the best way to do things. Otherwise you can implement locking using SETNX or RENAMENX or other atomic primitives. Please post your specific problem on the Redis google group for more information. Almost all the times there is a locking free way to do stuff. Btw after Redis 1.0-stable release I plan to add locking primitives (that are not about keys or the whole DB but just about "tokens", so what a given token is really locking is up to your application). Ciao, Salvatore.

Comment by tobutaz, May 01, 2009

The best way to do lock-free updates would be to have an equivalent to memcache CAS, a check and set operation. The client gets the old value and a cookie (the cookie can be the old value itself, or some last-modified timestamp or a version number). Then they compute the new value, and ask the server to perform a check and set. Either the server sets the value, or it has changed and the server returns a status code indicating no change was done. In which case, the client retries, starting at the GET stage.

Synopsis: CAS KEY NEW_VALUE COOKIE

Sets key to new value if it wasn't modified since COOKIE.

Comment by wong.edwin, Jul 24, 2009

I think there is a typo in the section SUNION. I believe the sentence ending "then this command produces the same result as SELEMENTS" should actually read "then this command produces the same result as SMEMBERS".

Comment by shein.artemiy, Jul 29, 2009

I don't understand where is LDEL command or something similar?

Comment by zze...@163.com, Sep 16, 2009

I think it should be better if add a EXPIRE command to DB, to let the whole db cache data expired

Comment by zze...@163.com, Sep 16, 2009

to support Map in the future?

Comment by birukoff, Sep 28, 2009

I don't see any practical benefit of having SCARD and LLEN as separate commands. Proposal: similar to SORT, which works for both sets and lists, unite them in one (for example, LEN).

Comment by acharnock, Oct 19, 2009

I echo Shein's question, where is LDEL command or similar?

Comment by antirez, Oct 20, 2009

@acharnock: it's not needed as DEL is already a vararg. DEL x y z ...

Comment by nova77, Oct 22, 2009

Is there a plan to support the memcache protocol? This one looks pretty close to me.

Comment by antirez, Oct 22, 2009

Hello nova77: no plans to support the memcached protocol. Redis has tons of features more, and a number of client libraries already implemented for many languages.

Comment by Bulkan, Oct 24, 2009

Is the command list a reflection of what commands are available in the development version or stable released version ?

Comment by antirez, Nov 13, 2009

@Bulkan: in every man page of commands requiring Redis > 1.0 it's specified what version is needed.

Comment by alexgenaud, Dec 05 (4 days ago)

For completeness, shouldn't PING be added (NB: Shouldn't JRedis return a boolean?)


Sign in to add a comment
Hosted by Google Code