Firebase tips for beginners - I

This will be my first article on the internet. This article is intended for developers having a basic understanding of firebase. I will share some tips and tricks which are necessary to keep in mind while using firebase.

Structure your database properly

Before jumping into writing code, I would suggest spending some time planning and structuring your database. Understand the business requirements and plan accordingly. Ask yourself, what 'queries' would you like to perform to retrieve the data. While structuring your database, keep security rules in mind. Security rules are written to avoid unauthenticated and unauthorized access to your cloud firestore database. Structuring the schema of your database is extremely important and you should consider the future scope of the project as well.

Understand Limitations

Firebase comes with some limitations. Some important limitations are as follows -

  • The maximum size of a document is 1MiB - roughly 1,048,576 bytes.

  • If you have to store a lot of data, try to divide it into collections and subcollections.

  • There's no limit on the number of documents you can have in one single collection but the maximum number of nested subcollections inside the parent collection is 100.

  • You can't implement 'full-text-search' directly using firebase admin-SDK queries, though there are some third-party extensions such as algolia, typesense, etc. which you can use.

Firebase functions by-passes security rules

If there’s a collection named ‘users’ and it is restricted to everyone as follows, it will still be accessible through firebase admin-SDK and cloud functions

service cloud.firestore { 
  match /databases/{database}/documents {
        match /{document=**} {
            allow read, write: if false; 
        } 
    } 
}

Security rules are applied for requests coming from the client side firebase library.

Name your collections carefully

You can’t change the name of the collection once it is declared. You will need to use other third-party apps/services to do this. E.g. ‘Firefoo’

Use minimal words for naming

Use minimal words for naming key-value pairs in a document. This will help reduce document size.

  • E.g. instead of writing {totalHours:1}, write it as {hrs: 1} (if possible).

    The size of {totalHours:1} is 19 byte and the size of {hrs: 1} is just 12 byte.

Refer to storage size and calculations


These are some tips that I found useful while using firebase for my project. Feel free to correct me and connect with me. Thank you!