{
  "schema_version": 2,
  "id": "commands/setrange",
  "title": "SETRANGE",
  "url": "https://redis.io/docs/latest/commands/setrange/",
  "summary": "Overwrites a part of a string value with another by an offset. Creates the key if it doesn't exist.",
  "since": "2.2.0",
  "tags": [
    "docs",
    "develop",
    "stack",
    "oss",
    "rs",
    "rc",
    "oss",
    "kubernetes",
    "clients"
  ],
  "last_updated": "2026-09-18T16:45:12+01:00",
  "page_type": "content",
  "content_hash": "3407d9957e32c72856220eba65ca3ca9ce5013ceca29687f231edf64d01be064",
  "sections": [
    {
      "id": "overview",
      "title": "Overview",
      "role": "overview",
      "text": "Overwrites part of the string stored at `key`, starting at the specified offset,\nfor the entire length of `value`.\nIf the offset is larger than the current length of the string at `key`, the\nstring is padded with zero-bytes to make `offset` fit.\nNon-existing keys are considered as empty strings, so this command will make\nsure it holds a string large enough to be able to set `value` at `offset`.\n\nNote that the maximum offset that you can set is 2^29 -1 (536870911), as Redis\nstrings are limited to 512 megabytes.\nIf you need to grow beyond this size, you can use multiple keys.\n\n**Warning**: When setting the last possible byte and the string value stored at\n`key ` does not yet hold a string value, or holds a small string value, Redis\nneeds to allocate all intermediate memory which can block the server for some\ntime.\n\nNote that once the first allocation is done, subsequent calls to `SETRANGE` for\nthe same `key` will not have the allocation overhead."
    },
    {
      "id": "required-arguments",
      "title": "Required arguments",
      "role": "content",
      "text": "<details open><summary><code>key</code></summary>\n\nThe name of the key.\n\n</details>\n\n<details open><summary><code>offset</code></summary>\n\nThe zero-based offset at which to start overwriting. The string is zero-padded if the offset is beyond the current length.\n\n</details>\n\n<details open><summary><code>value</code></summary>\n\nThe string to write at the offset.\n\n</details>"
    },
    {
      "id": "examples",
      "title": "Examples",
      "role": "example",
      "text": "Basic usage:\n\n\nredis> SET key1 \"Hello World\"\nOK\nredis> SETRANGE key1 6 \"Redis\"\n(integer) 11\nredis> GET key1\n\"Hello Redis\"\n\n\nExample of zero padding:\n\n\nredis> SETRANGE key2 6 \"Redis\"\n(integer) 11\nredis> GET key2\n\"\\x00\\x00\\x00\\x00\\x00\\x00Redis\""
    },
    {
      "id": "details",
      "title": "Details",
      "role": "content",
      "text": ""
    },
    {
      "id": "patterns",
      "title": "Patterns",
      "role": "content",
      "text": "Thanks to `SETRANGE` and the analogous [`GETRANGE`](https://redis.io/docs/latest/commands/getrange) commands, you can use Redis\nstrings as a linear array with O(1) random access.\nThis is a very fast and efficient storage in many real world use cases."
    },
    {
      "id": "redis-software-and-redis-cloud-compatibility",
      "title": "Redis Software and Redis Cloud compatibility",
      "role": "content",
      "text": "| Redis<br />Software | Redis<br />Cloud | <span style=\"min-width: 9em; display: table-cell\">Notes</span> |\n|:----------------------|:-----------------|:------|\n| <span title=\"Supported\">&#x2705; Standard</span><br /><span title=\"Supported\"><nobr>&#x2705; Active-Active</nobr></span> | <span title=\"Supported\">&#x2705; Standard</span><br /><span title=\"Supported\"><nobr>&#x2705; Active-Active</nobr></span> |  |"
    },
    {
      "id": "return-information",
      "title": "Return information",
      "role": "returns",
      "text": "**RESP2:**\n\n[Integer reply](../../develop/reference/protocol-spec#integers): the length of the string after it was modified by the command.\n\n**RESP3:**\n\n[Integer reply](../../develop/reference/protocol-spec#integers): the length of the string after it was modified by the command."
    }
  ],
  "examples": []
}
