Backends
Backends store queue state (FIFO order and concurrency). Choose the backend based on whether you run a single instance or multiple instances of your application.
Single Instance
If you run a single process, use the default in-memory backend:
from genesis import Queue
queue = Queue() # InMemoryBackend by default- State lives in process memory
- No extra dependencies
- Omit the backend for simplicity
Multiple Instances
If you run multiple instances (horizontal scaling), pass RedisBackend so all instances share the same queue state:
from genesis import Queue
from genesis.queue import RedisBackend
queue = Queue(RedisBackend(url="redis://localhost:6379"))
async with queue.slot("sales", item_id=session.uuid):
# ...- State lives in Redis (list + counter per queue, pub/sub to wake waiters)
- Each instance enqueues its own call and waits until it is that call’s turn; the process that holds the ESL session must be the one that runs the handler. Redis only stores order and concurrency.
- Optional timeout on
queue.slot()/queue.semaphore()is supported by both backends; when it expires, the item is removed and :exc:genesis.exceptions.QueueTimeoutErroris raised.
Custom Redis Key Prefix
To avoid key collisions in Redis, set a custom prefix:
backend = RedisBackend(
url="redis://localhost:6379",
key_prefix="myapp:queue:"
)
queue = Queue(backend)Parameters
Queue(backend=None)
backend: Backend to use (FIFO + concurrency state). Default:InMemoryBackend, soQueue()is enough for single-process use.
InMemoryBackend()
- No arguments
RedisBackend(url="redis://localhost:6379", key_prefix="genesis:queue:")
url: Redis connection URLkey_prefix: Prefix for Redis keys (default:"genesis:queue:")
Best Practices
- Create the backend once and reuse the same
Queueinstance (e.g. at app startup) - Use
InMemoryBackendfor single-instance deployments,RedisBackendwhen running multiple instances - With Redis, pass
item_id=session.uuid(or similar) when acquiring a slot so you can correlate metrics and traces across instances - If Redis becomes unavailable,
RedisBackendwill raise; ensure your application handles these errors
Related
- Queue - API and usage
- Ring Group - Often used inside a queue slot
- Observability / Metrics - Queue metrics