Skip to content

sembh1998/golang-rest-api-mysql-with-redis

Repository files navigation

Golang REST API with Redis Cache and MySQL Database

This repository contains a simple REST API built with Golang that uses Redis cache to speed up database queries.

Here it is an image of what can be deployed with this repository as an guide

image

Installation

To install the dependencies and run the application, you need to have Docker and Docker Compose installed on your system. Once you have Docker Compose installed, run the following command:

docker-compose up

This command will start a container with a MySQL database and a Redis cache. It will also start a container with the Golang application that exposes the REST API.

How Redis Cache is Used

The Redis cache is used to speed up database queries. When a query is made to the API, the application first checks if the data is available in the Redis cache. If the data is available, it is returned directly from the cache. If the data is not available in the cache, the application queries the MySQL database and saves the data in the Redis cache for future use.

To ensure that the data in the Redis cache is always up-to-date, the application uses a simple mechanism to track when the data in the database was last modified. Whenever a modification is made to the database, the application updates a timestamp in Redis to indicate when the modification was made.

When a query is made to the API, the application checks the timestamp for each table involved in the query. If the timestamp for any table is later than the timestamp for the query, the application knows that the data in the cache is out-of-date and needs to be refreshed. The application then queries the database and updates the cache with the new data.

Example Use

Suppose you have a query that joins two tables, users and orders. Here's how you can use the validateCacheData function to ensure that the data returned by the API is always up-to-date:

consult := "SELECT * FROM users JOIN orders ON users.id = orders.user_id"
tables := []string{"users", "orders"}

validateCacheData(tables, consult)

This function checks the timestamp for both the users and orders tables. If the timestamp for either table is later than the timestamp for the query, the function knows that the data in the cache is out-of-date and needs to be refreshed. The function then queries the database and updates the cache with the new data.

Conclusion

Using Redis cache is a great way to speed up database queries in a Golang application. By using a simple mechanism to track modifications to the database, you can ensure that the data in the cache is always up-to-date and avoid returning stale data to your users.

About

my logic of how to work with mysql and redis

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published