Persistent Key/Value Storage via Ruby Standard Lib
June 06 2015
Key value storage can come up for a variety of reasons when you’re writing an application. Sometimes it’s for simple configuration values while other times it can be for massive datasets shared across multiple machines.
Ruby developers have a number of options for this sort of thing.
Typical solutions include:
- Plain old Ruby Hashes (for tiny and ephemeral data)
- ENV vars
- Key/Value Caches like Redis and Memcached
- Traditional SQL and NoSQL Databases
Somewhere in between these options lies Ruby’s PStore Library. It’s a read optimized file backed key/value storage mechanism. It stores Ruby objects in a marshaled binary file format that is lighter on disk space than standard serialization methods. It also lets you store real ruby objects to disk. You’re not confined to usual 6 data types seen with other serialization formats.
When Should I Use PStore?
PStore is a good choice if:
- You need to persist object state across reloads.
- Your dataset is too big or complex for ENV vars.
- Your dataset is too small for a full blown cache server.
- You’re only using the data for one particular process (PStore is thread safe)
When NOT To Use PStore
- You need to share the data across many nodes (Use redis or Memcached)
- The data is unlikely to change during the lifetime of the application and the number of values are small (Just use ENV vars or Ruby constants)
- You need the data to be human readable (Use YAML or JSON serialization)
- You need to search the data (Use a Database)
How Do I Use It?
Basic CRUD operations in PStore are simple.
As you can see, PStore has a very straightforward API. Aside from a few options like
ultra_safe, there’s not much overhead to get started.
PStore is a simple solution that just might save you the burden of writing an object caching system by hand. For the right use case, it is very effective.
I hope you give this lesser known corner of the Ruby library a look. Questions and comments are always welcome.