Skip to content

Commit 7a9ba9f

Browse files
committed
Update course definition with detailed Kafka wire protocol explanation and stages.
1 parent 2dc6849 commit 7a9ba9f

File tree

1 file changed

+44
-16
lines changed

1 file changed

+44
-16
lines changed

course-definition.yml

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -74,25 +74,48 @@ stages:
7474
name: "Send Correlation ID"
7575
difficulty: easy
7676
description_md: |-
77-
In this stage, you'll start implementing the Kafka wire protocol.
78-
The response structure is as follows:
77+
In this stage, you'll respond to a request with a correlation ID.
7978
80-
ResponseHeader: V0
79+
### Kafka wire protocol
8180
82-
ResponseHeader:
83-
CorrelationId: INT32
81+
The [Kafka wire protocol](https://kafka.apache.org/protocol.html) is a binary protocol used by Kafka brokers and clients to communicate with each other.
8482
85-
The response is structured as follows:
83+
All communication over this protocol happens in request-response pairs. A client sends a request to the broker, and the broker responds with a response.
8684
87-
```
88-
+---------------+--------------------+------------------+
89-
| MessageLength | ResponseHeader | ResponseBody |
90-
+---------------+--------------------+------------------+
91-
| INT32 | RESPONSE_HEADER_V0 | RESPONSE_BODY_V3 |
92-
+---------------+--------------------+------------------+
93-
```
85+
You'll implement support for this protocol over the next few stages. We'll break down the protocol into smaller pieces and implement them one by one.
86+
87+
### Kafka requests
88+
89+
Examples of some commonly used Kafka requests:
90+
91+
- `APIVersions`, used to get the supported API versions by the broker
92+
- `Fetch`, used to get data from the broker
93+
- `Produce`, used to send data to the broker
94+
95+
We'll learn about these in detail later, for now we'll focus on the protocol format itself.
9496
95-
In general each response in the Kafka wire protocol starts with a INT32 containing the length of the entire message, followed by the ResponseHeader and then the ResponseBody.
97+
### Request/Response format
98+
99+
As described in [the docs](https://kafka.apache.org/protocol.html#protocol_common), each request and response in the Kafka wire protocol starts with 4 bytes that
100+
describe the length of the entire message (this includes the header & body), followed by the message. This is true for both requests and responses.
101+
102+
The first 4 bytes are encoded as an int32 (4 bytes, big-endian / network byte order).
103+
104+
For example, if the length of the message is 10, the first 4 bytes (in hexadecimal) will be `00 00 00 0a` (`0a` is the hexadecimal representation of 10).
105+
106+
### Response format
107+
108+
Each response to a Kafka request contains a header and a body. The header format is the same across all request types, but the body format varies per request.
109+
110+
The header format is as follows:
111+
112+
- `correlation_id`: An integer that uniquely identifies the request (the client sends this value in the request)
113+
- This is an "INT32" according to the Kafka wire protocol
114+
- It is a signed integer, encoded using 4 bytes in the response (big-endian / network byte order)
115+
- `tag_buffer`: An array of optional "tagged fields"
116+
- This can be ignored for now, they're [optional tagged fields](https://cwiki.apache.org/confluence/display/KAFKA/KIP-482%3A+The+Kafka+Protocol+should+Support+Optional+Tagged+Fields) used to introduce additional features over time.
117+
118+
Note that as mentioned in the previous section, the header is preceded by 4 bytes that describe the length of the entire message (header and body combined).
96119
97120
### Tests
98121
@@ -102,10 +125,15 @@ stages:
102125
$ ./your_program.sh
103126
```
104127
105-
It'll then try to connect to your server. It will then send a `APIVersions` request. You don't need to implement the logic to parse this request yet, you need to just send the header with a hardcoded correlation ID.
128+
It'll then connect to your server on port 9092 and send a `APIVersions` request. You don't need to implement the logic to parse this request yet, in this stage the tester
129+
will only assert that your server sends back a response with a hardcoded correlation ID of `7` (that's `00 00 00 07` in hexadecimal).
106130
107-
In this stage, you don't need to add the actual message length, just send a INT32 with any value. And hardcode the CorrelationId to `7`.
131+
The tester won't validate the first 4 bytes of your response (the "message length") in this stage.
132+
133+
### Notes
108134
135+
- Since the response format is currently incomplete, the first 4 bytes of your response (the "message length") will not be validated in this stage. We'll come back to this in later stages.
136+
- In this stage you're expected to hardcode the correlation ID as `7` in your response. We'll get to reading this value from the request in later stages.
109137
marketing_md: |-
110138
In this stage, you'll start implementing the ResponseHeader.
111139

0 commit comments

Comments
 (0)