FortuneTapper / FortuneTapper-Bot

GNU General Public License v3.0
0 stars 1 forks source link

Implement non-blocking database save for character repository #13

Open ismael-sensei opened 1 day ago

ismael-sensei commented 1 day ago

Currently, the CachingCharacterRepository saves data to both the cache (Redis) and the database (PostgreSQL) synchronously. This behavior introduces potential latency, especially when the database operation is slow or under high load.

To improve performance and responsiveness, the database save operation should be made non-blocking while keeping the cache update synchronous. This ensures quick cache updates while delegating the database save to a background process.


Proposed Solutions

1. ThreadPoolExecutor

Use Python's built-in concurrent.futures.ThreadPoolExecutor to execute the database save operation in a separate thread.


2. Celery with Redis as Task Queue

Integrate a task queue system using Celery to offload the database save operation to a worker process.


Comparison Table

Aspect ThreadPoolExecutor Celery
Ease of Use Simple and lightweight Requires additional setup
Scalability Limited by max_workers Highly scalable
Retry Mechanism Needs custom implementation Built-in retry support
Error Handling Must handle manually More robust out-of-the-box
Ideal For Small applications or low concurrency Larger, high-load systems

Acceptance Criteria

  1. Implement a non-blocking save operation for CachingCharacterRepository using one of the proposed solutions.
  2. Clearly document the chosen solution and its trade-offs.
  3. Ensure proper logging and error handling for background tasks.
  4. Provide unit tests for the new save behavior.

Additional Notes