Redis Command: RESTORE

Summary

Command NameRESTORE
UsageRestore/recreate key from serialized data
Group generic
ACL Category@keyspace
@write
@slow
@dangerous
Time ComplexityO(N+M)
FlagWRITE
DENYOOM
Arity-4

NOTES

  • O(1) + O(N * M) is the complexity of this command in general case
    • O(1) is the complexity of creating a new key.
    • O(N * M) is the complexity of retrieving(unserializing) data from provided serialized data.
    • Here N is the number Redis Object used to compose the value.
    • M is the average size Redis Objects.
  • In case of a small string the time complexity will be O(1) + O(1 * M). Or simply we can drive O(1) as M is small.
  • In the case of a complex sorted set value, the complexity will be O(N * M * log(N)), as the complexity of inserting a value into the sorted set is O(log(N)).

Signature

RESTORE <key> <ttl> <serialized_data> [ REPLACE ] [ ABSTTL ] [ IDLETIME <idletime_in_seconds> ] [ FREQ <frequency> ]

For general/regular usages, we just need to pass the first three params <key>, <ttl>, <serialize_data>. Here is the simple usage signature-

RESTORE <key> <ttl> <serialized_data>

Usage

Restore data from a serialized(by DUMP command) data. This command creates a key, unserializes the provided data, then stores the value for the key.

NOTES

  • If the key already exists and the REPLACE option is not provided, then it will be recreated or replaced. It will throw an error in case of an existing key.
  • The expiration time of the original key is not contained in the data, so that is not contained in the created key.
  • RDB version and data checksum are checked before restoring data.

As the data is serialized by the DUMP command, so understanding the DUPM command is beneficial for using the REPLACE command.

Arguments

ParameterDescriptionNameTypeOptional
<key>Name of the key to be createdkeykey
<ttl>TTL(expiration time) of the keykeykey
<serialized_data>Serialized data generated by DUMP commandserialized-valuestring
REPLACEIf provided, then the key will be replaced(if exists)replacepure-tokenTrue
ABSTTLIf provided, then the <ttl> has to be a UNIX timestamp in millisecondsabsttlpure-tokenTrue
<idletime_in_seconds>Set idle time for Redis eviction policysecondsintegerTrue
<frequency>Set access frequency for the Redis eviction policyfrequencyintegerTrue

NOTES

  • In general cases and most of the cases, the <ttl> key will be 0. In that case, there will be no expiration time for the key.
  • If the key does not exist, then REPLACE modifier has no effect. The key will be created normally.

Return Value

Return valueCase for the return valueType
OKIf the command is successfulconst
errorIf the serialized data has some fault, or a key exists but REPLACE is not providederror

NOTES

  • If the data has some fault, then the following error is returned-
    (error) ERR DUMP payload version or checksum are wrong
  • If REPLACE modifier is not provided and the key already exists, then the following error is returned-
    (error) BUSYKEY Target key name already exists.

Command Examples

Here are a few examples of the RESTORE command usage-

# Redis RESTORE command examples

# Set a string value
127.0.0.1:6379> set bigboxstr "My string value saved in a Big Box"
OK

# Dump the string value
127.0.0.1:6379> dump bigboxstr
"x00"My string value saved in a Big Boxx0bx00axbf3nx83Mxdd!"

# Delete string key
127.0.0.1:6379> del bigboxstr
(integer) 1

# Restore the string from dumpped string value
127.0.0.1:6379> restore bigboxstr 0 "x00"My string value saved in a Big Boxx0bx00axbf3nx83Mxdd!"
OK

# Check restored string value
127.0.0.1:6379> get bigboxstr
"My string value saved in a Big Box"

# Push values to list
127.0.0.1:6379> rpush bigboxlist abc123 def234 ghi345 jkl456 mno567 pqrst678 uvwxyz789
(integer) 7

# Dump the list data
127.0.0.1:6379> dump bigboxlist
"x12x01x02@DDx00x00x00ax00x86abc123ax86def234ax86ghi345ax86jkl456ax86mno567ax88pqrst678tx89uvwxyz789nxffx0bx00x8bxcar xb8x1c'x7f"

# Add some more item to the list
127.0.0.1:6379> rpush bigboxlist "extra item 1" "extra item 2"
(integer) 9

# Restore list to the dumpped value, which is older value
# Use REPLACE to relace existing value
127.0.0.1:6379> restore bigboxlist 0 "x12x01x02@DDx00x00x00ax00x86abc123ax86def234ax86ghi345ax86jkl456ax86mno567ax88pqrst678tx89uvwxyz789nxffx0bx00x8bxcar xb8x1c'x7f" replace
OK

# Check the list value
# It has old values 
127.0.0.1:6379> lrange bigboxlist 0 -1
1) "abc123"
2) "def234"
3) "ghi345"
4) "jkl456"
5) "mno567"
6) "pqrst678"
7) "uvwxyz789"

# Add data to set
127.0.0.1:6379> sadd bigboxset 6218014406 1619751279 7858399611 7595030997 9982317472
(integer) 5

# Dump set data
127.0.0.1:6379> dump bigboxset
"x0b0bx00x00x00x05x00x00x00oqx8b`x00x00x00x00xc6^x9frx01x00x00x00xd5xf9xb2xc4x01x00x00x00{xa9exd4x01x00x00x00xa0x13xfeRx02x00x00x00x0bx00xa1,xe09x13xf0fxe5"

# Delete set
127.0.0.1:6379> del bigboxset
(integer) 1

# Restore set data
127.0.0.1:6379> restore bigboxset 0 "x0b0bx00x00x00x05x00x00x00oqx8b`x00x00x00x00xc6^x9frx01x00x00x00xd5xf9xb2xc4x01x00x00x00{xa9exd4x01x00x00x00xa0x13xfeRx02x00x00x00x0bx00xa1,xe09x13xf0fxe5"
OK

# Check set data
127.0.0.1:6379> smembers bigboxset
1) "1619751279"
2) "6218014406"
3) "7595030997"
4) "7858399611"
5) "9982317472"

# Set hash values
127.0.0.1:6379> hset bigboxhash secret1 3622625569 secret2 5251979648 secret3 7686908197 secret4 0645904521 secret5 8386817336 secret6 2706180686 secret7  7252297879
(integer) 7

# Dump hash
127.0.0.1:6379> dump bigboxhash
"x10xc3@i@x8ex13x8ex00x00x00x0ex00x87secret1bxf4!xe1xecxd7 x12x01x00txa0x12a2bxf4x80xd9n9x01xe0x02x12x063bxf4%xe9,xcaxe0x03x12r4bx8a0645904521x0bxa0:x065bxf48xadxe4xf3xe0x03'x066bxf4NnMxa1 Lxe0x00`x067bxf4x97HExb0@%x01txffx0bx00xeax15x94x95Kxf0xd7R"

# Add new item to hash
127.0.0.1:6379> hset bigboxhash new1 "new item 1" new2 "new item 2"
(integer) 2

# Check hash
127.0.0.1:6379> hgetall bigboxhash
 1) "secret1"
 2) "3622625569"
 3) "secret2"
 4) "5251979648"
 5) "secret3"
 6) "7686908197"
 7) "secret4"
 8) "0645904521"
 9) "secret5"
10) "8386817336"
11) "secret6"
12) "2706180686"
13) "secret7"
14) "7252297879"
15) "new1"
16) "new item 1"
17) "new2"
18) "new item 2"

# Restore hash with replace using a older dump data
127.0.0.1:6379> restore bigboxhash 0 "x10xc3@i@x8ex13x8ex00x00x00x0ex00x87secret1bxf4!xe1xecxd7 x12x01x00txa0x12a2bxf4x80xd9n9x01xe0x02x12x063bxf4%xe9,xcaxe0x03x12r4bx8a0645904521x0bxa0:x065bxf48xadxe4xf3xe0x03'x066bxf4NnMxa1 Lxe0x00`x067bxf4x97HExb0@%x01txffx0bx00xeax15x94x95Kxf0xd7R" replace
OK

# Check hash data
# It has the data form the dump
127.0.0.1:6379> hgetall bigboxhash
 1) "secret1"
 2) "3622625569"
 3) "secret2"
 4) "5251979648"
 5) "secret3"
 6) "7686908197"
 7) "secret4"
 8) "0645904521"
 9) "secret5"
10) "8386817336"
11) "secret6"
12) "2706180686"
13) "secret7"
14) "7252297879"

# Wrong data example
127.0.0.1:6379> restore somekey 0 "some random and wrong data, not serialized properly"
(error) ERR DUMP payload version or checksum are wrong

# Existing key without replace
127.0.0.1:6379> restore bigboxstr 0 "x00"My string value saved in a Big Boxx0bx00axbf3nx83Mxdd!"
(error) BUSYKEY Target key name already exists.

Code Implementations

Here are the usage examples of the Redis RESTORE command in different programming languages.

// Redis RESTORE command example in Golang

package main

import (
	"context"
	"fmt"

	"github.com/redis/go-redis/v9"
)

var rdb *redis.Client
var ctx context.Context

func init() {
	rdb = redis.NewClient(&redis.Options{
		Addr:     "localhost:6379",
		Username: "default",
		Password: "",
		DB:       0,
	})

	ctx = context.Background()
}

func main() {

	/**
	* Set a string value
	*
	* Command: set bigboxstr "My string value saved in a Big Box"
	* Result: OK
	 */
	setResult, err := rdb.Set(ctx, "bigboxstr", "My string value saved in a Big Box", 0).Result()

	if err != nil {
		fmt.Println("Command: set bigboxstr "My string value saved in a Big Box" | Error: " + err.Error())
	}

	fmt.Println("Command: set bigboxstr "My string value saved in a Big Box" | Result: " + setResult)

	/**
	* Dump the string value
	*
	* Command: dump bigboxstr
	* Result: "x00"My string value saved in a Big Boxx0bx00axbf3nx83Mxdd!"
	 */
	dumpResult, err := rdb.Dump(ctx, "bigboxstr").Result()

	if err != nil {
		fmt.Println("Command: dump bigboxstr | Error: " + err.Error())
	}

	fmt.Println("Command: dump bigboxstr | Result: ", dumpResult)

	/**
	* Delete string key
	*
	* Command: del bigboxstr
	* Result: (integer) 1
	 */
	deleteResult, err := rdb.Del(ctx, "bigboxstr").Result()

	if err != nil {
		fmt.Println("Command: del bigboxstr | Error: " + err.Error())
	}

	fmt.Println("Command: del bigboxstr | Result: ", deleteResult)

	/**
	* Restore the string from dumpped string value
	*
	* Command: restore bigboxstr 0 "x00"My string value saved in a Big Boxx0bx00axbf3nx83Mxdd!"
	* Result: OK
	 */
	restoreResult, err := rdb.Restore(ctx, "bigboxstr", 0, dumpResult).Result()

	if err != nil {
		fmt.Println("Command: restore bigboxstr 0 "x00"My string value saved in a Big Boxx0bx00axbf3nx83Mxdd!" | Error: " + err.Error())
	}

	fmt.Println("Command: restore bigboxstr 0 "\x00\"My string value saved in a Big Box\x0b\x00\a\xbf3\n\x83M\xdd!" | Result: " + restoreResult)

	/**
	* Check restored string value
	*
	* Command: get bigboxstr
	* Result: "My string value saved in a Big Box"
	 */
	getResult, err := rdb.Get(ctx, "bigboxstr").Result()

	if err != nil {
		fmt.Println("Command: get bigboxstr | Error: " + err.Error())
	}

	fmt.Println("Command: get bigboxstr | Result: " + getResult)
	/**
	* Push values to list
	*
	* Command: rpush bigboxlist abc123 def234 ghi345 jkl456 mno567 pqrst678 uvwxyz789
	* Result: (integer) 7
	 */
	rpushResult, err := rdb.RPush(ctx, "bigboxlist", "abc123", "def234", "ghi345", "jkl456", "mno567", "pqrst678", "uvwxyz789").Result()

	if err != nil {
		fmt.Println("Command: rpush bigboxlist abc123 def234 ghi345 jkl456 mno567 pqrst678 uvwxyz789 | Error: " + err.Error())
	}

	fmt.Println("Command: rpush bigboxlist abc123 def234 ghi345 jkl456 mno567 pqrst678 uvwxyz789 | Result: ", rpushResult)

	/**
	* Dump the list data
	*
	* Command: dump bigboxlist
	* Result: "x12x01x02@DDx00x00x00ax00x86abc123ax86def234ax86ghi345ax86jkl456ax86mno567ax88pqrst678tx89uvwxyz789nxffx0bx00x8bxcar xb8x1c'x7f"
	 */
	dumpResult, err = rdb.Dump(ctx, "bigboxlist").Result()

	if err != nil {
		fmt.Println("Command: dump bigboxlist | Error: " + err.Error())
	}

	fmt.Println("Command: dump bigboxlist | Result: " + dumpResult)

	/**
	* Add some more item to the list
	*
	* Command: rpush bigboxlist "extra item 1" "extra item 2"
	* Result: (integer) 9
	 */
	rpushResult, err = rdb.RPush(ctx, "bigboxlist", "extra item 1", "extra item 2").Result()

	if err != nil {
		fmt.Println("Command: rpush bigboxlist "extra item 1" "extra item 2" | Error: " + err.Error())
	}

	fmt.Println("Command: rpush bigboxlist "extra item 1" "extra item 2" | Result: ", rpushResult)

	/**
	* Restore list to the dumpped value, which is older value
	* Use REPLACE to relace existing value
	*
	* Command: restore bigboxlist 0 "x12x01x02@DDx00x00x00ax00x86abc123ax86def234ax86ghi345ax86jkl456ax86mno567ax88pqrst678tx89uvwxyz789nxffx0bx00x8bxcar xb8x1c'x7f" replace
	* Result: OK
	 */
	restoreResult, err = rdb.RestoreReplace(ctx, "bigboxlist", 0, dumpResult).Result()

	if err != nil {
		fmt.Println("Command: restore bigboxlist 0 "\x12\x01\x02@DD\x00\x00\x00\a\x00\x86abc123\a\x86def234\a\x86ghi345\a\x86jkl456\a\x86mno567\a\x88pqrst678\t\x89uvwxyz789\n\xff\x0b\x00\x8b\xca\r \xb8\x1c'\x7f" replace | Error: " + err.Error())
	}

	fmt.Println("Command: restore bigboxlist 0 "\x12\x01\x02@DD\x00\x00\x00\a\x00\x86abc123\a\x86def234\a\x86ghi345\a\x86jkl456\a\x86mno567\a\x88pqrst678\t\x89uvwxyz789\n\xff\x0b\x00\x8b\xca\r \xb8\x1c'\x7f" replace | Result: " + restoreResult)

	/**
	* Check the list value
	* It has old values
	*
	* Command: lrange bigboxlist 0 -1
	* Result:
	*      1) "abc123"
	*      2) "def234"
	*      3) "ghi345"
	*      4) "jkl456"
	*      5) "mno567"
	*      6) "pqrst678"
	*      7) "uvwxyz789"
	 */
	lrangeResult, err := rdb.LRange(ctx, "bigboxlist", 0, -1).Result()

	if err != nil {
		fmt.Println("Command: lrange bigboxlist 0 -1 | Error: " + err.Error())
	}

	fmt.Println("Command: lrange bigboxlist 0 -1 | Result: ", lrangeResult)

	/**
	* Add data to set
	*
	* Command: sadd bigboxset 6218014406 1619751279 7858399611 7595030997 9982317472
	* Result: (integer) 5
	 */
	saddResult, err := rdb.SAdd(ctx, "bigboxset", "6218014406", "1619751279", "7858399611", "7595030997", "9982317472").Result()

	if err != nil {
		fmt.Println("Command: sadd bigboxset 6218014406 1619751279 7858399611 7595030997 9982317472 | Error: " + err.Error())
	}

	fmt.Println("Command: sadd bigboxset 6218014406 1619751279 7858399611 7595030997 9982317472 | Result: ", saddResult)

	/**
	* Dump set data
	*
	* Command: dump bigboxset
	* Result: "x0b0bx00x00x00x05x00x00x00oqx8b`x00x00x00x00xc6^x9frx01x00x00x00xd5xf9xb2xc4x01x00x00x00{xa9exd4x01x00x00x00xa0x13xfeRx02x00x00x00x0bx00xa1,xe09x13xf0fxe5"
	 */
	dumpResult, err = rdb.Dump(ctx, "bigboxset").Result()

	if err != nil {
		fmt.Println("Command: dump bigboxset | Error: " + err.Error())
	}

	fmt.Println("Command: dump bigboxset | Result: " + dumpResult)

	/**
	* Delete set
	*
	* Command: del bigboxset
	* Result: (integer) 1
	 */
	deleteResult, err = rdb.Del(ctx, "bigboxset").Result()

	if err != nil {
		fmt.Println("Command: del bigboxset | Error: " + err.Error())
	}

	fmt.Println("Command: del bigboxset | Result: " + restoreResult)

	/**
	* Restore set data
	*
	* Command: restore bigboxset 0 "x0b0bx00x00x00x05x00x00x00oqx8b`x00x00x00x00xc6^x9frx01x00x00x00xd5xf9xb2xc4x01x00x00x00{xa9exd4x01x00x00x00xa0x13xfeRx02x00x00x00x0bx00xa1,xe09x13xf0fxe5"
	* Result: OK
	 */
	restoreResult, err = rdb.Restore(ctx, "bigboxset", 0, dumpResult).Result()

	if err != nil {
		fmt.Println("Command: restore bigboxset 0 "x0b0bx00x00x00x05x00x00x00oqx8b`x00x00x00x00xc6^x9frx01x00x00x00xd5xf9xb2xc4x01x00x00x00{xa9exd4x01x00x00x00xa0x13xfeRx02x00x00x00x0bx00xa1,xe09x13xf0fxe5" | Error: " + err.Error())
	}

	fmt.Println("Command: restore bigboxset 0 "x0b0bx00x00x00x05x00x00x00oqx8b`x00x00x00x00xc6^x9frx01x00x00x00xd5xf9xb2xc4x01x00x00x00{xa9exd4x01x00x00x00xa0x13xfeRx02x00x00x00x0bx00xa1,xe09x13xf0fxe5" | Result: " + restoreResult)

	/**
	* Check set data
	*
	* Command: smembers bigboxset
	* Result:
	*      1) "1619751279"
	*      2) "6218014406"
	*      3) "7595030997"
	*      4) "7858399611"
	*      5) "9982317472"
	 */
	smemebersResult, err := rdb.SMembers(ctx, "bigboxset").Result()

	if err != nil {
		fmt.Println("Command: smembers bigboxset | Error: " + err.Error())
	}

	fmt.Println("Command: smembers bigboxset | Result: ", smemebersResult)

	/**
	* Set hash values
	*
	* Command: hset bigboxhash secret1 3622625569 secret2 5251979648 secret3 7686908197 secret4 0645904521 secret5 8386817336 secret6 2706180686 secret7  7252297879
	* Result: (integer) 7
	 */
	hsetResult, err := rdb.HMSet(ctx, "bigboxhash",
		"secret1", "3622625569",
		"secret2", "5251979648",
		"secret3", "7686908197",
		"secret4", "0645904521",
		"secret5", "8386817336",
		"secret6", "2706180686",
		"secret7", "7252297879",
	).Result()

	if err != nil {
		fmt.Println("Command: hset bigboxhash secret1 3622625569 secret2 5251979648 secret3 7686908197 secret4 0645904521 secret5 8386817336 secret6 2706180686 secret7  7252297879 | Error: " + err.Error())
	}

	fmt.Println("Command: hset bigboxhash secret1 3622625569 secret2 5251979648 secret3 7686908197 secret4 0645904521 secret5 8386817336 secret6 2706180686 secret7  7252297879 | Result: ", hsetResult)

	/**
	* Dump hash
	*
	* Command: dump bigboxhash
	* Result: "x10xc3@i@x8ex13x8ex00x00x00x0ex00x87secret1bxf4!xe1xecxd7 x12x01x00txa0x12a2bxf4x80xd9n9x01xe0x02x12x063bxf4%xe9,xcaxe0x03x12r4bx8a0645904521x0bxa0:x065bxf48xadxe4xf3xe0x03'x066bxf4NnMxa1 Lxe0x00`x067bxf4x97HExb0@%x01txffx0bx00xeax15x94x95Kxf0xd7R"
	 */
	dumpResult, err = rdb.Dump(ctx, "bigboxhash").Result()

	if err != nil {
		fmt.Println("Command: dump bigboxhash | Error: " + err.Error())
	}

	fmt.Println("Command: dump bigboxhash | Result: " + dumpResult)

	/**
	* Add new item to hash
	*
	* Command: hset bigboxhash new1 "new item 1" new2 "new item 2"
	* Result: (integer) 2
	 */
	hsetResult, err = rdb.HMSet(ctx, "bigboxhash",
		"new1", "new item 1",
		"new2", "new item 2",
	).Result()

	if err != nil {
		fmt.Println("Command: hset bigboxhash new1 "new item 1" new2 "new item 2" | Error: " + err.Error())
	}

	fmt.Println("Command: hset bigboxhash new1 "new item 1" new2 "new item 2" | Result: ", hsetResult)

	/**
	* Check hash
	*
	* Command: hgetall bigboxhash
	* Result:
	*      1) "secret1"
	*      2) "3622625569"
	*      3) "secret2"
	*      4) "5251979648"
	*      5) "secret3"
	*      6) "7686908197"
	*      7) "secret4"
	*      8) "0645904521"
	*      9) "secret5"
	*      10) "8386817336"
	*      11) "secret6"
	*      12) "2706180686"
	*      13) "secret7"
	*      14) "7252297879"
	*      15) "new1"
	*      16) "new item 1"
	*      17) "new2"
	*      18) "new item 2"
	 */
	hgetallResult, err := rdb.HGetAll(ctx, "bigboxhash").Result()

	if err != nil {
		fmt.Println("Command: hgetall bigboxhash | Error: " + err.Error())
	}

	fmt.Println("Command: hgetall bigboxhash | Result: ", hgetallResult)

	/**
	* Restore hash with replace using a older dump data
	*
	* Command: restore bigboxhash 0 "x10xc3@i@x8ex13x8ex00x00x00x0ex00x87secret1bxf4!xe1xecxd7 x12x01x00txa0x12a2bxf4x80xd9n9x01xe0x02x12x063bxf4%xe9,xcaxe0x03x12r4bx8a0645904521x0bxa0:x065bxf48xadxe4xf3xe0x03'x066bxf4NnMxa1 Lxe0x00`x067bxf4x97HExb0@%x01txffx0bx00xeax15x94x95Kxf0xd7R" replace
	* Result: OK
	 */
	restoreResult, err = rdb.RestoreReplace(ctx, "bigboxhash", 0, dumpResult).Result()

	if err != nil {
		fmt.Println("Command: restore bigboxhash 0 "x10xc3@i@x8ex13x8ex00x00x00x0ex00x87secret1bxf4!xe1xecxd7 x12x01x00txa0x12a2bxf4x80xd9n9x01xe0x02x12x063bxf4%xe9,xcaxe0x03x12r4bx8a0645904521x0bxa0:x065bxf48xadxe4xf3xe0x03'x066bxf4NnMxa1 Lxe0x00`x067bxf4x97HExb0@%x01txffx0bx00xeax15x94x95Kxf0xd7R" replace | Error: " + err.Error())
	}

	fmt.Println("Command: restore bigboxhash 0 "\x10\xc3@i@\x8e\x13\x8e\x00\x00\x00\x0e\x00\x87secret1\b\xf4!\xe1\xec\xd7 \x12\x01\x00\t\xa0\x12\a2\b\xf4\x80\xd9\n9\x01\xe0\x02\x12\x063\b\xf4%\xe9,\xca\xe0\x03\x12\r4\b\x8a0645904521\x0b\xa0:\x065\b\xf48\xad\xe4\xf3\xe0\x03'\x066\b\xf4N\nM\xa1 L\xe0\x00`\x067\b\xf4\x97HE\xb0@%\x01\t\xff\x0b\x00\xea\x15\x94\x95K\xf0\xd7R" replace | Result: " + restoreResult)

	/**
	* Check hash data
	* It has the data form the dump
	*
	* Command: hgetall bigboxhash
	* Result:
	*          1) "secret1"
	*          2) "3622625569"
	*          3) "secret2"
	*          4) "5251979648"
	*          5) "secret3"
	*          6) "7686908197"
	*          7) "secret4"
	*          8) "0645904521"
	*          9) "secret5"
	*          10) "8386817336"
	*          11) "secret6"
	*          12) "2706180686"
	*          13) "secret7"
	*          14) "7252297879"
	 */
	hgetallResult, err = rdb.HGetAll(ctx, "bigboxhash").Result()

	if err != nil {
		fmt.Println("Command: hgetall bigboxhash | Error: " + err.Error())
	}

	fmt.Println("Command: hgetall bigboxhash | Result: ", hgetallResult)

	/**
	* Wrong data example
	*
	* Command: restore somekey 0 "some random and wrong data, not serialized properly"
	* Result: (error) ERR DUMP payload version or checksum are wrong
	 */
	restoreResult, err = rdb.RestoreReplace(ctx, "somekey", 0, "some random and wrong data, not serialized properly").Result()

	if err != nil {
		fmt.Println("Command: restore somekey 0 "some random and wrong data, not serialized properly" | Error: " + err.Error())
	}

	fmt.Println("Command: restore somekey 0 "some random and wrong data, not serialized properly" | Result: " + restoreResult)

	/**
	* Existing key without replace
	*
	* Command: restore bigboxstr 0 "x00"My string value saved in a Big Boxx0bx00axbf3nx83Mxdd!"
	* Result: (error) BUSYKEY Target key name already exists.
	 */
	restoreResult, err = rdb.Restore(ctx, "bigboxhash", 0, dumpResult).Result()

	if err != nil {
		fmt.Println("Command: restore bigboxstr 0 "x00"My string value saved in a Big Boxx0bx00axbf3nx83Mxdd! | Error: " + err.Error())
	}

	fmt.Println("Command: restore bigboxstr 0 "x00"My string value saved in a Big Boxx0bx00axbf3nx83Mxdd! | Result: " + restoreResult)

}

Output:

Command: set bigboxstr "My string value saved in a Big Box" | Result: OK
Command: dump bigboxstr | Result:  "My string value saved in a Big Box
�3
�M�!
Command: del bigboxstr | Result:  1
Command: restore bigboxstr 0 "x00"My string value saved in a Big Boxx0bx00axbf3nx83Mxdd!" | Result: OK
Command: get bigboxstr | Result: My string value saved in a Big Box

Command: rpush bigboxlist abc123 def234 ghi345 jkl456 mno567 pqrst678 uvwxyz789 | Result:  7
Command: dump bigboxlist | Result: ↕☺☻@DD�abc123�def234�ghi345�jkl456�mno567�pqrst678   �uvwxyz789
�
 �∟'
Command: rpush bigboxlist "extra item 1" "extra item 2" | Result:  9
Command: restore bigboxlist 0 "x12x01x02@DDx00x00x00ax00x86abc123ax86def234ax86ghi345ax86jkl456ax86mno567ax88pqrst678tx89uvwxyz789nxffx0bx00x8bxcar xb8x1c'x7f" replace | Result: OK
Command: lrange bigboxlist 0 -1 | Result:  [abc123 def234 ghi345 jkl456 mno567 pqrst678 uvwxyz789]

Command: sadd bigboxset 6218014406 1619751279 7858399611 7595030997 9982317472 | Result:  5
Command: dump bigboxset | Result:
♣oq�`�^�r☺����☺{�e�☺�‼�R☻
�,�9‼�f�
Command: del bigboxset | Result: OK
Command: restore bigboxset 0 "
♣oq�`�^�r☺����☺{�e�☺�‼�R☻
�,�9‼�f�" | Result: OK
Command: smembers bigboxset | Result:  [1619751279 6218014406 7595030997 7858399611 9982317472]

Command: hset bigboxhash secret1 3622625569 secret2 5251979648 secret3 7686908197 secret4 0645904521 secret5 8386817336 secret6 2706180686 secret7  7252297879 | Result:  true
Command: dump bigboxhash | Result: ►�@i@�‼��secret�!��� ↕☺      �↕���
�0645904521�♥↕
�:♠�8����♥'♠�N
M� L�`♠��HE�@%☺ �
�§��K��R
Command: hset bigboxhash new1 "new item 1" new2 "new item 2" | Result:  true
Command: hgetall bigboxhash | Result:  map[new1:new item 1 new2:new item 2 secret1:3622625569 secret2:5251979648 secret3:7686908197 secret4:0645904521 secret5:8386817336 secret6:2706180686 secret7:7252297879]
Command: restore bigboxhash 0 "x10xc3@i@x8ex13x8ex00x00x00x0ex00x87secret1bxf4!xe1xecxd7 x12x01x00txa0x12a2bxf4x80xd9n9x01xe0x02x12x063bxf4%xe9,xcaxe0x03x12r4bx8a0645904521x0bxa0:x065bxf48xadxe4xf3xe0x03'x066bxf4NnMxa1 Lxe0x00`x067bxf4x97HExb0@%x01txffx0bx00xeax15x94x95Kxf0xd7R" replace | Result: OK
Command: hgetall bigboxhash | Result:  map[secret1:3622625569 secret2:5251979648 secret3:7686908197 secret4:0645904521 secret5:8386817336 secret6:2706180686 secret7:7252297879]

Command: restore somekey 0 "some random and wrong data, not serialized properly" | Error: ERR DUMP payload version or checksum are wrong
Command: restore somekey 0 "some random and wrong data, not serialized properly" | Result:

Command: restore bigboxstr 0 ""My string value saved in a Big Box
�3
�M�! | Error: BUSYKEY Target key name already exists.
Command: restore bigboxstr 0 ""My string value saved in a Big Box
�3
�M�! | Result:

NOTES

  • Use “Restore“, or “RestoreReplace” method from redis-go module.
  • Signature of the methods is-
    Restore(ctx context.Context, key string, ttl time.Duration, value string) *StatusCmd
    RestoreReplace(ctx context.Context, key string, ttl time.Duration, value string) *StatusCmd
// Redis RESTORE command example in Java

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.params.RestoreParams;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class Replace {
    public static void main(String[] args) {
        // Create connection pool
        JedisPool jedisPool = new JedisPool("localhost", 6379);

        try (Jedis jedis = jedisPool.getResource()) {

            /**
             * Set a string value
             *
             * Command: set bigboxstr "My string value saved in a Big Box"
             * Result: OK
             */
            String setResult = jedis.set("bigboxstr", "My string value saved in a Big Box");

            System.out.println("Command: set bigboxstr "My string value saved in a Big Box" | Result: " + setResult);

            /**
             * Dump the string value
             *
             * Command: dump bigboxstr
             * Result: "x00"My string value saved in a Big Boxx0bx00axbf3nx83Mxdd!"
             */
            byte[] dumpResult = jedis.dump("bigboxstr");

            System.out.println("Command: dump bigboxstr | Result: " + new String(dumpResult, StandardCharsets.UTF_8));

            /**
             * Delete string key
             *
             * Command: del bigboxstr
             * Result: (integer) 1
             */
            long deleteResult = jedis.del("bigboxstr");

            System.out.println("Command: del bigboxstr | Result: " + deleteResult);

            /**
             * Restore the string from dumpped string value
             *
             * Command: restore bigboxstr 0 "x00"My string value saved in a Big Boxx0bx00axbf3nx83Mxdd!"
             * Result: OK
             */
            String restoreResult = jedis.restore("bigboxstr", 0, dumpResult);

            System.out.println("Command: restore bigboxstr 0 "\x00\"My string value saved in a Big Box\x0b\x00\a\xbf3\n\x83M\xdd!" | Result: " + restoreResult);

            /**
             * Check restored string value
             *
             * Command: get bigboxstr
             * Result: "My string value saved in a Big Box"
             */
            String getResult = jedis.get("bigboxstr");

            System.out.println("Command: get bigboxstr | Result" + getResult);

            /**
             * Push values to list
             *
             * Command: rpush bigboxlist abc123 def234 ghi345 jkl456 mno567 pqrst678 uvwxyz789
             * Result: (integer) 7
             */
            long rpushResult = jedis.rpush("bigboxlist", "abc123", "def234", "ghi345", "jkl456", "mno567", "pqrst678", "uvwxyz789");

            System.out.println("Command: rpush bigboxlist abc123 def234 ghi345 jkl456 mno567 pqrst678 uvwxyz789 | Result: " + rpushResult);

            /**
             * Dump the list data
             *
             * Command: dump bigboxlist
             * Result: "x12x01x02@DDx00x00x00ax00x86abc123ax86def234ax86ghi345ax86jkl456ax86mno567ax88pqrst678tx89uvwxyz789nxffx0bx00x8bxcar xb8x1c'x7f"
             */
            dumpResult = jedis.dump("bigboxlist");

            System.out.println("Command: dump bigboxlist | Result: " + new String(dumpResult, StandardCharsets.UTF_8));

            /**
             * Add some more item to the list
             *
             * Command: rpush bigboxlist "extra item 1" "extra item 2"
             * Result: (integer) 9
             */
            rpushResult = jedis.rpush("bigboxlist", "extra item 1", "extra item 2");

            System.out.println("Command: rpush bigboxlist "extra item 1" "extra item 2" | Result: " + rpushResult);

            /**
             * Restore list to the dumpped value, which is older value
             * Use REPLACE to relace existing value
             *
             * Command: restore bigboxlist 0 "x12x01x02@DDx00x00x00ax00x86abc123ax86def234ax86ghi345ax86jkl456ax86mno567ax88pqrst678tx89uvwxyz789nxffx0bx00x8bxcar xb8x1c'x7f" replace
             * Result: OK
             */
            restoreResult = jedis.restore("bigboxlist", 0, dumpResult, RestoreParams.restoreParams().replace());

            System.out.println("Command: restore bigboxlist 0 "\x12\x01\x02@DD\x00\x00\x00\a\x00\x86abc123\a\x86def234\a\x86ghi345\a\x86jkl456\a\x86mno567\a\x88pqrst678\t\x89uvwxyz789\n\xff\x0b\x00\x8b\xca\r \xb8\x1c'\x7f" replace | Result: " + restoreResult);

            /**
             * Check the list value
             * It has old values
             *
             * Command: lrange bigboxlist 0 -1
             * Result:
             *      1) "abc123"
             *      2) "def234"
             *      3) "ghi345"
             *      4) "jkl456"
             *      5) "mno567"
             *      6) "pqrst678"
             *      7) "uvwxyz789"
             */
            List<String> lrangeResult = jedis.lrange("bigboxlist", 0, -1);

            System.out.println("Command: lrange bigboxlist 0 -1 | Result: " + lrangeResult.toString());

            /**
             * Add data to set
             *
             * Command: sadd bigboxset 6218014406 1619751279 7858399611 7595030997 9982317472
             * Result: (integer) 5
             */
            long saddResult = jedis.sadd("bigboxset", "6218014406", "1619751279", "7858399611", "7595030997", "9982317472");

            System.out.println("Command: sadd bigboxset 6218014406 1619751279 7858399611 7595030997 9982317472 | Result: " + saddResult);

            /**
             * Dump set data
             *
             * Command: dump bigboxset
             * Result: "x0b0bx00x00x00x05x00x00x00oqx8b`x00x00x00x00xc6^x9frx01x00x00x00xd5xf9xb2xc4x01x00x00x00{xa9exd4x01x00x00x00xa0x13xfeRx02x00x00x00x0bx00xa1,xe09x13xf0fxe5"
             */
            dumpResult = jedis.dump("bigboxset");

            System.out.println("Command: dump bigboxset | Result: " + new String(dumpResult, StandardCharsets.UTF_8));

            /**
             * Delete set
             *
             * Command: del bigboxset
             * Result: (integer) 1
             */
            deleteResult = jedis.del("bigboxset");

            System.out.println("Command: del bigboxset | Result: " + deleteResult);

            /**
             * Restore set data
             *
             * Command: restore bigboxset 0 "x0b0bx00x00x00x05x00x00x00oqx8b`x00x00x00x00xc6^x9frx01x00x00x00xd5xf9xb2xc4x01x00x00x00{xa9exd4x01x00x00x00xa0x13xfeRx02x00x00x00x0bx00xa1,xe09x13xf0fxe5"
             * Result: OK
             */
            restoreResult = jedis.restore("bigboxset", 0, dumpResult);

            System.out.println("Command: restore bigboxset 0 "\x0b0\b\x00\x00\x00\x05\x00\x00\x00oq\x8b`\x00\x00\x00\x00\xc6^\x9fr\x01\x00\x00\x00\xd5\xf9\xb2\xc4\x01\x00\x00\x00{\xa9e\xd4\x01\x00\x00\x00\xa0\x13\xfeR\x02\x00\x00\x00\x0b\x00\xa1,\xe09\x13\xf0f\xe5" | Result: " + restoreResult);

            /**
             * Check set data
             *
             * Command: smembers bigboxset
             * Result:
             *      1) "1619751279"
             *      2) "6218014406"
             *      3) "7595030997"
             *      4) "7858399611"
             *      5) "9982317472"
             */
            Set<String> smemebersResult = jedis.smembers("bigboxset");

            System.out.println("Command: smembers bigboxset | Result: " + smemebersResult.toString());

            /**
             * Set hash values
             *
             * Command: hset bigboxhash secret1 3622625569 secret2 5251979648 secret3 7686908197 secret4 0645904521 secret5 8386817336 secret6 2706180686 secret7  7252297879
             * Result: (integer) 7
             */
            Map<String, String> hashData  = new HashMap<>() {{
                put("secret1", "3622625569");
                put("secret2", "5251979648");
                put("secret3", "7686908197");
                put("secret4", "0645904521");
                put("secret5", "8386817336");
                put("secret6", "2706180686");
                put("secret7", "7252297879");
            }};
            long hsetResult = jedis.hset("bigboxhash", hashData);

            System.out.println("Command: hset bigboxhash secret1 3622625569 secret2 5251979648 secret3 7686908197 secret4 0645904521 secret5 8386817336 secret6 2706180686 secret7  7252297879 | Result: " + hsetResult);

            /**
             * Dump hash
             *
             * Command: dump bigboxhash
             * Result: "x10xc3@i@x8ex13x8ex00x00x00x0ex00x87secret1bxf4!xe1xecxd7 x12x01x00txa0x12a2bxf4x80xd9n9x01xe0x02x12x063bxf4%xe9,xcaxe0x03x12r4bx8a0645904521x0bxa0:x065bxf48xadxe4xf3xe0x03'x066bxf4NnMxa1 Lxe0x00`x067bxf4x97HExb0@%x01txffx0bx00xeax15x94x95Kxf0xd7R"
             */
            dumpResult = jedis.dump("bigboxhash");

            System.out.println("Command: dump bigboxhash | Result: " + new String(dumpResult, StandardCharsets.UTF_8));

            /**
             * Add new item to hash
             *
             * Command: hset bigboxhash new1 "new item 1" new2 "new item 2"
             * Result: (integer) 2
             */
            hashData  = new HashMap<>() {{
                put("new1", "new item 1");
                put("new2", "new item 2");
            }};
            hsetResult = jedis.hset("bigboxhash", hashData);

            System.out.println("Command: hset bigboxhash new1 "new item 1" new2 "new item 2" | Result: " + hsetResult);

            /**
             * Check hash
             *
             * Command: hgetall bigboxhash
             * Result:
             *      1) "secret1"
             *      2) "3622625569"
             *      3) "secret2"
             *      4) "5251979648"
             *      5) "secret3"
             *      6) "7686908197"
             *      7) "secret4"
             *      8) "0645904521"
             *      9) "secret5"
             *      10) "8386817336"
             *      11) "secret6"
             *      12) "2706180686"
             *      13) "secret7"
             *      14) "7252297879"
             *      15) "new1"
             *      16) "new item 1"
             *      17) "new2"
             *      18) "new item 2"
             */
            Map<String, String> hgetallResult = jedis.hgetAll("bigboxhash");

            System.out.println("Command: hgetall bigboxhash | Result: " + hgetallResult.toString());

            /**
             * Restore hash with replace using a older dump data
             *
             * Command: restore bigboxhash 0 "x10xc3@i@x8ex13x8ex00x00x00x0ex00x87secret1bxf4!xe1xecxd7 x12x01x00txa0x12a2bxf4x80xd9n9x01xe0x02x12x063bxf4%xe9,xcaxe0x03x12r4bx8a0645904521x0bxa0:x065bxf48xadxe4xf3xe0x03'x066bxf4NnMxa1 Lxe0x00`x067bxf4x97HExb0@%x01txffx0bx00xeax15x94x95Kxf0xd7R" replace
             * Result: OK
             */
            restoreResult = jedis.restore("bigboxhash", 0, dumpResult, RestoreParams.restoreParams().replace());

            System.out.println("Command: restore bigboxhash 0 "\x10\xc3@i@\x8e\x13\x8e\x00\x00\x00\x0e\x00\x87secret1\b\xf4!\xe1\xec\xd7 \x12\x01\x00\t\xa0\x12\a2\b\xf4\x80\xd9\n9\x01\xe0\x02\x12\x063\b\xf4%\xe9,\xca\xe0\x03\x12\r4\b\x8a0645904521\x0b\xa0:\x065\b\xf48\xad\xe4\xf3\xe0\x03'\x066\b\xf4N\nM\xa1 L\xe0\x00`\x067\b\xf4\x97HE\xb0@%\x01\t\xff\x0b\x00\xea\x15\x94\x95K\xf0\xd7R" replace | Result: " + restoreResult);

            /**
             * Check hash data
             * It has the data form the dump
             *
             * Command: hgetall bigboxhash
             * Result:
             *          1) "secret1"
             *          2) "3622625569"
             *          3) "secret2"
             *          4) "5251979648"
             *          5) "secret3"
             *          6) "7686908197"
             *          7) "secret4"
             *          8) "0645904521"
             *          9) "secret5"
             *          10) "8386817336"
             *          11) "secret6"
             *          12) "2706180686"
             *          13) "secret7"
             *          14) "7252297879"
             */
            hgetallResult = jedis.hgetAll("bigboxhash");

            System.out.println("Command: hgetall bigboxhash | Result: " + hgetallResult.toString());

            /**
             * Wrong data example
             *
             * Command: restore somekey 0 "some random and wrong data, not serialized properly"
             * Result: (error) ERR DUMP payload version or checksum are wrong
             */
            try {
                restoreResult = jedis.restore("somekey", 0, "some random and wrong data, not serialized properly".getBytes(StandardCharsets.UTF_8));

                System.out.println("Command: restore somekey 0 "some random and wrong data, not serialized properly" | Result: " + restoreResult);
            } catch (Exception e) {
                System.out.println("Command: restore somekey 0 "some random and wrong data, not serialized properly" | Error: " + e.getMessage());
            }

            /**
             * Existing key without replace
             *
             * Command: restore bigboxstr 0 "x00"My string value saved in a Big Boxx0bx00axbf3nx83Mxdd!"
             * Result: (error) BUSYKEY Target key name already exists.
             */
            try {
                restoreResult = jedis.restore("bigboxstr", 0, "\x00\"My string value saved in a Big Box\x0b\x00\a\xbf3n\x83M\xdd!".getBytes(StandardCharsets.UTF_8));

                System.out.println("Command: restore bigboxstr 0 "\x00\"My string value saved in a Big Box\x0b\x00\a\xbf3\n\x83M\xdd!" | Result: " + restoreResult);
            } catch (Exception e) {
                System.out.println("Command: restore bigboxstr 0 "\x00\"My string value saved in a Big Box\x0b\x00\a\xbf3\n\x83M\xdd!" | Error: " + e.getMessage());
            }

        }

        jedisPool.close();
    }
}

Output:

Command: set bigboxstr "My string value saved in a Big Box" | Result: OK
Command: dump bigboxstr | Result:  "My string value saved in a Big Box �3
�M�!
Command: del bigboxstr | Result: 1
Command: restore bigboxstr 0 "x00"My string value saved in a Big Boxx0bx00axbf3nx83Mxdd!" | Result: OK
Command: get bigboxstr | ResultMy string value saved in a Big Box

Command: rpush bigboxlist abc123 def234 ghi345 jkl456 mno567 pqrst678 uvwxyz789 | Result: 7
Command: dump bigboxlist | Result: @DD    �abc123�def234�ghi345�jkl456�mno567�pqrst678	�uvwxyz789
 �'
Command: rpush bigboxlist "extra item 1" "extra item 2" | Result: 9
Command: restore bigboxlist 0 "x12x01x02@DDx00x00x00ax00x86abc123ax86def234ax86ghi345ax86jkl456ax86mno567ax88pqrst678tx89uvwxyz789nxffx0bx00x8bxcar xb8x1c'x7f" replace | Result: OK
Command: lrange bigboxlist 0 -1 | Result: [abc123, def234, ghi345, jkl456, mno567, pqrst678, uvwxyz789]

Command: sadd bigboxset 6218014406 1619751279 7858399611 7595030997 9982317472 | Result: 5
Command: dump bigboxset | Result:       oq�`    �^�r   ����   {�e�   ��R    �,�9�f�
Command: del bigboxset | Result: 1
Command: restore bigboxset 0 "x0b0bx00x00x00x05x00x00x00oqx8b`x00x00x00x00xc6^x9frx01x00x00x00xd5xf9xb2xc4x01x00x00x00{xa9exd4x01x00x00x00xa0x13xfeRx02x00x00x00x0bx00xa1,xe09x13xf0fxe5" | Result: OK
Command: smembers bigboxset | Result: [9982317472, 7858399611, 1619751279, 7595030997, 6218014406]

Command: hset bigboxhash secret1 3622625569 secret2 5251979648 secret3 7686908197 secret4 0645904521 secret5 8386817336 secret6 2706180686 secret7  7252297879 | Result: 7
Command: dump bigboxhash | Result: �@h@��    �secret�N
�0645904521�'�8����'��
9��%�,���!��� _ 	� �h��j?�
Command: hset bigboxhash new1 "new item 1" new2 "new item 2" | Result: 2
Command: hgetall bigboxhash | Result: {new2=new item 2, new1=new item 1, secret6=2706180686, secret7=7252297879, secret4=0645904521, secret5=8386817336, secret2=5251979648, secret3=7686908197, secret1=3622625569}
Command: restore bigboxhash 0 "x10xc3@i@x8ex13x8ex00x00x00x0ex00x87secret1bxf4!xe1xecxd7 x12x01x00txa0x12a2bxf4x80xd9n9x01xe0x02x12x063bxf4%xe9,xcaxe0x03x12r4bx8a0645904521x0bxa0:x065bxf48xadxe4xf3xe0x03'x066bxf4NnMxa1 Lxe0x00`x067bxf4x97HExb0@%x01txffx0bx00xeax15x94x95Kxf0xd7R" replace | Result: OK
Command: hgetall bigboxhash | Result: {secret6=2706180686, secret7=7252297879, secret4=0645904521, secret5=8386817336, secret2=5251979648, secret3=7686908197, secret1=3622625569}

Command: restore somekey 0 "some random and wrong data, not serialized properly" | Error: ERR DUMP payload version or checksum are wrong

Command: restore bigboxstr 0 "x00"My string value saved in a Big Boxx0bx00axbf3nx83Mxdd!" | Error: BUSYKEY Target key name already exists.

NOTES

  • Use method “restore” from Jedis package.
  • Signatures of the methods are-
    public String restore(final String key, final long ttl, final byte[] serializedValue)
    public String restore(final String key, final long ttl, final byte[] serializedValue, final RestoreParams params)

Output:

NOTES

  • Use the function “restore” from the package node-redis.
  • Here is the signature of the method –
    restore(key: RedisCommandArgument, ttl: number, serializedValue: RedisCommandArgument, options?: RestoreOptions)
  • Definition of the RedisOptions is-
    interface RestoreOptions {
        REPLACE?: true;
        ABSTTL?: true;
        IDLETIME?: number;
        FREQ?: number;
    }

Output:

NOTES

  • Use the methods “ListLeftPop“, “ListRightPop” from StackExchange.Redis.
  • Signature of the method for LMPOP is as below –
    ListPopResult ListLeftPop(RedisKey[] keys, long count, CommandFlags flags = CommandFlags.None)
    ListPopResult ListRightPop(RedisKey[] keys, long count, CommandFlags flags = CommandFlags.None)

Output:

NOTES

  • Use the method “lmpop” of predis.
  • Signature of the method is-
    lmpop(array $keys, string $modifier = ‘left’, int $count = 1): array|null
  • Make sure to pass the modifier left/right in lower case.

Output:

NOTES

  • Use method “lmpop” from redis-py.
  • Signature of the method is –
    def lmpop(self, num_keys: int, *args: List[str], direction: str, count: Optional[int] = 1,) -> Union[Awaitable[list], list]

Output:

NOTES

  • Use method “lmpop” from the redis-rb.
  • Signature of the method is-
    def lmpop(*keys, modifier: “LEFT”, count: nil)

Source Code

Use the following links to get the source code used in this article-

Related Commands

CommandDetails
DUMP DUMP Command Details
CONFIG CONFIG Command Details

Leave a Comment


The reCAPTCHA verification period has expired. Please reload the page.