Databases 4 min read

Redis String Commands: Basic and Common Operations

This article introduces Redis string data type and explains five basic commands (SET, GET, INCR, DECR, APPEND) as well as five commonly used commands (STRLEN, GETRANGE, SETRANGE, MSET, MGET), providing syntax, behavior, and example outputs.

Practical DevOps Architecture
Practical DevOps Architecture
Practical DevOps Architecture
Redis String Commands: Basic and Common Operations

In Redis, the string data type is the most fundamental type, capable of storing any form of string including binary data, serialized data, JSON objects, or even images.

Basic string commands (five basic commands)

1. SET key value – Stores the given value under the specified key. Example: 127.0.0.1:6379> set k5 v5 returns OK .

2. GET key – Retrieves the string value associated with the key. Example: 127.0.0.1:6379> get k5 returns "v5" .

3. INCR key – Increments the numeric value stored at the key by 1; if the key does not exist, it is initialized to 0 before incrementing. This operation works only on numeric strings.

4. DECR key – Decrements the numeric value stored at the key by 1, the inverse of INCR.

5. APPEND key value – If the key exists, appends the given value to the end of the current value; if the key does not exist, it creates the key with the given value. The command returns the total length of the string after the append. Example: 127.0.0.1:6379> append k2 hello returns (integer) 7 , and 127.0.0.1:6379> get k2 then returns "15hello" .

Common string commands (five common commands)

1. STRLEN key – Returns the length of the string value stored at the key. If the key does not exist, it returns 0.

2. GETRANGE key start end – Retrieves a substring of the string value from start to end (both inclusive). Negative indices count from the end of the string.

3. SETRANGE key offset value – Overwrites part of the string stored at the key starting at the specified offset with the given value; if the key does not exist, it is treated as an empty string.

4. MSET key value [key value ...] – Sets multiple key‑value pairs in a single command. Returns OK . Example: 127.0.0.1:6379> mset k1 v1 k2 v3 returns OK .

5. MGET key [key ...] – Retrieves the values of all specified keys.

DatabaseRedisString()tutorialcommandsKey-Value
Practical DevOps Architecture
Written by

Practical DevOps Architecture

Hands‑on DevOps operations using Docker, K8s, Jenkins, and Ansible—empowering ops professionals to grow together through sharing, discussion, knowledge consolidation, and continuous improvement.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.