PULSEPLAY_DIGITAL_LOGO
Sushil Katoch

Sushil Katoch

Backend Developer

January 3, 20244 min read109

Exploring the Viability of Redis as a Primary Database & Its Considerations.

/20240103-ylu17-ai-generated-image-2024-01-03-441960002022201

Exploring the Viability of Redis as a Primary Database & Its Considerations

Redis is a database which means we can use it for persisting information for any kind of app information like user accounts, blog posts, comments, and so on. After storing information, we can then retrieve it later by writing queries. This behaviour is similar to that of other databases out there. So why use Redis?

Redis is fast because all data is stored in memory, organized in simple data structures, and has a simple feature set. Other databases like PostgreSQL, MySQL, and so on choose to layer on feature upon feature to make them easier to use. But all those additional features have big performance penalties.

As said earlier Redis stores data in memory, what will happen if that instance is destroyed? If you think it is destroyed then you are wrong. It is persistence is built in, there are two types of persistence

1. Snapshotting(RDB): Point and time snapshot of the dataset in a specific interval. This method is used when you need to restore data at a point in time. It brings our Redis instance back to the same stage where it was before.

2. Append Only File(AOF): Redis writes an entry to a log file for every write operation the Redis server receives. When the Redis instance restarts then these operations are reconstructed and played.

Redis is closer to abstraction in traditional computer science such as hashmap, array, sets, and others. So, we can recreate them using Redis, and we can create a table that contains user_id and username which can be done by using sets that allow us to store data using key and value pair.

There is a problem with this implementation as Redis does not have any logical grouping or schema then we will lose the information about what we're storing when we are storing uuid as a key. For example, this uuid can be related to the product, order, email, and many more. There's no way to tell without actually looking at data. So, a good idea to specify the datatype as a namespace the way to do it in Redis via colon is to separate the namespace from the identifier. For example, 

This way we can provide some context about the data type we are storing and it has benefits like in authentication where a username and password are required

What we can perform is a sequential scan which is user data against our data. It is not an optimal method as it can take a long time to scan tables that have a large amount of data. To improve the performance of the database. We can perform an index on the email column which can act as a lookup table.

We can also store hash passwords in the database as a key and value pair. For this we use Redis hashes

We will now do ordering operations via Redis. For this, we will use Redis sorted sets, which allow us to store unique members which are uuid and associated with scores. We can use it to know the number of logins done by the user, we will use:

Redis for unique constraints

We can use Redis set for this which stores unique data inside it. For example, if we want to store a unique username or email, we can do this by:

Redis Transaction

In databases, transactions are used to group operations together in an anatomic fashion, either all are performed or not performed at all. It is important as it doesn't leave us in a partial state.

Cons:

Primitives

When it comes to Redis, there's a significant amount of work and time that goes into creating a database for free. In Redis, where operation uses the Primitives that Redis offers.

Memory Usage

Memory is much more expensive than storage. Will pay more money when building large data sets.

During the snapshot, redis will fork the process and dump what is in that forked process to disk. This means you need 2constraintsto to perform this action but also it’s a global db lock during the forking process. If you have a small dataset this is generally fine but if you have a large database this process will cause large spikes in latency when the snapshot begins.

It's not ACID-compliant

When you're handling thousands of transactions per second and it suddenly crashes, good luck to you, it may lose some of the data.

Append Only File(AOF)

As soon as you want persistence and you enable AOF for every write operation, you'll start to run into performance issues on Redis too.