Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

StackOverflow Point

StackOverflow Point Navigation

  • Web Stories
  • Badges
  • Tags
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Web Stories
  • Badges
  • Tags
Home/ Questions/Q 245168
Next
Alex Hales
  • 0
Alex HalesTeacher
Asked: August 17, 20222022-08-17T00:31:17+00:00 2022-08-17T00:31:17+00:00In: MongoDB, nosql, redis

MongoDB with redis – Stack Overflow

  • 0

[ad_1]

Redis works excellently with MongoDB as a caching server. Here is what happens.

Anytime that mongoose issues a cache query, it will first go over to the cache server.

The cache server will check to see if that exact query has ever been issued before.

If it hasn’t then the cache server will take the query, send it over to mongodb and Mongo will execute the query.

We will then take the result of that query, it then goes back to the cache server, the cache server will store the result of the query on itself.

It will say anytime I execute that query, I get this response and so its going to maintain a record between queries that are issued and responses that come back from those queries.

The cache server will take the response and send it back to mongoose, mongoose will give it to express and it eventually ends up inside the application.

Anytime that the same exact query is issued again, mongoose will send the same query to the cache server, but if the cache server sees that this query was issued before it will not send the query onto mongodb, instead its going to take the response to the query it got the last time and immediately send it back over to mongoose. There is no indices here, no full table scan, nothing.

We are doing a simple lookup to say has this query been executed? Yes? Okay, take the request and send it back immediately and don’t send anything to mongo.

We have the mongoose server, the cache server (Redis) and Mongodb.

On the cache server there might be a datastore with key value type of data store where all the keys are some type of query issued before and the value the result of that query.

So maybe we are looking up a bunch of blogposts by _id.

So maybe the keys in here are the _id of the records we have looked up before.

So lets imagine that mongoose issues a new query where it tries to find a blogpost with _id of 123, the query flows into the cache server, the cache server will check to see if it has a result for any query that was looking for an _id of 123.

If it does not exist in the cache server, this query is taken and sent on to the mongodb instance. Mongodb will execute the query, get a response and send it back.

This result is sent back over to the cache server who takes that result and immediately sends it back to mongoose so we get as fast a response as possible.

Right after that, the cache server will also take the query issued, and add that on to its collection of queries that have been issued and take the result of the query and store it right up against the query.

So we can imagine that in the future we issue the same query again, it hits the cache server, it looks at all the keys it has and says oh I already found that blogpost, it doesn’t reach out to mongo, it just takes the result of the query and sends it directly to mongoose.

We are not doing complex query logic, no indices, nothing like that. Its as fast as possible. Its a simple key value lookup.

Thats an overview of how the cache server (Redis) works with MongoDB.

Now there are other concerns. Are we caching data forever? How do we update records?

We don’t want to always be storing data in the cache and be reading from the cache.

The cache server is not used for any write actions. The cache layer is only used for reading data. If we ever write data, writing will always go over to the mongodb instance and we need to ensure that anytime we write data we clear any data stored on the cache server that is related to the record we just updated in Mongo.

[ad_2]

  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report
Leave an answer

Leave an answer
Cancel reply

Browse

Sidebar

Ask A Question

Related Questions

  • xcode - Can you build dynamic libraries for iOS and ...

    • 0 Answers
  • bash - How to check if a process id (PID) ...

    • 9 Answers
  • database - Oracle: Changing VARCHAR2 column to CLOB

    • 11 Answers
  • What's the difference between HEAD, working tree and index, in ...

    • 10 Answers
  • Amazon EC2 Free tier - how many instances can I ...

    • 0 Answers

Stats

  • Questions : 43k

Subscribe

Login

Forgot Password?

Footer

Follow

© 2022 Stackoverflow Point. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.