parse-server icon indicating copy to clipboard operation
parse-server copied to clipboard

Add the global redis cache the ability for caching query results

Open andreisucman opened this issue 2 years ago • 9 comments

New Issue Checklist

Issue Description

It would be nice to have global redis cache cache query results from Mongo DB by default.

Steps to reproduce

  1. Install a redis server and make sure it's running.
  2. Connect the redis server to parse via redis adapter like this:

const RedisCacheAdapter = require("parse-server").RedisCacheAdapter;

const serverConfig = {
   ...
   cacheAdapter: new RedisCacheAdapter({ url: process.env.REDIS_URL }),
   ...
}

Environment

node 18.2.0 redis-server on local docker v7.0.10 redis-server on digital ocean droplet with ubuntu 22 v7.0.4

Server

  • Parse Server version: ^6.0.0
  • Operating system: Win 64
  • Local or remote host (AWS, Azure, Google Cloud, Heroku, Digital Ocean, etc): Redis server tested both on redis-server version=7.0.10 on a local docker container as well as digital ocean droplet with ubuntu 22 redis-version 7.0.4

Database

  • System (MongoDB or Postgres): Mongo
  • Database version: 5.0.15
  • Local or remote host (MongoDB Atlas, mLab, AWS, Azure, Google Cloud, etc): Atlas

Client

  • SDK (iOS, Android, JavaScript, PHP, Unity, etc): JavaScript
  • SDK version: ^4.0.1

Logs

andreisucman avatar Apr 02 '23 13:04 andreisucman

Thanks for opening this issue!

  • 🚀 You can help us to fix this issue faster by opening a pull request with a failing test. See our Contribution Guide for how to make a pull request, or read our New Contributor's Guide if this is your first time contributing.

This sounds more like you're looking for support on how to set up Redis cache with Parse Server.

mtrezza avatar Apr 02 '23 15:04 mtrezza

Thank you for these resources I will check them out.

But there's not really any setup necessary, just copy and paste 2 lines of code which I did.

And my redis servers are up and running.

So this might be a bug rather than just me asking for support.

Thank you.

andreisucman avatar Apr 02 '23 15:04 andreisucman

Thanks for providing more details, if you can confirm that this is a bug and not an issue with your custom setup then please post here and we'll reopen the issue.

mtrezza avatar Apr 02 '23 15:04 mtrezza

From my observation, Parse Server uses an inbuilt cache / redis cache to store User Roles and User Data to speed up the time it takes to resolve a cloud request (beforeFind, afterFind, etc). It does not provide a "global cache" that stores query responses.

If you use a Redis GUI such as RedisInsight you can observe this happening.

dblythy avatar Apr 02 '23 23:04 dblythy

Thanks for confirming my surmise.

andreisucman avatar Apr 02 '23 23:04 andreisucman

Yes, so I think you would need to reword this as a feature request rather than a bug because the cache is working as expected.

See: https://docs.parseplatform.org/parse-server/guide/#configuring-cache-adapters

By default, parse-server provides an internal cache layer to speed up schema verifications, user, roles and sessions lookup.

In some cases, in distributed environment, you may want to use a distributed cache like Redis.

dblythy avatar Apr 03 '23 00:04 dblythy

I think this is a duplicate of #4750.

I think it would be hard to decide how a global cache should work, if it's providing a short-circuited response from beforeFind and afterFind triggers. A developer might have custom logic that they want to run in a trigger, regardless of whether the response is cached or not.

It seems like Flo's suggestion was:

  1. Allow beforeFind triggers to return objects
  • This could allow for custom query returns, for example if a developer is using a custom search such as ElasticSearch
  • This could allow cached objects to be returned
  1. Pass objects in afterFind to a cache adapter

It's worth noting that whether or not a cacheAdapter is specified, Parse Server will cache schema verifications, user, roles and sessions lookup in an in-memory cache. If we introduce the ability to cache query responses, we will need to make sure that the default behaviour is considered.

We could possibly add a parameter to SCHEMA or Cloud Validators of cache: true which interprets whether to cache query results with the CacheAdapters ttl

dblythy avatar Apr 04 '23 01:04 dblythy

Just as we were speaking there was a package out that does query result caching:

https://community.parseplatform.org/t/parse-cache-memory-a-cache-library-customized-for-parse-server-queries/3367/5

I've tested it and my query responses improved from 200ms to 1ms.

andreisucman avatar Apr 04 '23 10:04 andreisucman