Posted on

Android Firebase Geofire Performance


android firebase geofire perfomance

Designed by Freepik

Android Firebase Geofire is a library for searching by location with Firebase. Let’s learn about how it actually performs for real life applications and if it’s good enough

Geofire

Depends on the application type of course. In my example I’m going to use a sample app from the previous post about implementig Geofire with RecyclerView. And there I display users 50 km from my locations, sorted by distance from me.

There’s bunch of different sample of Firebase on Android like implementing push notification with Firebase and more

So is few words how Geofire works is you fetch keys of the users (what ever data you link to locations)  and then after having those keys – you actually fetch real data that you need to display. So fetching those keys should be really fast, if it’s just a small part of the whole process, right?

Concept

I created three locations: NYC, Boston, Fort Lauderdale. I stay in FL, so I see users only in 50 radius from me. I have those three handy buttons to add 1000 random users at each location, just to give it more realistic scale of real applications

android firebase geofire perfomance

When I add somebody to Boston or NY – I don’t see no changes, only Fort Lauderdale users displayed for me.

Performance

With Geofire there’s a method to search by location

So I started with 3 users at my location and no users anywhere else. Time to fetch was 1700 ms. Then I started to add bunch of users to locations but mine.

+1000 in NYC – time = 2700ms, +1000 in Boston  – time = 3100, +1000 in NYC – time = 4100 etc, it just gets slower and slower. That’s just to fetch keys! Then you need to fetch actual data after it as well.

And the first point to consider using Geofire for location search is that fetching key gets slower even if there’s nobody at your location. It seems to be fetching the whole geofire node, it doesn’t really matter how Geofire works, the fact is that it gets slower with more data.

And then I added 1000 users to my location, same number of growth is speed. Approximately + 800 ms for each thousand of keys

Solution?

So I wait for all users to be fetched, but you might not need it, right? Just wait for first 20 keys fetched in onKeyEntered, fetch those 20 users, display UI and let the rest of the keys be fetched in the background. Then you just implement pagination with the rest of the keys. In this case Geofire performs OK

But what if you need to display users sorted by distance from you? In Geofire keys aren’t sorted by distance from you of course. And to do that  you need to fetch all the keys, you can’t just fetch first 20, sort them and display.

Which breaks the whole pagination thing. Although Firebase is simple in using – it doesn’t mean that it’s great at everything out of the box. You’ll have to write you server for location search to be fast, but that will take some time

Or come up with some smart way to store users on Firebase so you fetch as smallest number of keys possible. I first got the idea dividing the map to small squares, so instead of using the root geofire node for the whole globe – you use geofire01, geofire02 depending on user location. So it’s less stuff to fetch, but then I realized it’s a stupid idea, because you can’t use Geofire instance from more than one node (if user is close to square border)

android firebase geofire perfomance

Algolia

If you need those features: sort by distance, pagination etc – I recommend using Algolia. It’s basically a server that mirrors you Firebase database, with pagination, location search, sorting by distance out of the box. Great service,  so you don’t use Firebase Android SDK to fetch some stuff, but Algolia SDK.

This means that you loose realtime features, because Algolia doesn’t have it. Just a simple REST API. If you need realtime Firebase and use Algolia – you need to implement it yourself.

 

Thanks for reading, tell me about your impression of ether Geofire or Algolia if you used them. You can get source code of the app I used here

 

 

 


  • Andres Garcia

    Yep… That’s the issue with Geofire at the moment and I haven’t found a solution yet.
    I guess, somehow we will have to divide the Data in different Nodes when the Data is being written, based on a Location. But, as you pointed out, for Users that are at boundaries this would be tricky.
    So yeah, I don’t know really and it’s one big thing still playing in the back of my mind, knowing that somehow this will have to be improved.
    Fingers crossed, those Geofire People are smart and surely they have identified the issue a for a long time now (coz it’s pretty obvious) and working on it.

    • Dude, do you have a blog? You’re like the most dedicated reader up here, always leave some comments, I’d love to read what you have to say

      • Andres Garcia

        Haha !
        Nah sorry, don’t have a blog. Can’t be bothered posting, maintaining etc.
        But I like yours very much. Often interesting subjects and sometimes we wonder about the same stuff ( like this Geofire Topic).