How to play with Firebase keeping control of the bandwidth

Michal Szycko
3 min readAug 31, 2017

After several weeks of work with Firebase and a bit of “what da fuk?” I collected a few simple tips which help you to save some bandwidth.

They may seem obvious now, but they didn’t when we started to work with Firebase.

Make friends with the database profiler tool

Database profiler tool is a part of the Firebase CLI. It logs all the activity connected with your database and gives you detailed reports. A report includes a piece of information about response time, downloaded and uploaded bytes and unindexed queries, which can affect the performance of the application.

The sooner you use it, the better it will be for your application (and for your peace of mind ;)).

Read more about the profiler tool in the official documentation.

Enable local caching

To not download data each time you turn on the app, enable disk persistence. After that, Firebase will store a local copy of the database and will retrieve only information about new, changed or deleted elements. It will also allow you to query the data offline.

Read more about offline capabilities in the official documentation.

Index your data

If you often filter or order the data based on a specific field, you should think about adding an index to it. That simple operation will not only improve the performance of your queries, but also save bandwidth, because for unindexed queries the client downloads all of the data for a given node and only then executes the query on it.

Read more about data indexing in the official documentation.

Make sure you use the right listener which listens to the appropriate node

Firebase provides you two types of listeners:

  • value listeners, which allows you to listen for changes to the given node (and its children)
  • child listeners (child added, child changed, child removed, child moved), which allows you to listen for changes to the given node’s children

Sounds like they do the same thing, huh? Not exactly.

So what’s the difference?

The value listener is triggered every time the data on the given node changes and it returns all data at that location, including children data.

The child listener is triggered every time the child on the given node is added, changed, removed or moved and it returns only the data of the processed child.

What’s the conclusion?

The value listener should be used only for the deepest nodes, which actually represent primitive types. Otherwise, use the child listener.

Read more about data retrieving in the official documentation.

Avoid downloading data when you don’t need it

When you write data through the REST API, Firebase returns the response which contains the data you just sent. In most cases, you probably don’t need to get this data.

To prevent downloading the whole output, just add print=silent to the request parameters. From this moment, the server will return 204 No Content status code.

Read more about the print parameter in the official documentation.

Last but not least

Always read the documentation carefully ;)

Do you have other tips? Feel free to put them in comments. It will be great to talk about it :)

--

--

Michal Szycko

freak, unfulfilled californian punk rock star and music festivals frequenter.