An intro to writing native DB driver in Scala

Speaking directly to Cassandra over its native protocol using ZIO & Scodec

Setting up the stage

Bits, bytes, and codecs

[int]             A 4 bytes integer
[long] A 8 bytes integer
...

Protocol envelope

0         8        16        24        32         40
+---------+---------+---------+---------+---------+
| version | flags | stream | opcode |
+---------+---------+---------+---------+---------+
| length |
+---------+---------+---------+---------+
| |
. ... body ... .
. .
. .
+----------------------------------------

Version

0         8
+---------+
| version |
+---------+

Flags

8        16 
-+---------+-
| flags |
-+---------+-
8  flags byte 16 
+-------------+
x x x 1 1 1 1 1
| | | | ^- compression flag
| | | ^--- tracing flag
| | ^----- custom payload flag
| ^------- warning flag
^--------- use beta features flag
^ ^ ^ ---------- unused in current Cassandra

Stream

16        24        32 
-+---------+---------+-
| stream |
-+---------+---------+-

OpCode

32         40
-+---------+
| opcode |
-+---------+

Length

0         8        16        24        32
+---------+---------+---------+---------+
| length |
+---------+---------+---------+---------+
| |
. ... body ... .
. .
. .
+----------------------------------------

Header

Requests and their encoding

Responses and their decoding

Data over network

BitVectorSocket

00 00 00 0b

MessageSocket

Sending real requests to Cassandra

request: OptionsRequest(4,FlagsPayload(10),StreamId(1))
response: SupportedResponse(
Header(Version(Response,4),FlagsPayload(0),StreamId(1),OpSupported),
Map(PROTOCOL_VERSIONS -> List(3/v3, 4/v4, 5/v5-beta),
COMPRESSION -> List(snappy, lz4),
CQL_VERSION -> List(3.4.4)))
-------------------------------------------------------
request: StartupRequest(4,FlagsPayload(10),StreamId(2),
Map(CQL_VERSION -> 3.0.0))
response: ReadyResponse(Header(Version(Response,4),FlagsPayload(0),StreamId(2),OpReady))

Next Steps

Conclusion

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store