Database Microservice

Earlier I wrote a blog post about the general concept of microservices vs a monolithic structure for projects. In this post I’m going to go into more detail about the specific microservice I will be developing for our project that encompasses the database interactions. This microservice will be responsible for taking in incoming requests from one of the other microservices and translating it to a query to be executed on the database server and then return the correct data. I’m going to go over the basic components needed to create a microservice involving databases.

When creating any sort of backend that involves interacting with a database there a few main things that must be taken into consideration, the database itself, the database server, and how to send queries to the database. For this project we’re using a PostgreSQL database hosted on a Heroku server that we will be interacting with via node.js utilizing PostgreSQL specific libraries and an object-relational mapper. I will go into further detail on this topics in the next sections and how they relate to my groups project.

The first choice you have to make when thinking about creating a database microservice is what type of database you want to use. There are a ton of database services out there with different pros and cons that make certain ones better in some situations than others and it can be daunting to chose. The one database that we decided to use is a PostgreSQL database because it is opensource and it has been around for a long time so there are a lot of libraries built for it and other tools that help integrate it into projects. Speaking of tools that help integrate PostgreSQL into your projects that brings us to the next topic, database server.

In order to have any sort of app or website interact with your database it has to be hosted somewhere which is where the database server come in. The database server is just a site that holds your database and maintains the entity relationships so that you can send queries to it and have your data returned. There are a lot of different services that provide this and a lot of them are not free to use because but for our project we are using a free service provided by Heroku. Heroku provides tools that allow you to create a database server that you can connect to if you have the credentials for that server and database and send queries to it and have data returned. Our database is hosted on this service and the integration has been pretty smooth when I have interacted with it from pgAdmin which is just a tool that can be used to create or connect to an existing database and interact with it via SQL queries.

The next step that I’m working on for the project is writing the actual code that will take a request and translate it into a SQL query that it will then send to the database and get the data back in return. To do this I’m utilizing node.js to create a server for our Javascript to run on and with node comes different utilities created specifically to interact with PostgreSQL databases like our. One of those utilities is a library called node-postgres which provides a lot of useful features for interfacing with our database such as commands for connecting to the database server as well as send queries to it. On top of this set up I will be using an object-relational mapper (ORM) which basically allows you to interact with a database in an object orientated fashion. In this case the one that I’m looking into using is called Sequelize which just makes it easier to query the database because rather than having various joins and long query statements you can just pass some parameters to a query object of a table such as the store name and the zip code and it will return all stores with that name in that zip code. So the frontend UI may have a form or search box or something and then these values will be passed to the database microservice and interpreted and then the ORM will map the query appropriately and return the correct data to the user.

So with these tools selected and outlined I’m now ready to start on the last major part of the project that I’m responsible for. This is a pretty huge part of our application so it’s going to take me a significant amount of time but luckily these tools are very well developed and documented. Maybe this post will be a good start for someone else looking to create a database microservice that is overwhelmed by all the different options nd combinations of tools out there.

Print Friendly, PDF & Email

Leave a comment

Your email address will not be published. Required fields are marked *