Two nice databases for Ruby

String of glass beads

String of glass beads, ca. 400, Korea

Doing small projects with Ruby, there are two data­bases I find myself com­ing back to again and again, so I wanted to men­tion them here.


The first is YAML::Store, which is built into Ruby. You’ve had it this whole time! For me, a char­ac­ter­is­tic use of YAML::Store looks some­thing like this:

require "yaml/store"

db ="zine-orders.yaml")

db.transaction do
  unless db[some_order_id]["printed"]
    db[some_order_id]["printed"] = true

In addi­tion to its over­all simplicity, YAML::Store has two really nice properties.

First, the data­base is, as advertised, just a YAML file, which means you can view its con­tents in a text edi­tor and even make changes if you like! Most peo­ple read­ing this post will be famil­iar with YAML; it’s totally simple, even minimal:

  recipient_name: Sherlock Holmes
  shipping_address: 221B Baker Street
  printed: false

I have found the abil­ity to eas­ily spot-check and edit the data­base in this way extremely useful.

Second, even though it’s so simple, YAML::Store still gives you the super­power of wrap­ping changes in a “transaction,” so if some­thing fails mid­way through — as it, uh, often does in these lit­tle scripts I write, espe­cially as I’m devel­op­ing them — the changes won’t be committed. In the exam­ple above, if my call to print_order(some_order_id) fails because, say, I forgot to plug in the label printer, the trans­ac­tion will bomb out and the order’s "printed" field will not be marked true. It’s all very basic, but/and that’s often exactly the level of com­plex­ity that I need.


I’m not going to explain this one in detail, because it is very well-explained on its own home page, but I feel like not enough peo­ple know about Daybreak, the “simple and very fast key value store” cre­ated by pro­gram­mers at ProPublica. YAML::Store’s one great weak­ness is that it’s very slow; for my purposes, that’s usu­ally fine, but/and when I need some­thing faster, I turn to Day­break.

The down­side is: you can’t open the data­base and view its con­tents manually.

The upside is: it is very fast.

Daybreak and YAML::Store are both single-file data­bases, so there’s none of the has­sle of set­ting up a data­base server, con­nect­ing to it, etc., etc. For a cer­tain scale of project — which includes basi­cally all of mine — they are really much bet­ter choices than MySQL, Redis, or any­thing like them.

May 2020, Oakland