DEV Community

Tech Tim (@TechTim42)
Tech Tim (@TechTim42)

Posted on • Updated on

Use Redis Stack to Replace Postgres in a RESTful API - Learn and Output

architecture change

Steps

  1. Refactoring existing code base, to split db logic and business logic
  2. Integration test
  3. Implementation
  4. Load test and Performance Result

Load test

Load test is used here to measure the performance.

  • same Machine (my personal macbook laptop)
  • same spec for DB
    • 10GB Volume
    • Same ARM64 Instance

Load Test result and Performance Compare

  • Original Postgres Result
    postgres performance

  • Switch to use Redis through Redis-Py
    redis-py

  • Switch to use Redis through Redis OM
    redis om

The 2 key results here are iterations (higher is better) and iteration_duration(avg)(lower is better)

Fact Postgres Redis-Py Redis OM
Iterations 25.26/s 5.52/s 2.30/s
Iteration Duration 560.15ms 5.53s 8.07s

This performance benchmark is surprising to myself as well, Redis as a well performanced DB,
in most of time, it give a very good result of performance.

Why Performance get worse after switching, 3 Potential Reasons

database

This performance compare may not be objective, the reasons behind it requires more researching. These are some potential reasons I assume,

1. Min Specs for different DB are difference

Even the spec for Postgres and Redis Stack here are the same, but their min spec requirements may be different.

  • Postgres: 1 GHz processor. 2 GB of RAM. 512 MB of HDD.
  • Redis: 2.6GHz(2 CPUs), 4 GB of Ram, 10 GD of HDD (Redis Stack supposed to be higher)

2. Data Type Convert May Slow the API

Decimal in Python as a class could not be saved directly as a JSON attribute in RedisJSOM,
so it was stores as string, and it will be converted to Decimal when read. This may slow the APP down.

3. Query was not used (RedisSearch) was not used,

So when query history data, a loop of GET are sent to DB, which is not efficient

Conclusion 1

This comparing result doesn't tell which is better between Postgres and RedisJSON, it only says that at current stage, at current DB Instance spec, for this API,
postgres gives better performance, and the way of Decimal Convert and Loop of Get in Application level slows the app too.
(If anyone knows better about Redis to solve these problems, please let me know). So this comparing seems not very fair.

2022 October 25, Get Rid of Assuming Data Conversion in Codes

thinking in front of desk

The result is in the 3rd Table Column Above.

Try solve the issue No 2, stop using Python data convert in application level, and No 3, tried to use find from Redis Search

Tried use Redis-OM for the querying, in this branch,
The result is still not good enough, (actually even worse), issues found:

  1. Redis-OM-Python function was limited, sort by function is not supported,

  2. find function does not support 2 + conditions (not sure way, this is different with official document)

Conclusion 2

dashboard for result

  • Redis-OM-Py is very new, it requires more community contribution
  • The root cause of the performance issue after switch is not from application level, it is still something either from db level, or db SDK/driver performance

Repos

GitHub logo tim-hub / sanic-currency-exchange-rates-api

This is a self hosted, free, open source Python Currency Exchange Rate API fork.

Migrate Sanic Currency Exchange Rates Api to Use Redis Stack @Redis Hackathon

Use Redis Stack (RedisJson) to replace Postgres as main storage, plus some refactoring

stack change

How it works

How the data is stored:

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

{
   "2020-01-01": {
      "USD": 1
      "EUR": 1.1,
      "AUD": 0.97
   }
}
Enter fullscreen mode Exit fullscreen mode

How the data is accessed:

In python codes, the data is accessed through a simple get

r.json().get('2020-01-01')
Enter fullscreen mode Exit fullscreen mode

Performance Benchmarks

postgres performance redis json performance

Load test is used here to measure the performance.

  • same Machine (my personal macbook laptop)
  • same spec for DB
    • 10GB Volume
    • Same ARM64 Instance

The 2 key results here are iterations (higher is better) and iteration_duration(avg)(lower is better)

Fact Postgres Redis Stack
Iterations 25.26/s 5.52/s
Iteration Duration 560.15ms 5.53s

This performance benchmark is surprising to myself as…

Top comments (0)