DEV Community

Umairius's  Repo
Umairius's Repo

Posted on

Why databases are how they are. A tale of Clojure and Postgresql.

Databases are complex beasts, but they're essential for storing and retrieving data efficiently. Exploring the internal structure of databases can be both daunting and enlightening, as I discovered during my Clojure adventure.

Have you ever wondered why databases are so complex? As it turns out, there are good reasons for it. Databases are essential for storing and retrieving data efficiently, and they need to be able to handle a wide variety of data types and queries.

Recently, I had the opportunity to work on a small database project in Clojure. At first, I thought it would be a simple task. After all, how hard could it be to create a basic database that can store and retrieve data? But as I delved deeper into the project, I realized that databases are much more complex than I had anticipated.

One of the key things that I learned is that databases need to be able to handle multiple clients and queries simultaneously. In PostgreSQL, for example, each client is handled by a separate backend process, and shared memory is used to coordinate complex operations that require data read/write in multiple areas.

Another important aspect of databases is logging. In PostgreSQL, the Write-Ahead Log (WAL) is used to keep track of all changes made to the database, providing a form of inbuilt version control. This ensures that even if the database crashes, it can be recovered to a consistent state.

In addition, databases need to be able to handle concurrency and maintain data consistency. For example, PostgreSQL uses a multiversion concurrency control (MVCC) system, which allows multiple transactions to access the same data without interfering with each other.

As I worked on my Clojure database project, I began to realize that there were many shortcomings in my barebones implementation. For example, I didn't have a good way to handle multiple clients or to ensure data consistency. I also didn't have any logging or recovery mechanism in place.

Despite these challenges, I found the process of working on a database project to be both daunting and enlightening. By exploring the internal structure of databases, I gained a deeper appreciation for the complexity and importance of these essential tools.

So basically, things like postgresql are like the natural next step and can be considered the emergent properties in a system which envolves from simple to complex.

Top comments (1)

Collapse
 
psimondk profile image
Palle Simonsen

What as the rationale for rolling your own? Iā€™d done a few persistence dbs in lisp and even they get woefully complex as soon as you have multiple processes handling the same task.