SUBSCRIBE
Subscribe to one or more channels. SyntaxOne or more channel names to subscribe to
Array of subscription confirmations, one per channel
- Enters pub/sub mode (client can only use pub/sub commands)
- Receives messages as arrays: [“message”, channel, payload]
- Subscription count increases with each channel
UNSUBSCRIBE
Unsubscribe from one or more channels. SyntaxOne or more channel names to unsubscribe from (empty = all)
Array of unsubscription confirmations
- Without arguments, unsubscribes from all channels
- Client exits pub/sub mode when subscription count reaches 0
PSUBSCRIBE
Subscribe to one or more channel patterns. SyntaxOne or more glob patterns (* matches any, ? matches one character)
Array of pattern subscription confirmations
- Pattern messages include the pattern that matched: [“pmessage”, pattern, channel, payload]
- A message can match multiple patterns (delivered once per pattern)
- Glob syntax:
*= any characters,?= single character,[abc]= character set
PUNSUBSCRIBE
Unsubscribe from one or more channel patterns. SyntaxOne or more patterns to unsubscribe from (empty = all)
Array of pattern unsubscription confirmations
PUBLISH
Publish a message to a channel. SyntaxThe channel name
The message payload
The number of subscribers that received the message
- Returns immediately after delivering to all subscribers
- Does not wait for subscribers to process the message
- Fire-and-forget semantics (no persistence or delivery guarantees)
Message Format
Channel Messages
When a channel subscriber receives a message:["message", channel, payload]
Pattern Messages
When a pattern subscriber receives a message:["pmessage", pattern, channel, payload]
Subscription Confirmations
When subscribing:["subscribe", channel, subscription_count]
When pattern subscribing:
["psubscribe", pattern, subscription_count]
Pub/Sub Mode
Entering Pub/Sub Mode
Clients enter pub/sub mode when they issue SUBSCRIBE or PSUBSCRIBE:- Cannot execute regular commands (GET, SET, etc.)
- Can only use: SUBSCRIBE, UNSUBSCRIBE, PSUBSCRIBE, PUNSUBSCRIBE, PING, QUIT
- Connection becomes push-based (receives messages asynchronously)
Exiting Pub/Sub Mode
Client exits pub/sub mode when all subscriptions are removed:- Call UNSUBSCRIBE without arguments to unsubscribe from all channels
- Call PUNSUBSCRIBE without arguments to unsubscribe from all patterns
- When subscription count reaches 0, client can execute regular commands again
Performance Characteristics
Throughput
- Single publisher: 1M+ messages/sec per shard
- Multiple publishers: Linear scaling with shards
- Fan-out: Minimal overhead per subscriber
Latency
- Intra-shard: Less than 100µs publish-to-delivery
- Cross-shard patterns: Less than 500µs (replicated to all shards)
- Network RTT: Adds ~1ms for remote clients
Scalability
- Channels: Unlimited (hash-partitioned across shards)
- Subscribers per channel: Thousands with minimal overhead
- Pattern subscriptions: Replicated to all shards (use sparingly)
Use Cases
Real-Time Notifications
Event Broadcasting
Chat Application
Cache Invalidation
Sharding Behavior
Channel Subscriptions
- Channels are hash-partitioned across shards
- Each channel is handled by exactly one shard
- Subscribers connect to the shard that owns their channel
- PUBLISH routes to the correct shard automatically
Pattern Subscriptions
- Patterns are replicated to all shards
- Pattern subscribers receive messages from all shards
- Higher memory overhead for pattern subscriptions
- Use specific channels when possible for better performance
Reliability Guarantees
Message Delivery
- At-most-once: Messages not persisted, lost if no subscribers
- Best-effort: Delivered to all active subscribers at publish time
- No replay: Subscribers only receive messages sent after subscription
Ordering
- Per-channel: Messages delivered in publish order
- Cross-channel: No ordering guarantees
- Pattern subscriptions: Order preserved within each matching channel
Failure Handling
- Publisher failure: Messages lost, no retry
- Subscriber disconnect: Misses messages while disconnected
- Server restart: All subscriptions and in-flight messages lost
Comparison with Redis
Kora pub/sub is fully compatible with Redis PUBLISH/SUBSCRIBE semantics:- Identical command syntax and behavior
- Same message format
- Compatible wire protocol (RESP2)
- Drop-in replacement for Redis pub/sub clients
- Sharded pub/sub (better multi-threaded performance)
- Lower latency for intra-shard messages
- Pattern subscriptions have higher overhead (replicated to all shards)
- No PUBSUB command (introspection) yet