Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for You don't know Redis
Sandor | tutorialhell.dev
Sandor | tutorialhell.dev

Posted on • Edited on

     

You don't know Redis

In my previouspost, I touched on the point that Redis is more than just an in-memory cache.

Most people do not even consider Redis as a primary database. There are a lot of use cases where Redis is a perfect choice for non-cache related tasks.

In this article, I will demonstrate how I built a fully functional Q&A board for asking and upvoting the most interesting questions.Redis will be used as a primary database.

I will use Gatsby (React), Netlify serverless functions andUpstash Serverless Redis.

Upstash has been a good choice so far and I decided to try it out in a more serious project. I love everything serverless and how it makes things simpler for me.

Serverless will be a great choice for most tasks however you need to know the pros and cons of the tech you are using. I encourage you to learn more about serverless to get the most out of it.

Q&A board features

As you may know, I run a tech newsletter for recruiters where I explain complex tech in simple terms. I have an idea to collect questions from recruiters using a Q&A board and let them vote for questions.

All questions will eventually be answered in my newsletter, however, the most upvoted questions will be addressed first.

Anyone can upvote a question and registration is not required.

Questions will be listed in three tabs:

  • Active - questions sorted by votes and available for voting.
  • Most recent - questions sorted by date (newest first).
  • Answered - only questions that have answers.

Upvoting will be one of the most frequently used features and Redis has a data type and optimized commands for it.

ASorted set is ideal for this task because all its members are automatically sorted by the score.

Scores are numeric values that we will associate with votes. It is very easy to increment a score (add a vote) by using theZINCRBY command.

We will also leverage scores for handling unmoderated questions by setting the score for them to0. All approved questions will have a score of1+.

It allows us to fetch all unmoderated questions by simply using theZRANGEBYSCORE command specifying themin andmax arguments as0.

To fetch all approved questions sorted by the score (highest first) we can use theZREVRANGEBYSCORE command setting themin score argument to1.

This is great that by using just a few Redis commands we can also solve logical tasks along the way.Lower complexity is a huge benefit.

We will also use sorted sets for sorting questions by date or filtering questions that have answers. I will explain it in more detail in a moment.

Less frequent operations, namely creating, updating and deleting questions are also easy to accomplish usinghashes.

Implementation details

The most interesting part is always the actual implementation. I use serverless functions and theioredis library and I will link the source code explaining what it does.

This article is dedicated to client-facing functionality. Although I will explain admin-related functions, in the final source code there will be no backend interface. You will need to use Postman or a similar tool to call the admin related endpoints.

Let’s take a look at the API endpoints and what they do.

Add a question

Users can create questions. All questions require moderation before they become visible.

A question is an object and Redis hash is a perfect data type to represent objects.

This is the structure of a questions:
{"datetime":"1633992009", "question":"What are Frontend technologies?", "author":"Alex", "email":"alex@email.com", “score:” “0”, “url”: “www.answer.com” }

We will store questions in hashes using theHMSET command which takes a key and multiple key-value pairs.

The key schema isquestion:{ID} whereID is the question ID generated using theuuid library.

This is a new question and there is no answer yet. We skip theurl property but it will be an easy task to add it later using theHSET command.

The score for a newly created question is0 by default. By our design, it means that this question needs moderation and will not be listed because we only fetch questions with scores starting from1.

Since we keep the score value in a hash, we’ll need to update it whenever it changes. There is aHINCRBY command that we can use to easily increment values in hashes.

As you can see, using Redis hashes solves a lot more for us than just storing data.

Now that we know how we’ll store questions, we also need to keep track of questions to be able to fetch them later.

For that, we add theID of a question to a sorted set with a score of0 using theZADD command. A sorted set will allow us to fetch question IDs sorted by scores.

As you can see, we are setting the score to0 just like we do it for thescore property in the hash above. The reason why we duplicate the score in a hash is that we need it when showing the most recent questions or questions that have answers.

For instance, the most recent questions are stored in a separate sorted set with timestamp as a score hence the original score value is not available unless it’s duplicated in a hash.

Since we store the score in two places, we need to make sure that values are updated both in a hash and in a sorted set. We use theMULTI command to execute commands in a manner where either all commands are executed successfully or they are rolled back. CheckRedis Transactions for more details.

We will use this approach where applicable. For example,HMSET andZADD will also be executed in a transaction (see source code below).

ZADD command takes a key and our schema for it isquestions:{boardID}

All questions are mapped to aboardID. For now, it’s a hardcoded value because I need one board only. In the future, I may decide to introduce more boards, for example, separately for Frontend, Backend, QA and so on. It’s good to have the needed structure in place.

Endpoint:
POST /api/create_question

Here is the source code for thecreate_question serverless function.

Approve a question

Before a question becomes available for voting, it needs to be approved. Approving a question means the following:

  1. Update the score value in hash from0 to1 usingHINCRBY command.
  2. Update the score value in thequestions:{boardID} sorted set from0 to1 using theZADD command.
  3. Add the questionID to thequestions:{boardID}:time sorted set with the timestamp as the score to fetch questions sorted by date (most recent questions) using the sameZADD command.

We can get the timestamp by looking up the question by itsID using theHGET command.

Once we have it, we can execute the remaining three commands in a transaction. This will ensure that the score value is identical in the hash and the sorted set.

To fetch all unapproved questions theZRANGEBYSCORE command is used with themin andmax values as0.

ZRANGEBYSCORE returns elements ordered by a score from low to high whileZREVRANGEBYSCORE - from high to low. We’ll use the latter to fetch questions ordered by the number of votes.

Endpoint for fetching all unapproved questions:
GET /api/questions_unapproved

Endpoint for approving a question:
PUT: /api/question_approve

Here is the source code for thequestions_unapproved serverless function. For the most part, this code is similar to otherGET endpoints and I will explain it in the next section.

Here is the source code for thequestion_approve serverless function.

Fetch approved questions

To fetch all approved questions we use theZREVRANGEBYSCORE command setting themin argument to1 in order to skip all unapproved questions.

As a result, we get a list of IDs only. We will need to iterate over them to fetch question details using theHGETALL command.

Depending on the number of questions fetched, this approach can become expensive and block the event loop in Node (I am using Node.js). There are a few ways to mitigate this potential problem.

For example, we can useZREVRANGEBYSCORE with the optionalLIMIT argument to only get a range of elements. However, if the offset is large,it can add up to O(N) time complexity.

Or we can use a Lua script to extend Redis by adding a custom command to fetch question details based on IDs from a stored set without us doing it manually in the application layer.

In my opinion, it would be overhead in this case. Besides that, one must be very careful with Lua scripts because they block Redis and you can’t do expensive tasks with them without introducing performance degradation. This approach may be cleaner however we would still use theLIMIT to avoid large amounts of data.

Always research the pros and cons before the final implementation. As long as you understand the potential issues and have evaluated ways to mitigate them, you are safe.

In my case, I know that it will take significant time before I will have enough questions to face this issue. No need forpremature optimization.

Endpoint:
GET /api/questions

Here is the source code for thequestions serverless function.

Vote for a question

The process of upvoting a question consists of two important steps that both need to be executed as a transaction.

However, before manipulating the score, we need to check if this question has no answer (url property). In other words, we do not allow anyone to vote for questions that have been answered.

The vote button is disabled for such questions. But we do not trust anyone on the internet and therefore check on the server if a givenID exists in thequestions:{boardID}:answered sorted set using theZSCORE command. If so, we do nothing.

We use theHINCRBY command to increment the score in the hash by1 and theZINCRBY command to increment the score in the sorted set by1.

Endpoint:
PATCH /api/question_upvote

Here is the source code for thequestion_upvote serverless function.

Fetch most recent approved questions

It’s very similar to how we fetch all approved questions with the only difference being that we read another sorted set where the key schema isquestions:{boardID}:time. Since we used the timestamp as a score, theZREVRANGEBYSCORE command returns IDs sorted in descending order.

Endpoint:
PATCH /api/questions_recent

Here is the source code for thequestions_recent serverless function.

Update a question with an answer

Updating or adding new properties to hashes is simple with theHSET command. However, when we add an answer, we move the question from thequestions:{boardID} sorted set to thequestions:{boardID}:answered one preserving the score.

To do so, we need to know the score of the question and we obtain it using theZSCORE command. Answered questions will be sorted by score in descending order.

Then we can:

  1. update the hash with theurl property using theHSET command;
  2. add the hash to thequestions:{boardID}:answered sorted set usingZADD;
  3. remove the question from thequestions:{boardID} sorted set running theZREM command.
  4. remove the question from thequestions:{boardID}:time sorted set running theZREM command.

All four commands are executed in a transaction.

Endpoint:
PATCH /api/question_add_answer

Here is the source code for thequestion_add_answer serverless function.

Fetch questions with answers

Again, the process is similar to fetching all approved questions. This time from thequestions:{boardID}:answered sorted set.

Endpoint:
PATCH /api/questions_unswered

Here is the source code for thequestions_unswered serverless function.


Fullsource code.
WorkingDEMO on my website.

Conclusion

Redis has a lot of use-cases going way beyond cache. I’ve demonstrated only one of the multiple applications for Redis that one can consider instead of reaching for an SQL database right away.

Of course, if you already use a database, adding yet another one may be an overhead.

Redis is very fast and scales well. Most commercial projects have Redis in their tech stack and often use them as an auxiliary database, not just in-memory cache.

I strongly recommend learning aboutRedis data patterns and best practices to realize how powerful it is and benefit from this knowledge in the long run.

Check my previous article where I createdLinkedIn-like reactions with Serverless Redis if you haven’t already.

Here isYou don't know Redis (Part 2)

Follow for more.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Sharing my wisdom and experiences is totally my thing, and you can feel the vibes in the epic tutorials I craft.
  • Location
    Malta
  • Work
    Head of Platform and Infrastructure, where I combine my programming skills with DevOps.
  • Joined

More fromSandor | tutorialhell.dev

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp