# Redis Persistence #JU6

**URL:** <https://forum.codecrafters.io/t/redis-persistence-ju6/398>\
**Category:** Challenges\
**Tags:** challenge:redis\
**Created:** [May 15, 2024, 7:23am UTC](https://forum.codecrafters.io/t/redis-persistence-ju6/398 "2024-05-15T07:23:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![leBolideur](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/lebolideur/32/349_2.png) [@leBolideur](https://forum.codecrafters.io/u/leBolideur)\
**Post date:** [May 15, 2024, 7:23am UTC](https://forum.codecrafters.io/t/redis-persistence-ju6/398/1 "2024-05-15T07:23:58Z")

</div>

I’m stuck on Persistence stage 3/6

I’ve tried to return the value and the tester expects a null string

Here are my logs:

```auto
[stage-7] Running tests for Stage #7: Expiry
remote: [stage-7] $ ./spawn_redis_server.sh
remote: [stage-7] $ redis-cli SET grape apple px 100
remote: [stage-7] Sent bytes: "*5\r\n$3\r\nSET\r\n$5\r\ngrape\r\n$5\r\napple\r\n$2\r\npx\r\n$3\r\n100\r\n"
remote: [stage-7] Received bytes: "+OK\r\n"
remote: [stage-7] Received RESP value: "OK"
remote: [stage-7] Received "OK"
remote: [stage-7] Received OK at 06:58:54.229
remote: [stage-7] Fetching key "grape" at 06:58:54.229 (should not be expired)
remote: [stage-7] $ redis-cli GET grape
remote: [stage-7] Sent bytes: "*2\r\n$3\r\nGET\r\n$5\r\ngrape\r\n"
remote: [stage-7] Received bytes: "$5\r\napple\r\n"
remote: [stage-7] Received RESP value: "apple"
remote: [stage-7] Received "apple"
remote: [stage-7] Sleeping for 101ms
remote: [stage-7] Fetching key "grape" at 06:58:54.376 (should be expired)
remote: [stage-7] $ redis-cli GET grape
remote: [stage-7] Sent bytes: "*2\r\n$3\r\nGET\r\n$5\r\ngrape\r\n"
remote: [stage-7] Received bytes: "$5\r\napple\r\n"
remote: [stage-7] Received RESP value: "apple"
remote: [stage-7] Expected null bulk string ("$-1\r\n"), got BULK_STRING
remote: [stage-7] Test failed

```

And if I return a null bulk string I get

```auto
[stage-7] Running tests for Stage #7: Expiry
remote: [stage-7] $ ./spawn_redis_server.sh
remote: [stage-7] $ redis-cli SET orange strawberry px 100
remote: [stage-7] Sent bytes: "*5\r\n$3\r\nSET\r\n$6\r\norange\r\n$10\r\nstrawberry\r\n$2\r\npx\r\n$3\r\n100\r\n"
remote: [stage-7] Received bytes: "+OK\r\n"
remote: [stage-7] Received RESP value: "OK"
remote: [stage-7] Received "OK"
remote: [stage-7] Received OK at 06:57:42.720
remote: [stage-7] Fetching key "orange" at 06:57:42.721 (should not be expired)
remote: [stage-7] $ redis-cli GET orange
remote: [stage-7] Sent bytes: "*2\r\n$3\r\nGET\r\n$6\r\norange\r\n"
remote: [stage-7] Received bytes: "$-1\r\n"
remote: [stage-7] Received RESP value: NIL
remote: [stage-7] Expected simple string or bulk string, got NIL
remote: [stage-7] Test failed

```

And here’s a snippet of my code:

```zig
 const key = elems[1].bulk_string.value;

    var value: ?[]const u8 = undefined;
    if (rdb_map.* != null) {
        value = rdb_map.*.?.get(key);
    } else {
        // _ = map;
        // _ = try client.stream.write("$-1\r\n");
        // return;
        value = map.*.get(key).?.value;
    }

    if (value == null) {
        _ = try client.stream.write("$-1\r\n");
        return;
    }

    _ = try client.stream.writer().print(
        "${d}\r\n{s}\r\n",
        .{ value.?.len, value.? },
    );

```

---

<div class="post-metadata">

**Author:** ![rohitpaulk](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/rohitpaulk/32/6_2.png) [@rohitpaulk](https://forum.codecrafters.io/u/rohitpaulk)\
**Post date:** [May 15, 2024, 11:17am UTC](https://forum.codecrafters.io/t/redis-persistence-ju6/398/2 "2024-05-15T11:17:10Z")

</div>

Hey @leBolideur!

This relates to expiry - the key is being fetched twice, once before expiry and once after.

In the first set of logs you posted, before the failure it says:

> Fetching key “grape” at 06:58:54.376 (should be expired)

That’s why the expected value is a null bulk string. There was a previous test in that same log that said:

> Fetching key “grape” at 06:58:54.229 (should not be expired)

That `GET` would’ve expected a value to be returned back.

Also note that the failure here is from stage 7 (expiry), a stage you previously passed - it looks like recent code changes might’ve broken functionality in previous stages?

---

<div class="post-metadata">

**Author:** ![rohitpaulk](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/rohitpaulk/32/6_2.png) [@rohitpaulk](https://forum.codecrafters.io/u/rohitpaulk)\
**Post date:** [May 27, 2024, 9:41pm UTC](https://forum.codecrafters.io/t/redis-persistence-ju6/398/3 "2024-05-27T21:41:14Z")

</div>

Going to close this out for now, please let us know if you still need help!

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/3X/7/0/700657133935c15703e22c7c8870394f6e6dc27d.svg) [@system](https://forum.codecrafters.io/u/system)\
**Post date:** [June 3, 2024, 7:12pm UTC](https://forum.codecrafters.io/t/redis-persistence-ju6/398/4 "2024-06-03T19:12:24Z")

</div>

Closed on June 3, 2024, 7:12pm UTC

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/3X/7/0/700657133935c15703e22c7c8870394f6e6dc27d.svg) [@system](https://forum.codecrafters.io/u/system)\
**Post date:** [June 4, 2024, 2:05am UTC](https://forum.codecrafters.io/t/redis-persistence-ju6/398/5 "2024-06-04T02:05:37Z")

</div>

Note: I’ve updated the title of this post to include the stage ID (#JU6). You can learn about the stages rename here: [Upcoming change: Stages overhaul](https://forum.codecrafters.io/t/upcoming-change-stages-overhaul/375).
