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

Redis · Module

GETRANGE

Returns a substring of the Redis string stored at a key, using inclusive zero-based start and end offsets. Negative offsets count backward from the string's end.

Since
2.4.0

How it works

GETRANGE reads part of the string stored at a key. The start and end offsets are zero-based and inclusive; negative offsets count backward from the string's end, and bounds outside the value are clipped to its actual length.

Syntax

GETRANGE key start end

Parameters

NameTypeDescription
keykeyThe name of the key.
startintegerThe zero-based starting offset; negative values count from the string's end.
endintegerThe zero-based inclusive ending offset; negative values count from the string's end.

Returns

Bulk string reply containing the substring selected by the inclusive start and end offsets.

Key points

  • GETRANGE extracts a portion of a Redis string without modifying the stored value.
  • The required key argument identifies the string to read.
  • start begins at character position zero for the first character.
  • end identifies the final included position and uses zero-based indexing.
  • Negative values allow suffix-oriented reads without first determining the string length.
  • The result is returned as a bulk string containing the selected data.
  • The command takes exactly one key and two integer offsets.

Common mistakes

  • The end index is included, so requesting 0 through 3 returns four characters when all positions exist.
  • Indexes beyond the string's boundaries are clipped rather than causing an error.

Example: Read string substrings

import { createClient } from 'redis';

const client = createClient();
await client.connect();

await client.set('mykey', 'This is a string');
console.log(await client.getRange('mykey', 0, 3));
console.log(await client.getRange('mykey', -3, -1));
console.log(await client.getRange('mykey', 0, -1));

await client.quit();

Stores a string, then retrieves an inclusive prefix, suffix, and the complete value using zero-based and negative offsets.

When to use it

Partial string reads

Read a fixed-width prefix or suffix from a stored string without retrieving the entire value.

Suffix inspection

Use negative offsets to inspect the tail of a string, such as its final characters.

Offset-based data access

Retrieve bounded sections of serialized or packed string data using known offsets.

Interview questions

QWhat does GETRANGE do and what is its syntax?

GETRANGE returns a substring from the string value at a key. Its syntax is GETRANGE key start end, and both offsets are zero-based and inclusive.

Follow-up: How does it behave with negative indexes? Negative offsets count backward from the end: -1 is the last character and -2 is the penultimate.

QHow do negative offsets work?

A negative start or end counts from the string's end. For example, -3 -1 selects the final three characters.

Follow-up: What happens if the requested range exceeds the value? Redis limits the range to the actual string length.

QWhat does GETRANGE return and how does its cost depend on the request?

The command returns a bulk string reply containing the selected substring. If the key does not contain a string value, the command is not applicable to that data type.

Follow-up: What is the performance characteristic? The work is O(N), where N is the length of the returned substring.

  • GET
  • SETRANGE
  • GETBIT
  • SETBIT

Source: GETRANGE | Docs. Checked 2026-10-16.