# Transaction Manager The Transaction Manager handles buffering transactions to Redis, managing state, implementing retry logic, and coordinating with the background processor. ```{contents} Table of Contents :depth: 2 :local: true ``` ## Core Implementation Key code from `transaction_manager.py`: ```{literalinclude} ../../../ccat_ops_db_api/transaction_buffering/transaction_manager.py :emphasize-lines: 20-28 :language: python :lines: 23-78 ``` ## Key Responsibilities 1. **Buffer transactions** to Redis queue (LPUSH) 2. **Track transaction status** with TTL 3. **Implement write-through cache** for generated IDs 4. **Cache buffered data** for smart queries 5. **Manage retry logic** with exponential backoff 6. **Handle failed transactions** (dead-letter queue) ## Buffering Flow ```python # Buffer a transaction async def buffer_transaction(transaction): # 1. Add to queue await redis.lpush(buffer_key, transaction.model_dump_json()) # 2. Store status await redis.setex(status_key, 3600, transaction.model_dump_json()) # 3. Write-through cache await _cache_generated_ids(transaction) # 4. Cache buffered data await _cache_buffered_data(transaction) return transaction.transaction_id ``` ## Redis Key Structure **Transaction Buffer** (List): ```text Key: site:{site_name}:transaction_buffer Operations: LPUSH (add), RPOP (process) ``` **Transaction Status** (String): ```text Key: site:{site_name}:transaction:{transaction_id} TTL: 3600 seconds Value: JSON transaction ``` **Write-Through Cache** (String): ```text Key: site:{site_name}:cache:ids:{model}:{id} TTL: Extended until replicated Value: JSON record data ``` **Buffered Data Cache** (String): ```text Key: site:{site_name}:buffered:{model}:{id} TTL: Extended until replicated Value: JSON record data ``` See {doc}`../../development/redis-inspection` for debugging commands. ## Next Steps - {doc}`background-processor` - Processing buffered transactions - {doc}`lsn-tracking` - Replication confirmation