Fireship · @fireship

How to NOT get a 30K Firebase Bill

Uploaded August 6, 2018 · Length 8:45

How to NOT get a 30K Firebase Bill

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.

  1. 0:00

    [Music]

  2. 0:06

    imagine if you woke up one day and

  3. 0:07

    you're fired based bill went from zero

  4. 0:09

    to thirty thousand US dollars well

  5. 0:12

    that's exactly what happened to a

  6. 0:13

    start-up in Colombia that scaled up to

  7. 0:14

    about two million daily active users

  8. 0:16

    they really just made one seemingly

  9. 0:18

    minor mistake and with this video I want

  10. 0:21

    to show you how we can take their thirty

  11. 0:22

    thousand dollar bill and reduce it down

  12. 0:23

    to about twenty five bucks if you're new

  13. 0:26

    to the channel make sure to LIKE and

  14. 0:27

    subscribe and you can grab the source

  15. 0:29

    code for this project at angular

  16. 0:30

    firebase com

  17. 0:32

    so you may have already seen this

  18. 0:33

    article because it kind of went viral

  19. 0:35

    last week on hacker noon but the source

  20. 0:37

    of this hi Bill was because they managed

  21. 0:39

    to make over 40 billion requests to

  22. 0:41

    firestore in less than 48 hours so how

  23. 0:44

    is that even possible when you only have

  24. 0:45

    two million users that means every user

  25. 0:47

    would have to make about twenty thousand

  26. 0:49

    document reads the mistake they made is

  27. 0:52

    that they needed to calculate the total

  28. 0:53

    for every crowdfunding campaign but they

  29. 0:56

    were doing this by reading every single

  30. 0:57

    document in a collection for every user

  31. 1:00

    on the site so as they got more users

  32. 1:02

    and more payments the problem just

  33. 1:04

    compounded to result in a shitload of

  34. 1:06

    document reads not only is that super

  35. 1:08

    expensive but it also results in a lot

  36. 1:10

    of latency on the client and that's

  37. 1:12

    actually how they discovered the problem

  38. 1:13

    in the first place and I should also

  39. 1:15

    point out that this type of mistake is

  40. 1:17

    very common and you really won't notice

  41. 1:18

    it until you start scaling up in users

  42. 1:20

    and you run into these problems with

  43. 1:22

    billing and performance I'm gonna show

  44. 1:24

    you how to avoid this with strategies

  45. 1:26

    both client-side and server-side but the

  46. 1:28

    number one thing you should do right off

  47. 1:29

    the bat is setup a billing alert on

  48. 1:31

    Google cloud platform every firebase

  49. 1:34

    project is tied to a GCP project so you

  50. 1:37

    just have to go into the billing panel

  51. 1:38

    and then setup the billing thresholds

  52. 1:40

    that you want to be alerted on so that

  53. 1:42

    way you can find out early if you have

  54. 1:43

    some kind of issue that's resulting in

  55. 1:45

    billing spikes but that doesn't really

  56. 1:47

    solve the problem at hand because the

  57. 1:49

    problem is in the code so I set up a

  58. 1:52

    demo here that does something similar

  59. 1:53

    basically it calculates the total amount

  60. 1:55

    of donations the average amount of each

  61. 1:57

    donation and this is all based on a fire