Firestore + RedisGraph Cloud - The Best of Both Worlds?
Tags: firebase, firestore, redis, redisgraph
Published: 2021-09-12 20:55:58

Right now if you want to do things like search your firestore database or have any kind of realistic relational data, tough cookies.

For reference, I have created work-arounds using Firestore alone:
- [Searching](https://dev.to/jdgamble555/firestore-full-text-search-package-1ea7)
- [Relational Data](https://dev.to/jdgamble555/firestore-many-to-many-mf8)

## Full-Text Searching

There are two officially supported ways to search your data:

- Algolia
- Elastic Search

I am purposely not linking to the docs on this, because paying for an outside service is not a real option. It is ridiculous that the Firebase team owned by the biggest search engine on earth, officially recommends paying for an outside service. I also think Firebase stepped down from Elastic Search since the Amazon forked it, and really just recommends Algolia.

My search version is way cheaper, and almost works just as well IMHO (minus speed).

However, if you don't mind spinning up your own docker instance, there are two other free-ish options (minus paying for the cloud run instance) that can be way faster and cheaper.

- [MeiliSearch](https://www.meilisearch.com/)
- [Typesense](https://typesense.org/)
- [Zinc Search](https://github.com/prabhatsharma/zinc) - light weight, self hosting

Users of Typesense claim it is way better than MeiliSearch, but I personally don't see a big difference. You can see [Fireship.io](https://fireship.io/lessons/meilisearch-firebase-google-cloud/) for quick instructions on implementing this.

## Relational Data

The problem with this is that you still can't search "relational" data. Algolia has ways to index relational data, but it is similar to how MongoDb indexes relational data. It is still a noSQL database under the hood that requires extraneous indexes. It is not efficient nor practical.

## Enter Redis Cloud with RedisGraph

1. Create a [Redis Enterprise Cloud](https://redis.com/redis-enterprise-cloud/overview/) Account.
2. Select [RedisGraph](https://youtu.be/G1rOthIU-uo) as your module
3. Login, get all your server information
4. Download [RedisInsight](https://redis.com/redis-enterprise/redis-insight/) and connect your account
5. Read the [basics](https://oss.redis.com/redisgraph/) about RedisGraph
6. Learn [Cypher](https://neo4j.com/developer/cypher/intro-cypher/)
7. Create a Firebase Function [OnWrite Trigger](https://firebase.google.com/docs/functions/firestore-events) that basically adds and deletes everything you want to search and / or be relational
8. Create a Firebase Callable Function that queries the db

Advanced users can increase the speed with:

- Re-writing your [Firebase Cloud Functions](https://medium.com/google-cloud/firebase-developing-serverless-functions-in-go-963cb011265d) in Go

## Why not just use Redis?

Redis is quick and runs on the heap! It is in the cloud and has automatic scaling! However, there are no frontend apis that I could find. I found one [small simple example](https://github.com/swilly22/RedisGraph-GraphQL) for RedisGraph GraphQL, but no Rest API, nada. Even then I would need to write my own and deploy it to docker. Hopefully something like [Prisma.io](https://www.prisma.io/docs/reference/database-reference/supported-databases) will write a GraphQL adapter, and Redis will support it out of the box. For the moment, it is a cloud database, but not a cloud platform.

**So let's fix two broken databases by putting them together shall we!**

## Counters and Realtime and Notes

- You can use redisGraph for Counters and aggregations through a cloud function
- You could still have realtime data by returning the relevant document ids in the cloud function, then grabbing the document(s) from firebase
- You could theoretically do a [websocket](https://cloud.google.com/run/docs/triggering/websockets) in cloud run, but you don't necessarily need to
- There are also Redis Enterprise versions on AWS and Heroku where you can add RedisGraph

## In Sum

I have downloaded RedisGraph and created a cloud account. It was super easy, free, and most importantly I don't have to mess with docker or understand complex server technology. RedisInsight is just a tool I can install, not a necessity.

I did mess with Cypher using RedisInsight. There are no complex schemas and it is a super easy language. Use [redisgraph.js](https://github.com/RedisGraph/redisgraph.js) in your Firebase Function to connect to it.

I have not done the last part, as I have other projects, but I think this should be a must for anyone who wants to use Firebase in the long haul. If you have a [universal trigger function](https://stackoverflow.com/a/57571042/271450), which keep in mind can be costly and will run on every write to your db, you could keep your redis db up-to-date at all times. If you grow past Firestore, your data is there, and saved in RDFs which is even better.

Hope someone tries this. Let me know your thoughts.

J