DEV Community

jonhourcade
jonhourcade

Posted on • Updated on

Getting Started With Redis

In-Memory Databases

In-memory databases store data, as the name implies, in random access memory instead of on a disk. As such, read and write operations are completed significantly quicker than those performed by traditional databases.

Available RAM is necessary for a highly-functioning server. Therefore, developers must be store data in-memory selectively. Else, the performance benefits associated with fast access and retrieval will be offset.

RAM is volatile. Every time the server powers down, all data stored in the in-memory database is lost. However, data stored in an in-memory database is not mission-critical and is typically set to expire after a set interval.

Traditional databases store data in relations in the case of SQL databases and collections in the case of NoSQL databases. Both kinds of databases employ schemas (a user has a username which is of type string and is required, a hash which is also of type string and is required, et cetera).

In-memory databases are data structure stores. There are no tables and no entities, there are only key-value pairs. In the Redis in-memory data structure store, keys point to strings, arrays, objects, and sets.

Use Cases

In-memory databases are very useful when storing data that does not need to persist. The first time I heard the term was when learning about sessions. Data associated with a session does not need to be saved after the user logs out or the session expires.

Servers can use in-memory databases to 'cache' query results from external APIs. This could be useful if it takes a long time to get results from the API, if there is a quota on the number of API calls a server may make, if it is expensive to format the returned data so the client can make use of it, et cetera.

Lastly, any application that requires real-time decision making can benefit from in-memory databases. Games are one example. Other, more significant, examples include Internet-of-Things enabled devices such as autonomous vehicles, smart cities, virtual reality, augmented reality, and many other technologies that we will see in our lifetimes.

Examples

I will now implement a very simple Redis database. The following implementation is written in Javascript using the Express web-server framework and the Node runtime environment. Redis can be implemented in almost every modern programming language. Check the official documentation out for a complete list.

After you've initialized node package manager, run the following command to install Express and Redis:

npm i express redis
Enter fullscreen mode Exit fullscreen mode

Run the following command to start up your Redis database. Then, spin up your server using node or nodemon.

redis-server
Enter fullscreen mode Exit fullscreen mode

Just like when using an ORM or ODM, you need to create a connection to the database before you can interact with it. Fortunately, Redis is incredibly simple to set up. The following implementation uses three lines. However, the first and second line could be combined.

After requiring the Redis package, invoke the createClient method on the default export. Capture the return value in a constant. Invoke the on method on the constant. The first argument is 'connect' and the second is a callback that runs when the connection is successful.

Alt Text

The following routes add and remove key-value pairs to and from the database.

To add a key-value pair to the database, call the set method on the client object (the object that is returned when you invoke create client on the default export).

The set method requires two arguments, the key and the value. The third argument is optional. It is a callback that runs when the insertion is complete. The callback can take up to two parameters, the first parameter is an error object and the second parameter is a success object.

The success object is the string 'OK'. I don't find that particularly useful so I opted only to pass one argument into the set method.

The get method only requires one argument, the key. I provided the optional callback with two parameters cause the second argument passed into the callback contains the data retrieved from the database.

Alt Text

The following routes add and remove arrays to the database.
The first argument passed into the rpush method is the key and every subsequent argument is an element in the array. I chose to spread them out using the spread operator.

The lrange method takes at least three arguments. The first is the key, the second is the starting index, and the third is the ending index. A starting index of 0 starts at the beginning of the array and an ending array of -1 retrieves the rest of the array. Just like with key-value pairs, a callback can be passed in that has access to the data returned by the query.

There are many more array methods. Some other useful methods include push, rpop (js equivalent of pop), lpush (js equivalent of shift), and lpop (js equivalent of unshift).

Alt Text

These routes add and retrieve objects from the database. The hmset method adds objets to the database and the hmgetall method retrieves objects from the database. The hget method takes a key and a property and returns the value associated with that property. The hset method sets the value associated with that property. The hdel method deletes a property.

Alt Text

The last two routes show how to remove a key-value pair from the database and add an expiration. Adding expirations is a method call you're going to be making quite a bit when you use Redis cause you're frequently going to use the database to store date you don't need to permanently.

They are both pretty explanatory. Expire takes a key and a number of seconds and delete takes a key.

Alt Text

Redis has a TON of methods you can use to interact with things stored in the database. I focused no the basic getters and setters to show how easy it is to implement into your projects. Here's a really good resource. If you want to perform an action, it's probably on this cheat cheat.

Top comments (0)