Fireship · @fireship
How to NOT get a 30K Firebase Bill
Uploaded August 6, 2018 · Length 8:45
Summary
Optimizing Firebase Firestore to reduce document reads and prevent high bills, using client-side and server-side strategies.
- firebase firestore optimization
- reduce document reads
- firebase billing
- firebase cloud functions
- angular firebase
- rxjs optimization
Transcript preview
The first 2 minutes of the transcript. Select a timestamp to watch that moment on YouTube.
- 0:00
[Music]
- 0:06
imagine if you woke up one day and
- 0:07
you're fired based bill went from zero
- 0:09
to thirty thousand US dollars well
- 0:12
that's exactly what happened to a
- 0:13
start-up in Colombia that scaled up to
- 0:14
about two million daily active users
- 0:16
they really just made one seemingly
- 0:18
minor mistake and with this video I want
- 0:21
to show you how we can take their thirty
- 0:22
thousand dollar bill and reduce it down
- 0:23
to about twenty five bucks if you're new
- 0:26
to the channel make sure to LIKE and
- 0:27
subscribe and you can grab the source
- 0:29
code for this project at angular
- 0:30
firebase com
- 0:32
so you may have already seen this
- 0:33
article because it kind of went viral
- 0:35
last week on hacker noon but the source
- 0:37
of this hi Bill was because they managed
- 0:39
to make over 40 billion requests to
- 0:41
firestore in less than 48 hours so how
- 0:44
is that even possible when you only have
- 0:45
two million users that means every user
- 0:47
would have to make about twenty thousand
- 0:49
document reads the mistake they made is
- 0:52
that they needed to calculate the total
- 0:53
for every crowdfunding campaign but they
- 0:56
were doing this by reading every single
- 0:57
document in a collection for every user
- 1:00
on the site so as they got more users
- 1:02
and more payments the problem just
- 1:04
compounded to result in a shitload of
- 1:06
document reads not only is that super
- 1:08
expensive but it also results in a lot
- 1:10
of latency on the client and that's
- 1:12
actually how they discovered the problem
- 1:13
in the first place and I should also
- 1:15
point out that this type of mistake is
- 1:17
very common and you really won't notice
- 1:18
it until you start scaling up in users
- 1:20
and you run into these problems with
- 1:22
billing and performance I'm gonna show
- 1:24
you how to avoid this with strategies
- 1:26
both client-side and server-side but the
- 1:28
number one thing you should do right off
- 1:29
the bat is setup a billing alert on
- 1:31
Google cloud platform every firebase
- 1:34
project is tied to a GCP project so you
- 1:37
just have to go into the billing panel
- 1:38
and then setup the billing thresholds
- 1:40
that you want to be alerted on so that
- 1:42
way you can find out early if you have
- 1:43
some kind of issue that's resulting in
- 1:45
billing spikes but that doesn't really
- 1:47
solve the problem at hand because the
- 1:49
problem is in the code so I set up a
- 1:52
demo here that does something similar
- 1:53
basically it calculates the total amount
- 1:55
of donations the average amount of each
- 1:57
donation and this is all based on a fire


