Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
August 25, 2022 05:45 am GMT

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)

FactPostgresRedis-PyRedis OM
Iterations25.26/s5.52/s2.30/s
Iteration Duration560.15ms5.53s8.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 No 2

thinking in front of desk

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   }}

How the data is accessed:

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

r.json().get('2020-01-01')

Performance Benchmarks

postgres performanceredis 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)

FactPostgresRedis Stack
Iterations25.26/s5.52/s
Iteration Duration560.15ms5.53s

This performance benchmark is surprising to myself as


Original Link: https://dev.to/timhub/use-redis-stack-to-replace-postgres-in-a-restful-api-2254

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To