Skip to content

Commit f2ffba5

Browse files
authored
Enhance documentation for REPLCONF GETACK offset tracking
Clarify the offset tracking process for REPLCONF GETACK commands, including examples and expected responses.
1 parent 88c9e23 commit f2ffba5

File tree

1 file changed

+31
-34
lines changed

1 file changed

+31
-34
lines changed
Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,29 @@
11
In this stage, you'll extend your `REPLCONF GETACK` implementation to respond with the number of bytes of commands processed by the replica.
22

3+
### ACKs (Recap)
4+
5+
As a recap, a master uses ACKs to verify that its replicas are in sync with it and haven't fallen behind. Each ACK contains an offset — the number of bytes of commands processed by the replica.
6+
37
### Offset tracking
48

5-
As we saw in previous stages, when a replica receives a command from the master, it processes the command and updates its state accordingly. In addition to processing commands, the replica also keeps a running count of the number of bytes of commands it has processed.
6-
7-
This count is called the "offset". When a master sends a `REPLCONF GETACK` command to a replica, the replica is expected to respond with `REPLCONF ACK <offset>`. The returned `<offset>` should only include the number of bytes of commands processed **before** receiving the `REPLCONF GETACK` command.
8-
9-
As an example:
10-
11-
- Let's say a replica connects to a master and completes the handshake.
12-
- The master then sends a `REPLCONF GETACK *` command.
13-
- The replica should respond with `REPLCONF ACK 0`.
14-
- The returned offset is 0 since no commands have been processed yet (before receiving the `REPLCONF GETACK` command)
15-
- The master then sends `REPLCONF GETACK *` again.
16-
- The replica should respond with `REPLCONF ACK 37`.
17-
- The returned offset is 37 since the first `REPLCONF GETACK` command was processed, and it was 37 bytes long.
18-
- The RESP encoding for the `REPLCONF GETACK` command looks like this: `*3\r\n$8\r\nreplconf\r\n$6\r\ngetack\r\n$1\r\n*\r\n` (that's 37 bytes long)
19-
- The master then sends a `PING` command to the replica (masters do this periodically to notify replicas that the master is still alive).
20-
- The replica must silently process the `PING` command and update its offset. It should not send a response back to the master.
21-
- The master then sends `REPLCONF GETACK *` again (this is the third REPLCONF GETACK command received by the replica)
22-
- The replica should respond with `REPLCONF ACK 88`.
23-
- The returned offset is 88 (37 + 37 + 14)
24-
- 37 for the first `REPLCONF GETACK` command
25-
- 37 for the second `REPLCONF GETACK` command
26-
- 14 for the `PING` command
27-
- Note that the third `REPLCONF GETACK` command is not included in the offset, since the value should only include the number of bytes of commands processed **before** receiving the current `REPLCONF GETACK` command.
9+
A replica keeps its offset updated by tracking the total byte size of every command received from its master. This includes both write commands (like `SET`, `DEL`) and non-write commands (like `PING`, `REPLCONF GETACK *`).
10+
11+
After processing the received command (e.g., `["SET", "foo", "bar]`), it adds the full RESP array byte length to its running offset.
12+
13+
An important rule for this process is that the offset should only include commands processed **before** the current `REPLCONF GETACK *` request.
14+
15+
For example:
16+
17+
- A replica connects, completes the handshake, and the master sends `REPLCONF GETACK *`.
18+
- The replica responds with `REPLCONF ACK 0` since no commands had been processed before this request.
19+
- Next, the master sends another `REPLCONF GETACK *`.
20+
- The replica responds with `REPLCONF ACK 37`, because the previous `REPLCONF` command consumed 37 bytes.
21+
- The master then sends a `PING` command.
22+
- The replica silently processes it, increments its offset by 14, and sends no response.
23+
- The next `REPLCONF GETACK *` arrives.
24+
- The replica responds with `REPLCONF ACK 88` — that’s 37 (for the first `REPLCONF`), +37 (for the second `REPLCONF`), +14 (for the `PING`).
25+
26+
Notice that the current `GETACK` request itself is not included in the offset value.
2827

2928
### Tests
3029

@@ -39,16 +38,16 @@ Just like in the previous stages, your replica should complete the handshake wit
3938
The master will then propagate a series of commands to your replica. These commands will be interleaved with `REPLCONF GETACK *` commands.
4039

4140
```bash
42-
REPLCONF getack * # expecting REPLCONF ACK 0, since 0 bytes have been processed
41+
REPLCONF getack * # expect: REPLCONF ACK 0
4342

44-
ping # master sending a ping command to notify the replica that it's still alive
45-
REPLCONF getack * # expecting REPLCONF ACK 51
46-
# 51 = 37 (for the first REPLCONF command) + 14 (for the ping command)
43+
PING # replica processes silently
44+
REPLCONF getack * # expect: REPLCONF ACK 51
45+
# 51 = 37 (first REPLCONF) + 14 (PING)
4746

48-
set foo 1 # propagated from master to replica
49-
set bar 2 # propagated from master to replica
50-
REPLCONF getack * # expecting REPLCONF ACK 146
51-
# 146 = 51 + 37 (for the second REPLCONF command) + 29 (for the first set command) + 29 (for the second set command)
47+
SET foo 1 # replica processes silently
48+
SET bar 2 # replica processes silently
49+
REPLCONF getack * # expect: REPLCONF ACK 146
50+
# 146 = 51 + 37 (second REPLCONF) + 29 (SET foo) + 29 (SET bar)
5251
```
5352

5453
Your replica must calculate and return the exact offset at each step.
@@ -57,6 +56,4 @@ Your replica must calculate and return the exact offset at each step.
5756

5857
- The offset should only include the number of bytes of commands processed **before** receiving the current `REPLCONF GETACK` command.
5958
- Although masters don't propagate `PING` commands when received from clients (since they aren't "write" commands), they may send `PING` commands to replicas to notify replicas that the master is still alive.
60-
- Replicas should update their offset to account for **all** commands propagated from the master, including `PING` and `REPLCONF` itself.
61-
- The response should be encoded as a [RESP array](https://redis.io/docs/latest/develop/reference/protocol-spec/#arrays), like
62-
this: `*3\r\n$8\r\nREPLCONF\r\n$3\r\nACK\r\n$3\r\n154\r\n`.
59+
- The response should be encoded as a [RESP array](https://redis.io/docs/latest/develop/reference/protocol-spec/#arrays).

0 commit comments

Comments
 (0)