Django Channels: Receiving messages in order from Redis but processing out of order
I'm dealing with I'm a bit lost with I'm attempting to set up I'm using Django Channels (version 3.0) with Redis as the channel layer..... My application requires that messages sent to the consumer are processed in the same order they are received. However, I notice that when messages arrive quickly, they sometimes get processed out of order. I understand that Redis is not guaranteeing the order of message delivery in case of multiple consumers and that my processing might not be atomic. Here's a simplified version of my consumer code: ```python from channels.generic.websocket import AsyncWebsocketConsumer import json class ChatConsumer(AsyncWebsocketConsumer): async def connect(self): self.room_group_name = 'chat_group' await self.channel_layer.group_add(self.room_group_name, self.channel_name) await self.accept() async def disconnect(self, close_code): await self.channel_layer.group_discard(self.room_group_name, self.channel_name) async def receive(self, text_data): text_data_json = json.loads(text_data) message = text_data_json['message'] await self.channel_layer.group_send( self.room_group_name, {'type': 'chat_message', 'message': message} ) async def chat_message(self, event): message = event['message'] await self.send(text_data=json.dumps({'message': message})) ``` In my tests, I send messages in rapid succession, and they are logged to the console in a different order than expected. I've tried implementing a lock around the message processing logic to ensure that only one message is being processed at a time: ```python import asyncio class ChatConsumer(AsyncWebsocketConsumer): lock = asyncio.Lock() async def chat_message(self, event): async with self.lock: message = event['message'] await self.send(text_data=json.dumps({'message': message})) ``` This seems to improve the situation, but it's still not perfect, especially when there's a spike in message volume. Is there a recommended strategy for ensuring message ordering across multiple consumers in Django Channels, or is there a better way to manage this with Redis? Are there any insights into how Redis handles message ordering under load that could help me? For context: I'm using Python on CentOS. What's the correct way to implement this? This is for a web app running on Ubuntu 20.04. Am I missing something obvious?