Build your own database

426 pointsposted 12 hours ago
by nansdotio

64 Comments

tombert

3 hours ago

I more or less built my own database in Erlang a few months ago. I say "more or less" because I did use Bitcask as the underlying store, and I used the riak_core libraries initially, but I did handle replication and different fault tolerance techniques on my own.

It was actually very fun; a key-value database is something that can be any level of difficulty that you want. If you want a simple KV "database", you could just serialize and deserialize a JSON string all the time, or write a protobuf, but there is of course no limit to the level of complexity.

I use the JSON example because that was actually how I started; I was constantly serializing and deserializing JSON with base64 binary encoded strings, just because it was easy and good enough, and over the course of building the project I ended up making a proper replicated database. I even had a very basic "query language" to handle some basic searches.

skeptrune

10 hours ago

I love the design and examples in this post. Easy to read for sure.

Exercises like this also seem fun in general. It's a real test of how much you know to start anything from scratch.

ashleyn

9 hours ago

I was tempted to knee-jerk dismiss this as "don't write your own database, don't even use a KV database, just use SQL". And then I remembered the only reason I'd say this is because I went through designing my own DB or using KV databases just to avoid SQL...only to realise i was badly reinventing SQL. It could be worth the lesson.

kevinqi

9 hours ago

my only minor critique is using lorem ipsum examples. It tends to make me want to gloss over instead of reading; I prefer seeing realistic data. other than that, it's a really cool post

WD-42

8 hours ago

Was going to post the same thing. Lorem Ipsum makes the data too hard to distinguish. I get that due to the dynamic nature of the examples the text needed to be generated, but Latin isn't the best choice IMO.

Otherwise great article, thank you!

doublerabbit

5 hours ago

It's the same for me when foo and bar are used as examples.

cube2222

10 hours ago

I clicked through a couple of the articles in the OP, and I must say, the design and animations are extremely pretty!

Kudos for that!

jumploops

7 hours ago

“LSM trees are the underlying data structure used for [..] DynamoDB, and they have proven to perform really well at scale [..] 80 million requests per second!”

This is a tad bit misleading, as the LSM is used for the node-level storage engine, but doesn’t explain how the overall distributed system scales to 80 million rps.

iirc the original Dynamo paper used BerkeleyDB (b-tree or LSM), but the 2012 paper shifted to a fully LSM-based engine.

exdeejay_

8 hours ago

The first example in the "Sorting in Practice" section appears to be broken. The text makes it seem like the list should be sorted in-memory and then written to disk sorted, but the example un-sorts the list when it's written to disk.

Edit: the flush example (2nd one) in the recap section does the same thing, when the text says that the records are supposed to be written to the file in sorted order.

chrisallick

9 hours ago

if author is reading, can you add an rss feed to your site? i want to add to feedly.

jamwil

4 hours ago

I was quite excited to add this one! And shocked to not find it, given the overall high production quality.

vladpowerman

8 hours ago

Great read. I’ve been modeling developer activity as a time series key value system where each developer is a key and commits are values. Faced the same issues: logs grow fast, indexes get heavy, range queries slow down. How do you decide what to drop when compacting segments? Balancing freshness and retention is tricky.

0xb0565e486

8 hours ago

I have spending the last ~4 weeks writing a triple store!

I wish this came out earlier, there are a few insights in there that took me me a while to understand :)

myth_drannon

9 hours ago

I also recommend this free online book to build a database https://build-your-own.org/database/

bionsystem

8 hours ago

I remember an article here, maybe a year ago, where somebody showed some database concepts from bash examples (like "write your db in bash"), but I can't find it anywhere, does anybody have it ?

DiabloD3

9 hours ago

It looks like it got hugged to death already.

winrid

9 hours ago

Needs a faster database

constantcrying

9 hours ago

I absolutely love this "first principles" approach of explaining a topic. You can really go through this and at each time understand what problem needs to be solved and what other problems this introduces, until you get at a reasonably satisfying solution.

keybored

9 hours ago

Part of the reason why I'm not a "maker" is because my mind gets ahead of me with all the things that I would need to do in order to do things properly. So the article starts out interesting and then gets more and more, well, not exactly stressful but I get a bit weary by it.

Not that I would aspire to implement a general-purpose database. But even smaller tasks can make my mind spin too much.

browningstreet

9 hours ago

I don't disagree with your take in general, but I do think it's different reading about minutiae than being invested in it. If you actually are curing these requirements it's probably quite engaging. If not, the eyes and mind start to gloss over them.

As a different example: I'm moving this week. I've known I'm moving for a while. Thinking about moving -- and all the little things I have to do -- is way more painful than doing them. Thinking about them keeps me up at night, getting through my list today is only fractionally painful.

I'm also leveling up a few aspects of my "way of living" in the midst of all this, and it'd be terribly boring to tell others about it, but when next Monday comes.. it'll be quite sweet indeed.

keybored

9 hours ago

> As a different example: I'm moving this week. I've known I'm moving for a while. Thinking about moving -- and all the little things I have to do -- is way more painful than doing them. Thinking about them keeps me up at night, getting through my list today is only fractionally painful.

this sounds familiar... :)

nawgz

8 hours ago

Have you considered if you have ADHD?

orliesaurus

8 hours ago

am i the only one who IS a huge fan of this blogpost layout

FpUser

9 hours ago

>Problem. How do we store data persistently and then efficiently look it up later?"

I would say without transactions it is not a database yet from a practical standpoint.

dangoodmanUT

9 hours ago

I think a lot of databases would disagree

socketcluster

2 hours ago

You can implement two-phase commit instead. It requires a bit of additional planning in terms of data management but I actually find it much more elegant and it scales better. DB transactions are expensive and unnecessarily complicated.

You can have a really simple two-phase commit system where you initially mark all records as 'pending' and then update them as 'settled' once all the necessary associated rows have been inserted into their respective tables. You can record a timestamp so that you know where to resume the settlement process from. I once had multiple processes doing settlement in parallel by hashing the ids to map to specific processes so it scales really well.

FpUser

9 hours ago

You might be on to something here ;)

alecco

9 hours ago

But they are web scale!

saxelsen

8 hours ago

Nice interactivity, but this is taken straight from the Designing Data-Intensive Applications. Literally all the content here is an interactive version of chapter 3.

Maybe give credit?

tomhow

5 hours ago

Thanks, we've added this to the thread's top text.

4ndrewl

11 hours ago

> Databases were made to solve one problem:

>

> "How do we store data persistently and then efficiently look it up later?"

Isn't that two problems?

nonethewiser

9 hours ago

It's amusing to me that this is really quite a pedantic observation yet it's driving very earnest engagement from hackernews. Myself included. Absolutely nothing in this article is riding on if its 1 or 2 problems - it's an aside at best. Yet I'm still trying to think through if it's 1 or 2. I mean, the "and" is right there - that clearly suggests two. It's almost comical even, to say "Here is one problem: X and Y." Yet in another way it seems like 2 sides of the same coin.

I guess there is a rather fine line between philosophy and pedantry.

Maybe we can think about it from another angle. If they are 2 problems databases were designed to solve, then that means this is a problem databases were designed to solve: storing data persistently.

Is that really a problem database were designed to solve? Not really. We had that long before databases. It was already solved. It's a pretty fundamental computer operation. Isn't it fair to say this is one thing? "Storing data so it can be retrieved efficiently."

i_k_k

10 hours ago

I always wanted to ship a write-only database. Lightning fast.

elygre

10 hours ago

Back in the 80s a professor at our college got a presentation on the concept of «write-only memory» accepted for some symposium.

Good times.

pcdevils

10 hours ago

Pretty much how eventstoredb works. Deleting data fully only happens at scavenge which rewrites the data files.

hxtk

9 hours ago

I think it was a joke. It sounds like you read it as append-only, like most LSM tree databases (not rewriting files in the course of write operations), but I think GP meant it as write-only to the exclusion of reads, roughly equivalent to `echo $data > /dev/null`

archerx

10 hours ago

That would be useful for logging.

Etheryte

9 hours ago

Also useful for backups, so long as you don't need to restore.

warkdarrior

10 hours ago

If it's write-only, and no reads ever happen, one can write to /dev/null without loss of utility.

mewpmewp2

9 hours ago

It would be good for before going to sleep then.

grokgrok

10 hours ago

How do we reconstruct past memory states? That's the fundamental problem.

Efficiency of storage or retrieval, reliability against loss or corruption, security against unwanted disclosure or modification are all common concerns, and the relative values assigned to these features and others motivate database design.

kiitos

10 hours ago

> How do we reconstruct past memory states? That's the fundamental problem.

reconstructing past memory states is rarely, if ever, a requirement that needs to be accommodated in the database layer

nonethewiser

9 hours ago

Can you elaborate? That certainly seems to be what happens in a typical crud app. You have some model for your data which you persist so that it can be loaded later. Perhaps partially at times.

In another context perhaps you're ingesting data to be used in analytics. Which seems to fit the "reconstruct past memory stat" less.

mamcx

7 hours ago

You can decompose in 2 problems, because well is better, but is in fact one. Can be argued that is only this single problem:

How, in ACID way, store data that will be efficiently look it up later by a unknown number of clients and unknown access patterns, concurrently, without blocking all the participants, in a fast way?

And then add SQL (ouch!)

didip

8 hours ago

Off by 1 error is indeed a hard problem.

mrighele

9 hours ago

It is a single problem that contains two smaller problems, but the actual hard part (a third problem, if you wish) is putting them together. If you limit yourself to solve those two problems independently you won't have a (useful) database.

dayjaby

11 hours ago

Store data persistently so it can be looked up efficiently* sounds like a single problem.

SirFatty

11 hours ago

Definitely two.

SahAssar

10 hours ago

"Store data persistently" implies "it can be looked up" since if you cannot look it up it is impossible to know if it is stored persistently.

The "efficiently" part can be considered a separate problem though.

prerok

10 hours ago

Well, if you just want to store data, you can use files. Lookup is a bit tedious and inefficient.

So, if we consider that persistent storage is a solved problem, then we can say that the reason for databases was how to look up data efficiently. In fact, that is why they were invented, even if persistent storage is a prerequisite.

nonethewiser

9 hours ago

How about "store data in certain way." That sounds more like 1 problem and encompasses an even larger problem space.

pratik661

10 hours ago

This is analogous to an elevator that’s unidirectional

rzzzt

10 hours ago

One that lets people enter. We will figure out exiting later, with exiting on a different floor as a stretch goal.

whartung

8 hours ago

> Isn't that two problems?

No, that would be regexes.

235ylkj

10 hours ago

Here's a simple key-value store inspired by D.B. Cooper:

  ~/bin/cooper-db-set
  ===================
  #! /bin/bash

  key="$1"
  value="$2"

  echo "${key}:${value}" >> /dev/null


  ~/bin/cooper-db-get
  ===================
  #! /bin/bash

  key="$1"
  </dev/null awk -F: -v key="$key" '$1 == key {result = $2} END {print result}'

MathMonkeyMan

9 hours ago

/dev/null is persistent across restarts and cache friendly, so it's got you covered.