Make read/write DB routing consistent for http requests and background tasks
When replica is enabled background tasks and HTTP requests can follow different DB routing behaviors, leading to inconsistent reads. HTTP requests can pin to the writer after a write, but Celery tasks may continue reading from replicas even after performing writes. This inconsistency risks read-after-write anomalies and makes behavior unpredictable across execution paths.
Desired behavior:
- Default to reading from replicas for both HTTP requests and background tasks.
- On the first write within a request/task, pin all subsequent DB operations to the writer for the remainder of that request/task.
- Ensure routing state is isolated per request/task and cleared afterwards.
- Works transparently with transactional job contexts and exporters.