DBAtlas · Database operations atlas

ERR Can't execute '<cmd>': only (P|S)SUBSCRIBE / (P|S)UNSUBSCRIBE / PING / QUIT / RESET are allowed in this context: Command not allowed in subscribe context

Redis

What does it mean?

After the connection entered Pub/Sub subscribe mode, you tried to run a regular command (e.g., GET/SET) that is not allowed in this mode. A connection in subscribe mode accepts only a limited set of commands. In older versions the message was "ERR only (P)SUBSCRIBE / (P)UNSUBSCRIBE / PING / QUIT allowed in this context".

Why does it happen?

Sending data commands over a connection that has switched to subscribe mode with SUBSCRIBE/PSUBSCRIBE/SSUBSCRIBE. In the RESP2 protocol a subscribed connection allows only (P|S)SUBSCRIBE, (P|S)UNSUBSCRIBE, PING, QUIT and RESET. Sharing a single connection for both pub/sub and regular work is the typical mistake.

How to fix it

Use a dedicated connection for Pub/Sub and open another one for regular commands (most client libraries do this automatically). Send UNSUBSCRIBE/PUNSUBSCRIBE to leave subscribe mode; regular commands then work. In the RESP3 protocol (HELLO 3) other commands are also allowed on a subscribed connection; switch to RESP3 if your driver supports it.

Open in DBAtlas →

More from this engine