{
  "schema_version": 2,
  "id": "commands/incr",
  "title": "INCR",
  "url": "https://redis.io/docs/latest/commands/incr/",
  "summary": "Increments the integer value of a key by one. Uses 0 as initial value if the key doesn't exist.",
  "since": "1.0.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": "e9311f1cb211cc2fe1c6cd5c985faeb1a53d4c3138c2a7fc7f72e619b0b3264e",
  "sections": [
    {
      "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>"
    },
    {
      "id": "examples",
      "title": "Examples",
      "role": "example",
      "text": "Foundational: Increment the integer value of a key by one using INCR (initializes to 0 if key doesn't exist)\n\n**Difficulty:** Beginner\n\n**Commands:** SET, INCR, GET\n\n**Complexity:**\n- SET: O(1)\n- INCR: O(1)\n- GET: O(1)\n\n**Available in:** Redis CLI, C, C#, Go, Java (Asynchronous - Lettuce), Java (Reactive - Lettuce), Java (Synchronous - Jedis), JavaScript (Node.js), JavaScript (Node.js), PHP, Python, Rust (Asynchronous), Rust (Synchronous)\n\n##### Redis CLI\n\n[code example]\n\n##### C\n\n[code example]\n\n##### C#\n\n[code example]\n\n##### Go\n\n[code example]\n\n##### Java (Asynchronous - Lettuce)\n\n[code example]\n\n##### Java (Reactive - Lettuce)\n\n[code example]\n\n##### Java (Synchronous - Jedis)\n\n[code example]\n\n##### JavaScript (Node.js)\n\n[code example]\n\n##### JavaScript (Node.js)\n\n[code example]\n\n##### JavaScript (Node.js)\n\n[code example]\n\n##### JavaScript (Node.js)\n\n[code example]\n\n##### PHP\n\n[code example]\n\n##### Python\n\n[code example]\n\n##### Rust (Asynchronous)\n\n[code example]\n\n##### Rust (Synchronous)\n\n[code example]"
    },
    {
      "id": "details",
      "title": "Details",
      "role": "content",
      "text": ""
    },
    {
      "id": "pattern-counter",
      "title": "Pattern: counter",
      "role": "content",
      "text": "The counter pattern is the most obvious thing you can do with Redis atomic\nincrement operations.\nThe idea is simply send an `INCR` command to Redis every time an operation\noccurs.\nFor instance in a web application we may want to know how many page views this\nuser did every day of the year.\n\nTo do so the web application may simply increment a key every time the user\nperforms a page view, creating the key name concatenating the User ID and a\nstring representing the current date.\n\nThis simple pattern can be extended in many ways:\n\n* It is possible to use `INCR` and [`EXPIRE`](https://redis.io/docs/latest/commands/expire) together at every page view to have\n  a counter counting only the latest N page views separated by less than the\n  specified amount of seconds.\n* A client may use GETSET in order to atomically get the current counter value\n  and reset it to zero.\n* Using other atomic increment/decrement commands like [`DECR`](https://redis.io/docs/latest/commands/decr) or [`INCRBY`](https://redis.io/docs/latest/commands/incrby) it\n  is possible to handle values that may get bigger or smaller depending on the\n  operations performed by the user.\n  Imagine for instance the score of different users in an online game."
    },
    {
      "id": "pattern-rate-limiter",
      "title": "Pattern: rate limiter",
      "role": "content",
      "text": "The rate limiter pattern is a special counter that is used to limit the rate at\nwhich an operation can be performed.\nThe classical materialization of this pattern involves limiting the number of\nrequests that can be performed against a public API.\n\nYou can implement this pattern with INCR in two ways. Both examples limit API calls to a maximum of ten requests per second per IP address.\n\n#### Pattern: rate limiter 1\n\nThe more simple and direct implementation of this pattern is the following:\n\n[code example]\n\nIn this example, there is a counter for every IP, for every different second.\nBut these counters are always incremented setting an expire of 10 seconds so that\nthey'll be removed by Redis automatically when the current second is a different\none.\n\nNote the used of [`MULTI`](https://redis.io/docs/latest/commands/multi) and [`EXEC`](https://redis.io/docs/latest/commands/exec) in order to make sure that we'll both\nincrement and set the expire at every API call.\n\n#### Pattern: rate limiter 2\n\nAn alternative implementation uses a single counter, but is a bit more complex\nto get it right without race conditions.\nWe'll examine different variants.\n\n[code example]\n\nThe counter is created in a way that it only will survive one second, starting\nfrom the first request performed in the current second.\nIf there are more than 10 requests in the same second the counter will reach a\nvalue greater than 10, otherwise it will expire and start again from 0.\n\n**In the above code there is a race condition**.\nIf for some reason the client performs the `INCR` command but does not perform\nthe [`EXPIRE`](https://redis.io/docs/latest/commands/expire) the key will be leaked until we'll see the same IP address again.\n\nThis can be easily fixed by turning the `INCR` with optional [`EXPIRE`](https://redis.io/docs/latest/commands/expire) into a Lua\nscript that is then sent using the [`EVAL`](https://redis.io/docs/latest/commands/eval) command (only available since Redis version\n2.6).\n\n[code example]\n\nThere is a different way to fix this issue without using scripting, by using\nRedis lists instead of counters.\nThe implementation is more complex and uses more advanced features but has the\nadvantage of remembering the IP addresses of the clients currently performing an\nAPI call, that may be useful or not depending on the application.\n\n[code example]\n\nThe [`RPUSHX`](https://redis.io/docs/latest/commands/rpushx) command only pushes the element if the key already exists.\n\nNote that we have a race here, but it is not a problem: [`EXISTS`](https://redis.io/docs/latest/commands/exists) may return\nfalse but the key may be created by another client before we create it inside\nthe [`MULTI`](https://redis.io/docs/latest/commands/multi) / [`EXEC`](https://redis.io/docs/latest/commands/exec) block.\nHowever this race will just miss an API call under rare conditions, so the rate\nlimiting will still work correctly."
    },
    {
      "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 value of the key after the increment.\n\n**RESP3:**\n\n[Integer reply](../../develop/reference/protocol-spec#integers): the value of the key after the increment."
    }
  ],
  "examples": [
    {
      "id": "examples-ex0",
      "language": "plaintext",
      "code": "> SET mykey \"10\"\nOK\n> INCR mykey\n(integer) 11\n> GET mykey\n\"11\"",
      "section_id": "examples"
    },
    {
      "id": "examples-ex1",
      "language": "c",
      "code": "reply = redisCommand(c, \"SET mykey 10\");\n    printf(\"%s\\n\", reply->str);\n    // >>> OK\n    freeReplyObject(reply);\n\n    reply = redisCommand(c, \"INCR mykey\");\n    printf(\"%lld\\n\", reply->integer);\n    // >>> 11\n    freeReplyObject(reply);\n\n    reply = redisCommand(c, \"GET mykey\");\n    printf(\"%s\\n\", reply->str);\n    // >>> 11\n    freeReplyObject(reply);",
      "section_id": "examples"
    },
    {
      "id": "examples-ex2",
      "language": "csharp",
      "code": "bool incrResult1 = db.StringSet(\"mykey\", \"10\");\n        Console.WriteLine(incrResult1); // >>> true\n\n        long incrResult2 = db.StringIncrement(\"mykey\");\n        Console.WriteLine(incrResult2); // >>> 11\n\n        RedisValue incrResult3 = db.StringGet(\"mykey\");\n        Console.WriteLine(incrResult3); // >>> 11",
      "section_id": "examples"
    },
    {
      "id": "examples-ex3",
      "language": "go",
      "code": "incrResult1, err := rdb.Set(ctx, \"mykey\", \"10\", 0).Result()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Println(incrResult1) // >>> OK\n\n\tincrResult2, err := rdb.Incr(ctx, \"mykey\").Result()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Println(incrResult2) // >>> 11\n\n\tincrResult3, err := rdb.Get(ctx, \"mykey\").Result()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Println(incrResult3) // >>> 11",
      "section_id": "examples"
    },
    {
      "id": "examples-ex4",
      "language": "java",
      "code": "CompletableFuture<Void> incrExample = asyncCommands.set(\"mykey\", \"10\")\n                    .thenCompose(incrResult1 -> {\n                        System.out.println(incrResult1);    // >>> OK\n                        return asyncCommands.incr(\"mykey\");\n                    })\n                    .thenCompose(incrResult2 -> {\n                        System.out.println(incrResult2);    // >>> 11\n                        return asyncCommands.get(\"mykey\");\n                    })\n                    .thenAccept(incrResult3 -> {\n                        System.out.println(incrResult3);    // >>> 11\n                    })\n                    .toCompletableFuture();",
      "section_id": "examples"
    },
    {
      "id": "examples-ex5",
      "language": "java",
      "code": "Mono<Void> incrExample = reactiveCommands.set(\"mykey\", \"10\")\n                    .flatMap(incrResult1 -> {\n                        System.out.println(incrResult1);    // >>> OK\n                        return reactiveCommands.incr(\"mykey\");\n                    })\n                    .flatMap(incrResult2 -> {\n                        System.out.println(incrResult2);    // >>> 11\n                        return reactiveCommands.get(\"mykey\");\n                    })\n                    .doOnNext(incrResult3 -> {\n                        System.out.println(incrResult3);    // >>> 11\n                    })\n                    .then();",
      "section_id": "examples"
    },
    {
      "id": "examples-ex6",
      "language": "java",
      "code": "String incrResult1 = jedis.set(\"mykey\", \"10\");\n        System.out.println(incrResult1);    // >>> OK\n\n        long incrResult2 = jedis.incr(\"mykey\");\n        System.out.println(incrResult2);    // >>> 11\n\n        String incrResult3 = jedis.get(\"mykey\");\n        System.out.println(incrResult3);    // >>> 11",
      "section_id": "examples"
    },
    {
      "id": "examples-ex7",
      "language": "javascript",
      "code": "const incrResult1 = await client.set('mykey', '10');\nconsole.log(incrResult1); // >>> OK\n\nconst incrResult2 = await client.incr('mykey');\nconsole.log(incrResult2); // >>> 11\n\nconst incrResult3 = await client.get('mykey');\nconsole.log(incrResult3); // >>> 11",
      "section_id": "examples"
    },
    {
      "id": "examples-ex8",
      "language": "javascript",
      "code": "const incrResult1 = await redis.set('mykey', '10');\nconsole.log(incrResult1); // >>> OK\n\nconst incrResult2 = await redis.incr('mykey');\nconsole.log(incrResult2); // >>> 11\n\nconst incrResult3 = await redis.get('mykey');\nconsole.log(incrResult3); // >>> 11",
      "section_id": "examples"
    },
    {
      "id": "examples-ex9",
      "language": "javascript",
      "code": "const incrResult1 = await client.set('mykey', '10');\nconsole.log(incrResult1); // >>> OK\n\nconst incrResult2 = await client.incr('mykey');\nconsole.log(incrResult2); // >>> 11\n\nconst incrResult3 = await client.get('mykey');\nconsole.log(incrResult3); // >>> 11",
      "section_id": "examples"
    },
    {
      "id": "examples-ex10",
      "language": "javascript",
      "code": "const incrResult1 = await redis.set('mykey', '10');\nconsole.log(incrResult1); // >>> OK\n\nconst incrResult2 = await redis.incr('mykey');\nconsole.log(incrResult2); // >>> 11\n\nconst incrResult3 = await redis.get('mykey');\nconsole.log(incrResult3); // >>> 11",
      "section_id": "examples"
    },
    {
      "id": "examples-ex11",
      "language": "php",
      "code": "$incrResult1 = $r->set('mykey', '10');\n        echo $incrResult1 . PHP_EOL;        // >>> OK\n\n        $incrResult2 = $r->incr('mykey');\n        echo $incrResult2 . PHP_EOL;        // >>> 11\n\n        $incrResult3 = $r->get('mykey');\n        echo $incrResult3 . PHP_EOL;        // >>> 11",
      "section_id": "examples"
    },
    {
      "id": "examples-ex12",
      "language": "python",
      "code": "incr_result1 = r.set(\"mykey\", \"10\")\nprint(incr_result1)\n# >>> True\n\nincr_result2 = r.incr(\"mykey\")\nprint(incr_result2)\n# >>> 11\n\nincr_result3 = r.get(\"mykey\")\nprint(incr_result3)\n# >>> 11",
      "section_id": "examples"
    },
    {
      "id": "examples-ex13",
      "language": "rust",
      "code": "if let Ok(res) = r.set(\"mykey\", \"10\").await {\n            let res: String = res;\n            println!(\"{res}\");    // >>> OK\n        }\n\n        match r.incr(\"mykey\", 1).await {\n            Ok(incr_result) => {\n                let incr_result: i64 = incr_result;\n                println!(\"{incr_result}\");    // >>> 11\n            }\n            Err(e) => {\n                println!(\"Error incrementing value: {e}\");\n            }\n        }\n\n        match r.get(\"mykey\").await {\n            Ok(get_result) => {\n                let get_result: String = get_result;\n                println!(\"{get_result}\");    // >>> 11\n            }\n            Err(e) => {\n                println!(\"Error getting value: {e}\");\n            }\n        }",
      "section_id": "examples"
    },
    {
      "id": "examples-ex14",
      "language": "rust",
      "code": "if let Ok(res) = r.set(\"mykey\", \"10\") {\n            let res: String = res;\n            println!(\"{res}\");    // >>> OK\n        }\n\n        match r.incr(\"mykey\", 1) {\n            Ok(incr_result) => {\n                let incr_result: i64 = incr_result;\n                println!(\"{incr_result}\");    // >>> 11\n            }\n            Err(e) => {\n                println!(\"Error incrementing value: {e}\");\n            }\n        }\n\n        match r.get(\"mykey\") {\n            Ok(get_result) => {\n                let get_result: String = get_result;\n                println!(\"{get_result}\");    // >>> 11\n            }\n            Err(e) => {\n                println!(\"Error getting value: {e}\");\n            }\n        }",
      "section_id": "examples"
    },
    {
      "id": "pattern-rate-limiter-ex0",
      "language": "plaintext",
      "code": "FUNCTION LIMIT_API_CALL(ip)\nts = CURRENT_UNIX_TIME()\nkeyname = ip+\":\"+ts\nMULTI\n    INCR(keyname)\n    EXPIRE(keyname,10)\nEXEC\ncurrent = RESPONSE_OF_INCR_WITHIN_MULTI\nIF current > 10 THEN\n    ERROR \"too many requests per second\"\nELSE\n    PERFORM_API_CALL()\nEND",
      "section_id": "pattern-rate-limiter"
    },
    {
      "id": "pattern-rate-limiter-ex1",
      "language": "plaintext",
      "code": "FUNCTION LIMIT_API_CALL(ip):\ncurrent = GET(ip)\nIF current != NULL AND current > 10 THEN\n    ERROR \"too many requests per second\"\nELSE\n    value = INCR(ip)\n    IF value == 1 THEN\n        EXPIRE(ip,1)\n    END\n    PERFORM_API_CALL()\nEND",
      "section_id": "pattern-rate-limiter"
    },
    {
      "id": "pattern-rate-limiter-ex2",
      "language": "plaintext",
      "code": "local current\ncurrent = redis.call(\"incr\",KEYS[1])\nif current == 1 then\n    redis.call(\"expire\",KEYS[1],1)\nend",
      "section_id": "pattern-rate-limiter"
    },
    {
      "id": "pattern-rate-limiter-ex3",
      "language": "plaintext",
      "code": "FUNCTION LIMIT_API_CALL(ip)\ncurrent = LLEN(ip)\nIF current > 10 THEN\n    ERROR \"too many requests per second\"\nELSE\n    IF EXISTS(ip) == FALSE\n        MULTI\n            RPUSH(ip,ip)\n            EXPIRE(ip,1)\n        EXEC\n    ELSE\n        RPUSHX(ip,ip)\n    END\n    PERFORM_API_CALL()\nEND",
      "section_id": "pattern-rate-limiter"
    }
  ]
}
