11 Persistence: key-value databases
This chapter covers
- Designing raw key-value database subsystem
- Introducing type-safe interface to the key-value database subsystem
- Designing simple key-value database model
- How to write generic, extensible code with advanced type-level features
It is a rare case in which a program doesn’t interact with any external data storage, whether it’s a relational database, key-value database, filesystem, cloud storage, or something else. While the model of interaction may vary, the idea is always the same: to get access to a significant amount of data that can’t be located directly in the program’s memory.
We’ll be using a new subsystem for the framework, namely the key-value database subsystem. It will be first a low-level raw string-based interface in the form of a new Free monadic language, KVDBL
.
Figure 11.1 Framework subsystems. A new Free monadic language is added into the hierarchy.

It will serve its role to connect to the external storages, but usage of it will has a particular issue string-based interfaces are not the best ones!
I’ll show you the way to fix this problem and make the system more correct, robust, convenient, and type safe. We’ll also talk about extensibility, and in this journey, we’ll learn some useful type-level magic.