add key value store to your app
Recently I implemented a small key value store on sqlite (in node.js). And because I liked it, I did the same for postgres. In the table I have 3 columns: key, value and timeout. There are methods for get, set. Set has an optional parameter ttl and get will respect the timeout. A simple interval removes timeout values all together. I even have a setIfNotExists
.
And the more I think about it, I think when starting any project, it is good to have such key value store in your project. no matter if you are using mongo, couchdb, sql or anything else.
I thing it is simply giving your app a place where it can store simple pieces of data. Configuration that you can change just by updating a value in db, Distributed locking and short living information that gets automatically removed.
In fact, adding a simple key value store is not even my idea. Many professional developers are using such a simple store. In Wordpress, the worlds most used system for building websites, has a key value store in a table called wp_options
. And in druapl such table is named variable
.
This keyValue stores are not there to replace redis. Because redis can store more data much faster than a keyValue table using sql. However when starting out with a new project such a store can be a valuable asset and directly put the engineers into the mindset of having such store and using it for some features without the need to have a redis server setup.
And as a keyValue store is a simple implementation, with a simple interface, you can in future easily change to a dedicated keyValue store. and you can still decide to move all data over to a keyValue database or only large amount of caching data, but not the configuration.
If you worry of having your main db also handle of key value lookups, feel free to add additional in memory cache or some query batching. The good thing is that a key value store is very simple, and so is adding such additional techniques.
My implementation is directly using sql queries. and on startup it runs create table if not exists
. But if you are using an orm such as typeOrm or mongoose, it is ok to directly use its features.
So far it sounds like I am telling you about a personal idea, but actually many professional developers already use such simple key value stores.
How about you? do you have an app of yours that would benefit of a simple keyValue store?