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

Redis · Module

STRLEN

Returns the length of the string value stored at a key. Missing keys return 0, while keys holding non-string values cause an error.

Since
2.2.0

How it works

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.

Syntax

STRLEN key

Parameters

NameTypeDescription
keykeyThe name of the key containing the string to measure.

Returns

Integer reply: the length of the string stored at key, or 0 when the key does not exist.

Key points

  • STRLEN reports the character/byte length of the string value associated with the supplied key.
  • The result is an integer, and an absent key is represented by zero rather than an error.
  • Its only required argument is the key name.
  • For the documented Hello world example, STRLEN returns 11.

Common mistakes

  • The key must contain a Redis string; using STRLEN on another data type produces an error.

Example: Measure a Redis string

redis-cli SET mykey "Hello world"
redis-cli STRLEN mykey
# (integer) 11
redis-cli STRLEN missing-key
# (integer) 0

Stores a string, measures its length, then demonstrates the zero result for an absent key.

When to use it

Check string size

Validate or inspect stored text before processing it.

Inspect key contents

Distinguish an empty string from a missing key when combined with existence checks.

Operational diagnostics

Measure a Redis string in scripts, monitoring commands, or command-line workflows.

Interview questions

QWhat does STRLEN return for an existing string and for a missing key?

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.

QWhat is the time complexity of STRLEN?

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.

QWhat is the command signature and required argument?

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.

  • APPEND
  • SET
  • GET
  • JSON.STRLEN

Source: STRLEN. Checked 2026-10-16.