Getting Started With Redis
Published: 2021-06-14 12:51:41

__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](https://redis.io/clients) 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
```

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

```
redis-server
```

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/04x4z64ts11hv647gs7t.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u8yybgo7sgkqhhncxd38.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/53rz4xby5ghzud628qvr.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mq77sbqmp31dozzuciab.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rrvp1s1puohllv9fmaf4.png)

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](https://gist.github.com/LeCoupa/1596b8f359ad8812c7271b5322c30946) a really good resource. If you want to perform an action, it's probably on this cheat cheat.