Redis Hackathon - Sanic Currency Exchange API
Tags: redishackathon, database, redisjson, postgres
Published: 2022-08-24 11:38:30

### Overview of My Submission

![api endpoint for Sanic Currency Exchange API](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ulzcyaphvmzn4tgga04a.png)
 
This submission is to switch my application (Sanic Currency Exchange API) from using PostgresDB to be able to use Redis Stack(RedisJSON) as data store, as shown in the diagram, it contains 3 parts of work.


![refactor and data store switch](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f661ebfetdrhnsd9ej6a.png)

1. Refactoring, to implement Dependency Injection to split Database logic and Application logic.
2. Auto Test/Regression Test, to guarantee that before and after db switch the API behaves the same, this part of work was done in Postman, and the test scripts were shared in repo.
3. Switch Data Store to RedisJson,  to implement a Redis repository which keep the same behaviour as Postgres repository, to make the data store switch happen easily and smoothly.


#### The Idea

> I cloned this Currency API (Github link below) about 3 years ago and kept maintaining it. The initial thoughts were maintaining the dependencies to avoid some security problems. Gradually, I started to re-write the application codes to make it more maintainable, readable. (This is a long way to go, I am sure it has not been finished yet, 😀)

This API is to fetch currency rates from Europe Central Bank, and offer the rates in RESTful API.

Since I saw the [Redis hackathon announcement](https://dev.to/devteam/announcing-the-redis-hackathon-on-dev-3248), I got this idea to **switch data store from Postgres to Redis JSON**. I used Redis before as a caching layer for application, but this is my first time to use the Redis modules. This implementation probably have a lot to improve, please feel free to leave comments to make it better and help me to learn more about Redis JSON and Redis Stack modules.

![say thanks](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/61wovakwlqbd5vod7c8p.png)
 






### Submission Category: **MEAN/MERN Mavericks**


- Use Redis to the RESTful API to add caching and advanced searching capabilities using JSON module.
- Use Redis as a primary database instead of Postgres.
- Redis Stack docker image was used here instead of Redis Cloud, to keep the Postgres and Redis Stack have the same hardware spec.

#### How the data is stored:

The data is store as a document, for each day of the currency rates.

```json
{
   "2020-01-01": {
      "USD": 1,
      "EUR": 1.1,
      "AUD": 0.97
   }
}
```

#### How the data is accessed:

In python codes, the data is accessed through a simple get
```shell
r.json().get('2020-01-01')
```


### Language Used
- Python (Sanic Framework)
- Javascript


### Link to Code

{% embed https://github.com/tim-hub/sanic-currency-exchange-rates-api %}




### Additional Resources / Info

- [License MIT](https://choosealicense.com/licenses/mit/)



### Collaborators

This is a solo submission.
- [TechTim42](https://dev.to/timhub)


![the end](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pin9zl5frgiblyce36mc.png)
 

- - - 

* _Check out [Redis OM](https://redis.io/docs/stack/get-started/clients/#high-level-client-libraries), client libraries for working with Redis as a multi-model database._
* _Use [RedisInsight](https://redis.info/redisinsight) to visualize your data in Redis._
* _Sign up for a [free Redis database](https://redis.info/try-free-dev-to)._