DEV Community

Cover image for 10 lines of code that will save your life
Tipicalls
Tipicalls

Posted on

10 lines of code that will save your life

Do you really control how much your Web application is going to cost in terms of bandwidth being used? Have you anticipated potential huge bills?
Image description

Most of the cloud providers are now basing their pricing on bandwidth usage, which is really smart as this is not something as a developer we usually handle and monitor...

Unless you do the following below, you will never have any clue about how much data your application is retrieving from the database or from any server every day and so how much the bill will be for your boss at the end of the month for such database or server.

If bad things happen, it will be also very difficult for you to understand which query from the hundreds you do is generating too much traffic…

Here is an example of what you could do when querying a Firebase database. It could be another database or a server instead, the logic would be exactly the same:

Image description

What we do here is that for each query we do to the DB, we track the path of such query (could be the table name for a SQL DB) and how much data in Bytes we are retrieving from such query.

Then every 3 minutes, we build an JSON object with all these different path values we queried and for each, the total number of Bytes of data we retrieved from it.

Such JSON is then sent to Elasticsearch Cloud.

We can also add the information regarding the total session usage of the user and the user ID.

This way you can easily build a visualization in Elasticsearch giving you per user (or for all) how much data is being retrieved from the DB.

You can build a monitoring dashboard this way, and also set email alerts based on a total bandwidth threshold you may not want to go over.

When something bad happens, you can easily retrieve the usage peak, which query generated it and which user too.

Image description

You can get an Elasticsearch cloud instance for 20US$ and this will really save your life when things get bad…

If you are interested in understanding more about the Elasticsearch configuration part, let me know!

I hope this article helped.

Feel free to visit our meeting and collaboration platform tipicalls.com

Bye

Top comments (0)