Sharded Pub/Sub in Redis 7 improves pub/sub scalability by distributing message traffic across multiple threads. Traditional Redis Pub/Sub was limited by a single-threaded model that could become a bottleneck under high load. With Sharded Pub/Sub, channels are divided into shards assigned to specific I/O threads; each thread handles message delivery for its assigned shard, reducing contention and improving performance. Key points include: 1) channel-to-shard mapping via hashing; 2) configurable number of shards; 3) use of SSUBSCRIBE for sharded subscriptions; 4) automatic sharding during PUBLISH. It is ideal for applications with many channels, such as real-time chat systems or IoT platforms. To use it, replace SUBSCRIBE with SSUBSCRIBE, while PUBLISH remains unchanged. Clients must explicitly opt-in to sharding, and both modes can coexist during migration.
Sharded Pub/Sub in Redis 7 is a new feature that addresses one of the limitations of the traditional Redis Pub/Sub system — scalability. If you're using Redis for real-time messaging and have multiple channels or patterns, especially under high load, you might have hit performance issues before. Sharded Pub/Sub helps by spreading pub/sub traffic across all available event loops (or threads), instead of routing everything through the main thread.
Why It Was Needed
Redis has always used a single-threaded model for handling commands, which keeps things simple but can become a bottleneck when handling large volumes of pub/sub messages. In earlier versions, even if Redis was compiled with threaded I/O enabled, all Pub/Sub operations still went through the main thread. That meant publishing or delivering messages to many subscribers could block other operations.
With Redis 7, this changes.
How Does Sharded Pub/Sub Work?
The core idea behind Sharded Pub/Sub is sharding — distributing the responsibility of message delivery across multiple threads. Here’s how it works:
- Redis divides channels into logical groups called "shards."
- Each shard is assigned to a specific I/O thread.
- When a client subscribes to a channel, the subscription is registered with the shard (and thus thread) responsible for that channel.
- Messages published to a channel are processed and delivered by the same thread, avoiding cross-thread synchronization overhead.
This way, Redis can scale pub/sub performance more effectively as the number of CPU cores increases.
Some key points:
- Channel names are mapped to shards using a hashing function.
- The number of shards is configurable via
redis.conf
(pubsub_shard_streams_per_node
). - Subscribers only receive messages from the channels they've sharded-subscribed to — there's a separate command for that:
SSUBSCRIBE
.
When Should You Use It?
You should consider using Sharded Pub/Sub when:
- Your application uses a large number of channels.
- You experience performance bottlenecks due to heavy pub/sub usage.
- You want to scale Redis horizontally without adding more instances just for message distribution.
It's particularly useful in applications like:
- Real-time chat systems with many rooms or topics.
- Notification services broadcasting updates to many users.
- IoT platforms where each device publishes status updates to its own channel.
If your use case involves only a few channels with many subscribers, standard Pub/Sub may still be fine. But once you start scaling the number of channels or need lower latency per message, sharding becomes a better fit.
How to Use Sharded Pub/Sub
Using Sharded Pub/Sub is straightforward, but different from the classic SUBSCRIBE
/PUBLISH
flow. Here’s what you need to do:
- To subscribe to a channel in a sharded way, use
SSUBSCRIBE
instead ofSUBSCRIBE
. - To publish to a sharded channel, use
PUBLISH
— the sharding is handled automatically based on the channel name. - To unsubscribe, use
SUNSUBSCRIBE
.
Example:
# Subscribe to a sharded channel client1> SSUBSCRIBE mychannel # Publish a message — handled by the correct thread client2> PUBLISH mychannel "Hello sharded world"
Keep in mind:
- Clients must explicitly use
SSUBSCRIBE
to take advantage of sharding. - Classic
SUBSCRIBE
andSSUBSCRIBE
cannot be mixed in the same connection. - Tools or clients not updated for Redis 7 won’t use sharded pub/sub unless modified.
Also, since Redis 7 supports both modes, you can gradually migrate parts of your app to use sharded subscriptions while keeping others on the traditional model.
Final Thoughts
Sharded Pub/Sub in Redis 7 brings much-needed scalability improvements to Redis' real-time messaging capabilities. By assigning channels to different threads, Redis can handle much higher throughput without sacrificing simplicity or reliability.
If you're working with Redis at scale, especially in pub/sub-heavy environments, it's worth upgrading and trying out this feature.
That’s basically it — not too complicated, but definitely a big step forward for Redis as a messaging platform.
The above is the detailed content of What is Sharded Pub/Sub in Redis 7?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Redis'sin-memorystoragemodelprovidessuperiorperformancecomparedtodisk-baseddatabasesduetofasterdataaccess.1)DataisstoredinRAM,enablingquickread/writeoperations.2)Persistencerequiresconfiguration,usingAOForRDB,whichimpactsperformance.3)Memorylimitatio

Installing RedisonLinux requires the following prerequisites: 1. A Linux distribution, such as Ubuntu, CentOS, or Debian; 2. GCC compiler, used to compile Redis from source; 3. Make and libc6-dev, used to build Redis; 4. Tcl (optional), used to run Redis tests. These tools ensure smooth installation and testing of Redis.

RedisusesRDBsnapshotsandAOFloggingfordatapersistence.RDBprovidesfast,periodicbackupswithpotentialdataloss,whileAOFoffersdetailedloggingforpreciserecoverybutmayimpactperformance.Bothmethodscanbeusedtogetherforoptimaldatasafetyandrecoveryspeed.

ToinstallRedisonaLinuxsystem,followthesesteps:1)DownloadandextractRedisfromtheofficialGitHubrepository,2)CompileRedisusingthe'make'command,3)InstallRediswith'sudomakeinstall',4)ConfigureRedisbycopyingandeditingtheconfigurationfile,and5)StartRedisusin

Redisexcelsinreal-timeanalytics,caching,sessionstorage,pub/submessaging,andratelimitingduetoitsin-memorynature.1)Real-timeanalyticsandleaderboardsbenefitfromRedis'sfastdataprocessing.2)Cachingreducesdatabaseloadbystoringfrequentlyaccesseddata.3)Sessi

Redisislimitedbymemoryconstraintsanddatapersistence,whiletraditionaldatabasesstrugglewithperformanceinreal-timescenarios.1)Redisexcelsinreal-timedataprocessingandcachingbutmayrequirecomplexshardingforlargedatasets.2)TraditionaldatabaseslikeMySQLorPos

ShardedPub/SubinRedis7improvespub/subscalabilitybydistributingmessagetrafficacrossmultiplethreads.TraditionalRedisPub/Subwaslimitedbyasingle-threadedmodelthatcouldbecomeabottleneckunderhighload.WithShardedPub/Sub,channelsaredividedintoshardsassignedt

Redisisbestsuitedforusecasesrequiringhighperformance,real-timedataprocessing,andefficientcaching.1)Real-timeanalytics:Redisenablesupdateseverysecond.2)Sessionmanagement:Itensuresquickaccessandupdates.3)Caching:Idealforreducingdatabaseload.4)Messagequ
