Redis Cheatsheet
101 essential methods for coding interviews
Showing 101 of 101 methods
| Method | Syntax | Description | Time | Priority |
|---|---|---|---|---|
SET | SET key value [EX seconds] [NX|XX] | Set a key-value pair with optional expiration and conditions | O(1) | essential |
GET | GET key | Retrieve the value of a key | O(1) | essential |
INCR / INCRBY | INCR key | INCRBY key increment | Atomically increment a numeric value | O(1) | essential |
LPUSH / RPUSH | LPUSH key value [value ...] | RPUSH key value [value ...] | Push elements to the head (L) or tail (R) of a list | O(1) per element | essential |
LPOP / RPOP | LPOP key [count] | RPOP key [count] | Remove and return elements from list head or tail | O(1) | essential |
LRANGE | LRANGE key start stop | Get a range of elements from a list | O(s+n) where s is start offset and n is range | essential |
BLPOP / BRPOP | BLPOP key [key ...] timeout | BRPOP key [key ...] timeout | Blocking pop from list head or tail, waits until element available | O(1) | essential |
HSET / HGET | HSET key field value [field value ...] | HGET key field | Set or get hash field values | O(1) | essential |
HGETALL | HGETALL key | Get all fields and values of a hash | O(n) | essential |
SADD / SMEMBERS | SADD key member [member ...] | SMEMBERS key | Add members to a set or get all members | O(1) for SADD, O(n) for SMEMBERS | essential |
SISMEMBER | SISMEMBER key member | Check if a member exists in a set | O(1) | essential |
ZADD | ZADD key [NX|XX] score member [score member ...] | Add members with scores to a sorted set | O(log n) | essential |
ZRANGE / ZREVRANGE | ZRANGE key start stop [WITHSCORES] | ZREVRANGE key start stop [WITHSCORES] | Get members by rank (ZREVRANGE for high-to-low) | O(log n + m) where m is result size | essential |
ZSCORE / ZRANK | ZSCORE key member | ZRANK key member | Get score or rank of a member | O(1) for ZSCORE, O(log n) for ZRANK | essential |
DEL / EXISTS | DEL key [key ...] | EXISTS key [key ...] | Delete keys or check existence | O(1) per key | essential |
EXPIRE / TTL | EXPIRE key seconds | TTL key | Set key expiration or check remaining time | O(1) | essential |
XADD | XADD key [MAXLEN ~ count] * field value [field value ...] | Append an entry to a stream | O(1) | essential |
XREAD | XREAD [COUNT count] [BLOCK milliseconds] STREAMS key [key ...] id [id ...] | Read entries from one or more streams | O(n) | essential |
DECR / DECRBY | DECR key | DECRBY key decrement | Atomically decrement a numeric value | O(1) | common |
MSET / MGET | MSET k1 v1 k2 v2 | MGET k1 k2 | Set or get multiple keys atomically | O(n) | common |
LTRIM | LTRIM key start stop | Trim list to specified range, removing elements outside range | O(n) where n is elements removed | common |
LMOVE | LMOVE source destination LEFT|RIGHT LEFT|RIGHT | Atomically pop from one list and push to another | O(1) | common |
LLEN | LLEN key | Get the length of a list | O(1) | common |
HINCRBY | HINCRBY key field increment | Atomically increment a hash field | O(1) | common |
HMGET | HMGET key field [field ...] | Get values of multiple hash fields | O(n) where n is number of fields | common |
HKEYS / HVALS | HKEYS key | HVALS key | Get all field names or all values from a hash | O(n) | common |
HDEL | HDEL key field [field ...] | Delete one or more hash fields | O(n) where n is number of fields | common |
HSCAN | HSCAN key cursor [MATCH pattern] [COUNT count] | Incrementally iterate hash fields and values | O(1) per call, O(n) total | common |
SUNION / SINTER / SDIFF | SUNION key1 key2 | SINTER key1 key2 | SDIFF key1 key2 | Set operations: union, intersection, difference | O(n*m) where n and m are set sizes | common |
SREM | SREM key member [member ...] | Remove one or more members from a set | O(n) where n is members to remove | common |
SPOP / SRANDMEMBER | SPOP key [count] | SRANDMEMBER key [count] | Remove and return random member(s) or just return without removing | O(1) for single, O(n) for count | common |
SCARD | SCARD key | Get the number of members in a set (cardinality) | O(1) | common |
SSCAN | SSCAN key cursor [MATCH pattern] [COUNT count] | Incrementally iterate set members | O(1) per call, O(n) total | common |
ZINCRBY | ZINCRBY key increment member | Increment the score of a sorted set member | O(log n) | common |
ZCOUNT | ZCOUNT key min max | Count members with scores in given range | O(log n) | common |
ZRANGEBYSCORE | ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count] | Get members by score range | O(log n + m) | common |
ZPOPMIN / ZPOPMAX | ZPOPMIN key [count] | ZPOPMAX key [count] | Remove and return members with lowest/highest scores | O(log n * m) | common |
ZREM | ZREM key member [member ...] | Remove one or more members from a sorted set | O(m*log n) | common |
ZCARD | ZCARD key | Get the number of members in a sorted set | O(1) | common |
ZSCAN | ZSCAN key cursor [MATCH pattern] [COUNT count] | Incrementally iterate sorted set members and scores | O(1) per call, O(n) total | common |
KEYS / SCAN | KEYS pattern | SCAN cursor [MATCH pattern] [COUNT count] | Find keys by pattern (SCAN is non-blocking) | O(n) for KEYS, O(1) per SCAN call | common |
UNLINK | UNLINK key [key ...] | Delete keys asynchronously in background thread | O(1) for command, O(n) async for reclaim | common |
MULTI / EXEC | MULTI ... commands ... EXEC | Execute commands atomically as a transaction | O(n) where n is number of commands | common |
WATCH / UNWATCH | WATCH key [key ...] | UNWATCH | Watch keys for changes, abort transaction if modified | O(1) | common |
PUBLISH / SUBSCRIBE | PUBLISH channel message | SUBSCRIBE channel [channel ...] | Publish messages to channels or subscribe to receive them | O(n+m) where n is clients and m is patterns | common |
PFADD | PFADD key element [element ...] | Add elements to HyperLogLog for cardinality estimation | O(1) | common |
PFCOUNT | PFCOUNT key [key ...] | Get approximate cardinality from HyperLogLog(s) | O(1) single key, O(n) multiple keys | common |
XRANGE / XREVRANGE | XRANGE key start end [COUNT count] | XREVRANGE key end start [COUNT count] | Read entries in ID range (forward or reverse) | O(n) | common |
XLEN | XLEN key | Get the number of entries in a stream | O(1) | common |
XGROUP CREATE | XGROUP CREATE key groupname id [MKSTREAM] | Create a consumer group for a stream | O(1) | common |
XREADGROUP | XREADGROUP GROUP group consumer [COUNT n] [BLOCK ms] STREAMS key [key ...] id [id ...] | Read from stream as a consumer in a group | O(n) | common |
XACK | XACK key group id [id ...] | Acknowledge message processing in consumer group | O(1) | common |
GEOADD | GEOADD key [NX|XX] longitude latitude member [longitude latitude member ...] | Add geospatial items (longitude, latitude, name) to a sorted set | O(log n) | common |
GEODIST | GEODIST key member1 member2 [m|km|ft|mi] | Calculate distance between two geospatial members | O(1) | common |
GEOSEARCH | GEOSEARCH key FROMMEMBER member|FROMLONLAT lon lat BYRADIUS dist unit|BYBOX w h unit [ASC|DESC] [COUNT n] | Search for members within radius or box (replaces GEORADIUS) | O(n+log m) | common |
SETBIT | SETBIT key offset value | Set or clear a bit at offset in string value | O(1) | common |
GETBIT | GETBIT key offset | Get bit value at offset | O(1) | common |
BITCOUNT | BITCOUNT key [start end [BYTE|BIT]] | Count set bits (1s) in a string | O(n) | common |
BITOP | BITOP AND|OR|XOR|NOT destkey key [key ...] | Perform bitwise operations between strings | O(n) | common |
EVAL | EVAL script numkeys key [key ...] arg [arg ...] | Execute a Lua script server-side | Depends on script | common |
EVALSHA | EVALSHA sha1 numkeys key [key ...] arg [arg ...] | Execute cached Lua script by SHA1 hash | Depends on script | common |
INFO | INFO [section] | Get server information and statistics | O(1) | common |
SLOWLOG | SLOWLOG GET [count] | SLOWLOG LEN | SLOWLOG RESET | Access the slow queries log | O(n) | common |
GETEX | GETEX key [EX seconds | PX milliseconds | EXAT timestamp | PXAT timestamp | PERSIST] | Get value and optionally set or remove expiration in one atomic operation | O(1) | useful |
GETDEL | GETDEL key | Get the value and delete the key atomically | O(1) | useful |
SETRANGE | SETRANGE key offset value | Overwrite part of a string at specified offset | O(1) if not increasing length, O(n) otherwise | useful |
APPEND | APPEND key value | Append value to existing string or create new | O(1) amortized | useful |
STRLEN | STRLEN key | Get the length of a string value | O(1) | useful |
LPOS | LPOS key element [RANK rank] [COUNT count] [MAXLEN len] | Find position of element in list | O(n) | useful |
LINSERT | LINSERT key BEFORE|AFTER pivot element | Insert element before or after a pivot value | O(n) to find pivot | useful |
HSETNX | HSETNX key field value | Set hash field only if it does not exist | O(1) | useful |
HEXISTS | HEXISTS key field | Check if a hash field exists | O(1) | useful |
HLEN | HLEN key | Get the number of fields in a hash | O(1) | useful |
SMOVE | SMOVE source destination member | Atomically move a member from one set to another | O(1) | useful |
SINTERSTORE / SUNIONSTORE / SDIFFSTORE | SINTERSTORE dest key1 key2 | SUNIONSTORE dest key1 key2 | SDIFFSTORE dest key1 key2 | Store result of set operations in a destination key | O(n*m) | useful |
BZPOPMIN / BZPOPMAX | BZPOPMIN key [key ...] timeout | BZPOPMAX key [key ...] timeout | Blocking pop of lowest/highest score member | O(log n) | useful |
ZUNIONSTORE / ZINTERSTORE | ZUNIONSTORE dest numkeys key [key ...] [WEIGHTS w1 w2] [AGGREGATE SUM|MIN|MAX] | Store union or intersection of sorted sets with score aggregation | O(n*log n) | useful |
ZRANGEBYLEX / ZLEXCOUNT | ZRANGEBYLEX key min max [LIMIT offset count] | ZLEXCOUNT key min max | Query sorted set lexicographically (when all scores are equal) | O(log n + m) | useful |
ZMSCORE | ZMSCORE key member [member ...] | Get scores of multiple members in one call | O(n) | useful |
COPY | COPY source destination [DB db] [REPLACE] | Copy a key to another key | O(n) for collections | useful |
DUMP / RESTORE | DUMP key | RESTORE key ttl serialized-value [REPLACE] | Serialize key to binary format or restore from serialized form | O(n) | useful |
OBJECT ENCODING | OBJECT ENCODING key | Get the internal encoding of a key value | O(1) | useful |
TOUCH | TOUCH key [key ...] | Update last access time of keys for LRU eviction | O(n) | useful |
SCAN with TYPE | SCAN cursor [MATCH pattern] [COUNT count] [TYPE type] | Scan keys filtered by data type | O(1) per call | useful |
RANDOMKEY | RANDOMKEY | Return a random key from the database | O(1) | useful |
DBSIZE | DBSIZE | Return the number of keys in the current database | O(1) | useful |
DISCARD | DISCARD | Abort a transaction, discarding all queued commands | O(n) | useful |
PFMERGE | PFMERGE destkey sourcekey [sourcekey ...] | Merge multiple HyperLogLogs into one | O(n) | useful |
XPENDING | XPENDING key group [start end count] [consumer] | View pending (unacknowledged) messages in a consumer group | O(n) | useful |
XTRIM | XTRIM key MAXLEN|MINID [= | ~] threshold | Trim stream to maximum length or minimum ID | O(n) where n is entries removed | useful |
XINFO | XINFO STREAM key | XINFO GROUPS key | XINFO CONSUMERS key groupname | Get stream, consumer group, or consumer info | O(1) to O(n) | useful |
GEOPOS | GEOPOS key member [member ...] | Get longitude and latitude of members | O(n) | useful |
GEOHASH | GEOHASH key member [member ...] | Get geohash strings for members | O(n) | useful |
BITPOS | BITPOS key bit [start [end [BYTE|BIT]]] | Find first bit set to 0 or 1 | O(n) | useful |
BITFIELD | BITFIELD key [GET type offset] [SET type offset value] [INCRBY type offset increment] | Treat string as array of arbitrary bit-width integers | O(1) | useful |
SCRIPT LOAD | SCRIPT LOAD script | Load script into cache and return SHA1 hash | O(n) where n is script length | useful |
SCRIPT EXISTS | SCRIPT EXISTS sha1 [sha1 ...] | Check if scripts are cached | O(n) | useful |
SCRIPT FLUSH | SCRIPT FLUSH [ASYNC|SYNC] | Clear all cached scripts | O(n) | useful |
CLIENT LIST | CLIENT LIST [TYPE normal|master|replica|pubsub] | Get list of connected clients | O(n) | useful |
CONFIG GET / SET | CONFIG GET pattern | CONFIG SET parameter value | Read or modify server configuration at runtime | O(n) | useful |
DEBUG SLEEP | DEBUG SLEEP seconds | Block the server for specified seconds (testing only) | O(1) | useful |