Check string size
Validate or inspect stored text before processing it.
Redis · Module
Returns the length of the string value stored at a key. Missing keys return 0, while keys holding non-string values cause an error.
STRLEN reads the string stored at a key and returns its length as an integer. If the key is absent, it returns 0; if the key contains another Redis data type, the command returns an error.
STRLEN key| Name | Type | Description |
|---|---|---|
key | key | The name of the key containing the string to measure. |
Integer reply: the length of the string stored at key, or 0 when the key does not exist.
Hello world example, STRLEN returns 11.redis-cli SET mykey "Hello world"
redis-cli STRLEN mykey
# (integer) 11
redis-cli STRLEN missing-key
# (integer) 0Stores a string, measures its length, then demonstrates the zero result for an absent key.
Validate or inspect stored text before processing it.
Distinguish an empty string from a missing key when combined with existence checks.
Measure a Redis string in scripts, monitoring commands, or command-line workflows.
STRLEN takes one key and returns an integer reply containing the length of the string stored there. A missing key produces 0.
Follow-up: What happens if the key stores a list? Redis returns an error because STRLEN only accepts string values.
Its documented complexity is O(1), so retrieving the length does not require scanning the string contents.
Follow-up: How would you measure a JSON string instead? Use JSON.STRLEN with a key and optional path.
The command syntax is STRLEN key. The required argument is the name of the key whose string value should be measured.
Follow-up: What related command appends data while working with a string key? APPEND adds a value and creates the key if it does not exist.
APPENDSETGETJSON.STRLEN