Strings are the most basic Redis data type. They can contain any kind of data: text, integers, serialized objects, or binary data up to 512 MB.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/redis/redis/llms.txt
Use this file to discover all available pages before exploring further.
GET
Returns the string value of a key.Syntax
The key to retrieve.
The value of the key, or
nil if the key does not exist.Examples
SET
Sets the string value of a key, ignoring its type. Creates the key if it doesn’t exist.Syntax
The key to set.
The value to set.
Only set the key if it does not already exist.
Only set the key if it already exists.
Only set if current value equals the provided value.
Only set if current value does not equal the provided value.
Return the old value stored at key, or nil if key did not exist.
Set expiration time in seconds.
Set expiration time in milliseconds.
Set expiration as Unix timestamp in seconds.
Set expiration as Unix timestamp in milliseconds.
Retain the TTL associated with the key.
OKif SET was executed correctlynilif condition was not met or GET option returned nil- Previous value if GET option was used
- 2.6.12: Added
EX,PX,NXandXXoptions. - 6.0.0: Added the
KEEPTTLoption. - 6.2.0: Added the
GET,EXATandPXAToptions. - 7.0.0: Allowed the
NXandGEToptions to be used together. - 8.4.0: Added
IFEQ,IFNE,IFDEQ,IFDNEoptions.
Examples
INCR
Increments the integer value of a key by one.Syntax
The key to increment. Must hold a string that can be represented as integer.
The value of the key after incrementing.
Examples
DECR
Decrements the integer value of a key by one.Syntax
The key to decrement. Must hold a string that can be represented as integer.
The value of the key after decrementing.
Examples
APPEND
Appends a value to a key.Syntax
The key to append to. Created if it doesn’t exist.
The value to append.
The length of the string after the append operation.
Examples
GETRANGE
Returns a substring of the string stored at a key.Syntax
The key containing the string.
The starting offset (0-based). Negative values count from the end.
The ending offset (inclusive). Negative values count from the end.
The substring within the specified range.
Examples
Related Commands
Additional String Commands
- GETSET: Set value and return old value (deprecated, use SET with GET)
- SETEX: Set with expiration (deprecated, use SET with EX)
- SETNX: Set if not exists (deprecated, use SET with NX)
- MGET: Get multiple keys
- MSET: Set multiple keys
- MSETNX: Set multiple keys only if none exist
- INCRBY: Increment by integer
- INCRBYFLOAT: Increment by float
- DECRBY: Decrement by integer
- STRLEN: Get string length
- SETRANGE: Overwrite part of string
- GETDEL: Get and delete key
- GETEX: Get with expiration option
Use Cases
Counters
Strings are perfect for counters using INCR/DECR:Caching
Store serialized objects with expiration:Rate Limiting
Combine with expiration for rate limiting:Session Storage
Best Practices
Atomic Operations
- Use INCR/DECR for atomic counter updates
- Use SET NX for distributed locks
- Use SET XX GET for atomic swap
Memory Efficiency
- Use integers when possible (more efficient than strings)
- Compress large strings before storing
- Set appropriate expiration times
- Use APPEND for incremental updates
Performance
- Batch operations with MGET/MSET
- Use pipelining for multiple operations
- Avoid very large strings (> 1 MB)
- Use GETRANGE for partial reads