DEV Community

Hrittik Bhattacharjee for

Posted on

Redis Commands Cheat Sheet Part 1 - Keys and Strings

Redis Icon

Following are some of the commonly used Redis commands and data structures, use this list as a quick cheat sheet or a ready reference for your day-to-day Redis usage as needed.πŸ˜„

This is Part 1 of this series, containing commands pertaining to Keys and Strings in Redis.


# SET: store a value for a given key name
# NX or XX allows us to check for "only if does not exist" or "only if exists" while setting, respectively
# PX is used to set TTL for a key in milliseconds
# EX is used to set TTL for a key in seconds
> SET key value [NX | XX] [EX seconds | PX milliseconds | EXAT unix-time-seconds | PXAT unix-time-milliseconds | KEEPTTL]
> SET customer:1000 fred

# options of the SET command:
#   EX seconds -- Set the specified expire time, in seconds.
#   PX milliseconds -- Set the specified expire time, in milliseconds.
#   EXAT timestamp-seconds -- Set the specified Unix time at which the key will expire, in seconds.
#   PXAT timestamp-milliseconds -- Set the specified Unix time at which the key will expire, in milliseconds.
#   NX -- Only set the key if it does not already exist.
#   XX -- Only set the key if it already exist.

# GET: return a value for a given key name
> GET key
> GET customer:1000

# keys and scan: get a list of existing key names, or to iterate over keys that match a pattern
# keys:
#   - blocks until complete
#   - never use in production
#   - useful for debugging
# scan:
#   - also blocks but returns only a handful of keys at a time, 
#   - then returns a slot reference (or, cursor) that can then be used to continue more iterations
#   - safe for production
> KEYS customer:1*

# returns customer:1000, customer 1500 etc. cus they match the pattern 1*

> SCAN cursor [MATCH pattern] [COUNT count] [TYPE type]
> SCAN 0 MATCH customer:1* COUNT 1000
# may or may not return results
# but returns the next slot reference to be used in the next command

# e.g. if it returns the slot reference 14848, then the next SCAN command will be
> SCAN 14848 MATCH customer:1* COUNT 1000

# COUNT is optional, it may be used to return more keys per call, but the more the COUNT, the longer it will block

# removing keys
# DEL: 
#   - remove the key and the memory associated with the key
#   - blocking operation
#   - the key will no longer exist
#   - the memory associated with the key value is claimed by an async process, so non-blocking
> DEL key [key ...]
> UNLINK key [key ...]
> UNLINK customer:1000

# returns the number of keys removed

# when SET is used on a key name that does not exist, the key is automatically created
# however, to set the key value only if the key exysts, use EXISTS
> EXISTS key [key ...]
> EXISTS customer:1000

# if the same existing key is mentioned in the arguments multiple times, it will be counted multiple times. 
# So if somekey exists, EXISTS somekey somekey will return 2.

# setting TTL for a key
> EXPIRE key seconds
> PEXPIRE key milliseconds
> EXPIREAT key timestamp
> PEXPIREAT key milliseconds-timestamp

# viewing TTL for a key
# in seconds
> TTL key
# in milliseconds
> PTTL key

# removing TTL for a key

# PX or EX can be used to set TTL for key at the time of creation

# TTL of a "persisted" key is -1

# SCAN basic usage example
# SCAN is a cursor based iterator
# this means that at every call of the command, the server returns an updated cursor that the user needs to use as the cursor argument in the next call
# an iteration starts when the cursor is set to 0, and terminates when the cursor returned by the server is 0
# commands `SCAN, SSCAN, HSCAN and ZSCAN` all work very similarly
# the default `COUNT` value is 10
> scan 0
1) "17"
2)  1) "key:12"
    2) "key:8"
    3) "key:4"
    4) "key:14"
    5) "key:16"
    6) "key:17"
    7) "key:15"
    8) "key:10"
    9) "key:3"
    10) "key:7"
    11) "key:1"
> scan 17
1) "0"
2)  1) "key:5"
    2) "key:18"
    3) "key:0"
    4) "key:2"
    5) "key:19"
    6) "key:13"
    7) "key:6"
    8) "key:9"
    9) "key:11"
Enter fullscreen mode Exit fullscreen mode


# SET can be used to create a string
> SET key value
> SET user:101:time-zone UTC-8

# sets the value "UTC-8" for the key user:101:time-zone

# GET can be used to retrieve a string
> GET key
> GET user:101:time-zone

# JSON responses can be serealized as a string and stored (cached) as a value for a key
# when caching data it is common to set an expiry time using the `EX` option for seconds
# after expiry, the key will be deleted along with its value
> SET key:1000 '{ "somehing": 1000 }' EX 7200

# check remaining TTL for a key/string using the TTL command
> TTL key:1000

# integer manipulation commands:
# using INCR and INCRBY commands can be used to increment a key's value by 1 or a specified number
# if key does not exist, it will be created and incremented appropriately
> INCR key
> INCRBY key increment

# if the number is negative, value will be decremented

# there is also the DECR and DECRBY command
> DECRBY key decrement

# for floating point incremet
> INCRBYFLOAT key increment

# what if the value of a key is a string?
> SET key:1 1000
> GET key:1
> DECRBY key:1 1
(integer) 999
> GET key:1
> TYPE key:1

# so the DECRBY command casted the string value to integer and performed numerical operation on it
# but the value still remains a string

# the encoding of the value cn be checked using the OBJECT ENCODING command

# so in the example above, the value is a string, but what is stored withing the string datatype is an integer value

# redis supports polymorphism
> SET key:1 "hello"
"embstr" # i.e. a text value

> DECRBY key:1 10
(error) ERR # since the encoded vaue is not an integer anymore
Enter fullscreen mode Exit fullscreen mode

Hope you found this useful! Save the post for reference, Parts 2 and 3 will be out soon!

Do follow we're working on some pretty cool stuff and have exciting content for you in the works.😊

Top comments (0)