0

Let's say that I have a collection in my database called rabbits. My app uses this database and currently there are multiple users using my app. The users want to see the rabbits one by one; when they start the app they see 1 rabbit and then they press 'next' to see the next one, and so on.

I don't want to query the database every time the user presses next, so I decided to use cursors. I am thinking of creating a simple map data structure (working as a cache) that maps a user to its cursor. So before querying the database again we simply check in the map first.

Is this good practice? should I perhaps use redis here instead?

3
  • Depending on what your app is running on and how many rabbits there are, you could just retrieve all rabbits once, send to the client, and have the client code switch to the next rabbit from the list it already has. Commented Mar 21, 2017 at 10:52
  • I could do that, although I'd appreciate it if someone could clarify when it's really time to start using a cache layer like redis. I guess it's not worth it for something as simple as this? Commented Mar 21, 2017 at 10:58
  • If you have a lot of users all asking for the same list of rabbits, it can't hurt to put redis in the middle. Commented Mar 21, 2017 at 11:10

1 Answer 1

1

there are probably a million answers to this question and most would be correct. Just some possibilities:

  1. Of course you can use Redis, and read it from memory.
  2. You can also downgrade a bid use something like node-cache which will have less overhead and simpler to implement.
  3. You can take the cursor --> array ---> JSON and if you are not worried about constant new rabbits (after all rabbits do multiply fast :) -- then you can write the rabbits to a JSON file, and pick up as the client wants to swing through it.
  4. You can of course aggregate your MongoDB Cursor...or have a cron job run every few minutes to create a new Rabbit Pick cursor.

On and on it goes.

The critical thing is to match what you decide with the services, memory and cores on your server(s).

Sign up to request clarification or add additional context in comments.

2 Comments

This is the answer I was looking for. I wanted modules that already implemented what I wanted to implement. Your introduction of node-cache and cron job helped a lot. Thanks!
@AmerMograbi glad it helped :) Good luck

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.