Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

API reference

Redis API reference

Redis caching and data structures

Identifiers
141
With full guides
59
Types
4

Topics covered

  • Strings & TTL
  • Hashes & lists
  • Sets & sorted sets
  • Pub/Sub
  • Caching patterns
  • Rate limiting
  • Streams
  • Persistence

Function

NameWhat it does
createClientcreateClient creates a node-redis client configured for Redis, defaulting to localhost on port 6379. Supply options such as a connection URL for another server, then connect explicitly before using commands.

Const

NameWhat it does
StringConst
HashConst
ListConst
SetConst
Sorted SetConst
StreamConst
BitmapConst
HyperLogLogConst
GeospatialConst
Bloom filterConst

Module

NameWhat it does
GETSETGETSET atomically replaces a key's string value and returns the value previously stored there. It is deprecated since Redis 6.2.0; new code should use SET with GET.
GETDELGETDEL returns a string value and deletes its key in the same command. It returns nil when the key is missing or does not contain a string.
MSETMSET atomically assigns string values to one or more keys in a single command. Existing values are overwritten, and a successful command returns OK.
MGETMGET atomically retrieves the string values of one or more keys in a single command. Missing keys and keys holding non-string values return nil in their corresponding result positions.
DECRDecrements the integer value stored at a key by one and returns the updated value. If the key is missing, Redis starts from zero.
INCRBYINCRBY adds an integer increment to a key's integer value and returns the updated value. Missing keys begin at zero, while wrong types, non-integer strings, and out-of-range values cause errors.
DECRBYDECRBY subtracts an integer amount from a key's stored integer value and returns the updated value. Missing keys start at zero, while non-integer values and out-of-range results cause errors.
INCRBYFLOATAdds a floating-point number to the numeric string stored at a Redis key and returns the updated value as a string. Missing keys start at zero, and negative increments decrement the value.
APPENDAPPEND adds a string to the end of a key's existing string value, creating the key if it is missing. It returns the resulting string length.
STRLENReturns the length of the string value stored at a key. Missing keys return 0, while keys holding non-string values cause an error.
SETNXSets a string value only when a key does not already exist. It returns 1 when the key is created and 0 when no write occurs; SET with NX is the recommended replacement for new code.
SETRANGEOverwrites part of a Redis string from a specified zero-based offset. It pads with zero bytes when necessary and returns the string's resulting length.
GETRANGEReturns a substring of the Redis string stored at a key, using inclusive zero-based start and end offsets. Negative offsets count backward from the string's end.
EXISTSEXISTS determines whether one or more Redis keys exist and returns the number found. It can check multiple keys in one command.
PEXPIRESets a key's expiration timeout in milliseconds. Optional NX, XX, GT, and LT modes conditionally control whether the timeout is applied.
PTTLReturns the remaining expiration time of a key in milliseconds. It distinguishes an existing key without expiration (-1) from a missing key (-2).
TYPETYPE reports the Redis data type stored at a key. It returns none when the key does not exist.
RANDOMKEYModule
TOUCHTOUCH updates the last-access time of each supplied key that exists and returns how many supplied keys were found. It does not alter key values.
UNLINKUNLINK asynchronously deletes one or more Redis keys. It removes keys from the keyspace promptly while reclaiming their memory in a background thread.
DUMPReturns a Redis-specific serialized representation of the value stored at a key. The result can be used with RESTORE, but does not include expiration information.
RESTORERESTORE creates a key by deserializing a serialized value produced by DUMP. It can apply a relative or absolute expiration and optionally replace an existing key or initialize eviction metadata.
COPYCopies the value of one Redis key to another key. It can target a different logical database and optionally replace an existing destination.
HGETReturns the value of one field in a Redis hash. If the field or its hash key does not exist, Redis returns nil/null.
HMGETReturns the values of multiple fields from a Redis hash, preserving field order and representing missing fields with nil.
HEXISTSChecks whether a field exists in a Redis hash. It returns 1 for an existing field and 0 when the field or hash key is absent.
HKEYSHKEYS returns all field names in the hash stored at a key. It returns an empty list when the key does not exist and has O(N) complexity based on the hash size.
HVALSReturns all values stored in a Redis hash. Use it when field names are unnecessary; the result order is not guaranteed.
HLENHLEN returns the number of fields in a Redis hash. It runs in O(1) time and returns 0 when the key does not exist.
HINCRBYHINCRBY adds a signed integer to a field in a Redis hash and returns the updated value. Missing hashes and fields are created automatically, with a missing field starting at zero.
HSCANHSCAN incrementally iterates through the fields of a Redis hash. It supports glob matching, a COUNT hint, and NOVALUES for returning field names without values.
HRANDFIELDReturns one or more randomly selected fields from a hash. Positive counts select distinct fields up to the hash size; negative counts allow repeats, and WITHVALUES also returns the selected values.
RPOPModule
LLENModule
LINDEXModule
LSETModule
LINSERTModule
LREMModule
LTRIMModule
BLPOPModule
BRPOPModule
LMOVEModule
LPOSModule
SADDModule
SREMModule
SISMEMBERModule
SCARDModule
SPOPModule
SRANDMEMBERModule
SUNIONModule
SINTERModule
SDIFFModule
SSCANModule
SMISMEMBERModule
ZREMModule
ZRANGEBYSCOREModule
ZREVRANGEModule
ZSCOREModule
ZRANKModule
ZREVRANKModule
ZCARDModule
ZCOUNTModule
ZINCRBYModule
ZPOPMINModule
ZPOPMAXModule
PSUBSCRIBEModule
UNSUBSCRIBEModule
XADDModule
XREADModule
XRANGEModule
XLENModule
XGROUPModule
XREADGROUPModule
XACKModule
XTRIMModule
EXECModule
DISCARDModule
WATCHModule
UNWATCHModule
EVALModule
EVALSHAModule
SCRIPTModule
FUNCTIONModule
FCALLModule
PINGModule
INFOModule
CONFIGModule
DBSIZEModule
FLUSHALLModule
CLIENTModule
COMMANDModule
MEMORYModule
GEOADDModule
GEOSEARCHModule
SETBITModule
GETBITModule
BITCOUNTModule
PFADDModule
PFCOUNTModule
PFMERGEModule
AUTHModule
SELECTModule
HELLOModule

Command

NameWhat it does
GETGET returns the string value stored at a key. It returns nil for a missing key and errors when the key contains a non-string value.
SETSets a key to a string, creating it or replacing its current value. Conditions, expiration controls, and GET support conditional writes, lifetimes, and retrieval of the previous string.
HSETCreates or updates one or more fields in a Redis hash. It returns how many fields were newly added, excluding fields whose existing values were overwritten.
LPUSHLPUSH prepends one or more elements to a Redis list, creating the list when the key is absent. It returns the list's length after insertion.
ZADDAdds members with scores to a sorted set, updating existing members and creating the key when needed. Options can restrict updates, count changed entries, or increment one member's score.
EXPIRESets a relative expiration timeout, in seconds, on a key. When the timeout elapses, Redis automatically deletes the key.
SUBSCRIBESubscribes the client to one or more exact channel names. Redis then pushes matching published messages and subscription confirmations through that connection.
MULTIMULTI starts a Redis transaction and queues later commands on the current connection. EXEC runs the queued commands atomically, while DISCARD cancels them.
INCRIncrements the integer stored at a key by one. A missing key starts at zero, so the first call returns one.
DELDEL removes one or more specified keys and ignores names that do not exist. It returns the count of keys successfully deleted.
HGETALLReturns every field and value in the hash stored at a key. The operation is O(N), where N is the number of fields.
LRANGEReturns an inclusive range of elements from a Redis list. Start and stop use zero-based indexes, and negative indexes count from the list's tail.
SMEMBERSReturns every member of the set stored at a key. The result contains the set's complete, unordered membership.
ZRANGEReturns members from a sorted set selected by rank, score, or lexicographical range. Optional reverse ordering, pagination, and score output support ranking, filtering, and windowed reads.
TTLReturns a key's remaining expiration time in seconds. It distinguishes missing keys from existing keys that have no expiration.
KEYSReturns every key name matching a glob-style pattern. Because it can block the Redis server while producing the complete result, reserve it for debugging and exceptional administrative tasks.
SCANSCAN incrementally iterates over key names in the currently selected Redis database. Continue with each returned cursor until it becomes 0, optionally filtering by pattern, count hint, or type.
PUBLISHPUBLISH sends a message to a channel for delivery to its current subscribers. It returns the number of clients that received the message.
FLUSHDBFLUSHDB removes all keys from the currently selected Redis database. Use ASYNC or SYNC to choose asynchronous or synchronous flushing.
GETEXReturns a string value while optionally setting, changing, or removing the key's expiration. GETEX supports relative and absolute expiration forms and is a write command.
SETEXSETEX stores a string at a key and gives it a timeout in seconds. It is deprecated in favor of SET with the EX option.
HDELHDEL deletes one or more fields from a Redis hash. It ignores missing fields, removes the hash when no fields remain, and returns the number of fields actually deleted.
LPOPRemoves and returns elements from the beginning of a Redis list. It removes one element by default or up to count elements when count is supplied.
RPUSHAppends one or more values to the tail of a Redis list, creating the key when absent. It returns the list's new length.
EXPIREATSets a key's expiration to an absolute Unix timestamp in seconds. Optional NX, XX, GT, or LT conditions control when the deadline may be changed.
PERSISTRemoves the existing expiration from a key, making it persistent. Returns 1 when a timeout was removed, otherwise 0.
RENAMERenames a key to a new name, replacing the destination if it already exists. In Redis Cluster, both names must belong to the same hash slot.

Search across every package and filter by type in your dashboard.

Start learning