To design Slack is to build a workplace messaging system in which many separate organisations, called workspaces, each have channels, direct messages, threads, search and notifications. A good answer explains how a message is accepted and numbered inside a channel, how unread badges are kept right and cheap, how a message in a channel with thousands of members reaches only the people who need it now, how search respects permissions, how data is split across servers when workspaces differ enormously in size, how notifications avoid drowning people, and how history is loaded without gaps or repeats.

This lesson designs CampusHub, a workspace app for the student clubs of a college, with a few huge workspaces and hundreds of tiny ones. Six runnable programs in Java, with Python and C++ versions, test the decisions interviewers probe most: unread counts, delivery to big channels, search with permissions, sharding, notification rules, and history paging. All sizes, rates and timings are assumptions chosen to make comparisons clear, not benchmarks of a product.

Key takeaways

  • A row for every message and member wrote 2,117,176 rows for 20,000 messages, and one message in the largest channel (910 members) wrote 910 rows. A latest number for the channel plus a read position for each member wrote 20,000 rows and gave 0 wrong badges. A counter that is raised for every message ended wrong for 3,052 members when 1 update in 500 was lost.

  • Pushing every message to every online member made 985,145 events and 591.0 MB in a busy minute. Pushing the message only to members looking at the channel, and one small dot per channel to the rest, made 94,116 events and 33.1 MB.

  • A search that took the newest 50 matches and then removed what the student could not see returned 7.5 results of 20, and 757 of 1,000 searches came up short. Checking permissions inside the search returned 19.0, and an index for each workspace read 23.9 postings instead of 524.6.

  • Sharding by workspace put 53.4% of all messages on one shard, against an even share of 6.2%. Sharding by channel brought the busiest shard to 19.8%, and the general channel of the huge workspace explains most of what is left.

  • Notifying every message meant 491.7 notifications a student an hour. Notifying only direct messages, mentions and @channel meant 8.0.

  • Asking for history by position showed 100,086 messages twice and skipped 5,029. Asking by time and message id repeated and skipped none.

1. Clarifying Requirements

Functional requirements

  • Members belong to a workspace. A workspace has public channels, private channels and direct messages.

  • Members post messages, reply in threads, mention people by name and use @channel.

  • Each member sees unread badges per channel, and a separate count of mentions.

  • Messages can be edited and deleted, and history can be scrolled back without limit.

  • Search across the messages a member is allowed to see.

  • Notifications on phone and desktop, with per-channel preferences.

  • Files and links, and apps that post through webhooks.

Non-functional requirements

  • Strict isolation between workspaces: nothing from one workspace may appear in another, in a search, a cache or a log.

  • Live updates within about a second for members who are looking at a channel.

  • No lost messages, one agreed order inside each channel.

  • High availability, and uneven load: one workspace may have 50,000 members and another 12.

  • Per-workspace retention rules and export.

Ask whether there are voice or video calls, how large a channel can be, and whether messages are end-to-end encrypted (they are usually not, because search, retention and admin export need the text). Leave out calls, workflow builders and the app store, and say so.

2. Back-of-the-Envelope Estimation

  • Messages: 5 million daily users sending 30 messages each is 150 million messages a day, 1,736 a second on average and about 10,000 at the peak.

  • Storage: at 500 bytes a message that is 75 GB a day and 27 TB a year, and the search index is about as large again.

  • Deliveries: they depend on who is looking, not on how many members a channel has. Most members of a big channel are in other windows.

  • Skew: a few workspaces and a few channels hold a large share of the traffic.

3. Core APIs

  • POST /v1/channels/{id}/messages with {"client_msg_id": "...", "text": "...", "thread_ts": null}. The reply holds the number the server gave the message in that channel. The client_msg_id makes a retry harmless, as in Design WhatsApp.

  • GET /v1/channels/{id}/history?before=(time, id)&limit=50 returns older messages. The cursor is a time and a message id, never a position.

  • POST /v1/channels/{id}/read with {"up_to": 4812} sets the read position of the caller.

  • GET /v1/search?q=...&cursor=... searches only messages the caller may see.

  • A socket sends live events: message for the channel being viewed, and a small channel_unread event for others.

4. High-Level Design

A client keeps one socket for live events and uses HTTP for everything else. The edge gateway authenticates the token and checks that the member belongs to the workspace and the channel. The channel message service gives each message the next number in its channel, writes it to the log, and answers. Everything else is fed from that log through queues: the live fan-out, the notification service, the search indexer and the retention jobs. Because they read the log, a slow search indexer cannot slow a post.

The data model has four levels. A workspace is the tenant and the natural unit for isolation, billing and retention. A channel belongs to one workspace and is public or private. A message has a number in its channel, and a thread reply carries the number of its parent, so a thread is a filter on the same log with a reply count kept on the parent. Per-member state (read position, mute, mentions) is kept apart from the messages.

5. Unread Counts

Every channel in the sidebar shows how many messages are unread, and the number must be right on every device. Three designs: store an unread row for every message and every member, keep a counter for every member and channel that is raised for each message, or keep one latest number for each channel and a read position for each member, and subtract.

The first program has 1,000 students and 60 channels. Channels have 5 to 30, 100 to 400 or 500 to 1,000 members, and each student is in 6.2 channels on average. 20,000 messages arrive in random channels. In the counter design 1 update in 500 is lost, a rare failure that a retry cannot always fix.

Output

1000 students, 60 channels, 20000 messages, the largest channel has 910 members.
Each student is in 6.2 channels on average.

how unread is kept                 writes for the   writes for one   reads to draw one  members with a
                                   20000 messages   message, largest sidebar            wrong badge
row for every message and member   2117176          910              count of rows      0
counter for every member + channel 2117176          910              1 per channel      3052
latest number + read position      20000            1                2 per channel      0
  • A row for every message and member: 2,117,176 writes for 20,000 messages, and 910 rows for one message in the largest channel. The badge is exact but every post becomes a large write.

  • A counter for every member and channel: the same 2,117,176 writes, and 3,052 members ended with a wrong badge, because a lost update is never noticed and never corrected.

  • Latest number and read position: 20,000 writes, 1 for each message, and 0 wrong badges. The sidebar needs 2 reads for each channel, a batch of lookups.

Dry run. A channel's latest number is 120 and a member's read position is 95, so the badge is 25. The member opens the channel, and the client sends up_to: 120, so the badge is 0 on every device. Message 121 arrives, the latest number becomes 121, and the badge is 1, with no write to any member row. Mentions are different: they cannot be derived from two numbers, so keep a small row for each mention (member, channel, number), and delete it when the read position passes it.

6. Delivering to Big Channels

A channel such as the announcements of a whole college can have 20,000 members. A message there should not become 20,000 pushes of the full text, because most members are not looking at the channel. A member needs the text only when the channel is on screen. Otherwise a small signal that something is new is enough.

The second program has 100 channels. 5 in 100 are announcement channels with 5,000 to 20,000 members, 25 in 100 have 200 to 1,000, and 70 in 100 have 5 to 40. Three in 10 members are online, and of those 5 in 100 are looking at a big channel (4 in 10 at a small one). In a busy minute each channel gets 10 to 50 messages. A message is 600 bytes and a dot is 40.

Output

100 channels, 2994 messages in a busy minute, the largest channel has 17750 members.

what is pushed                           events         MB         events for one message in the largest channel
every message to every online member     985145         591.0      5325
message to viewers, dot per message      985145         68.8       5325
message to viewers, one dot per channel  94116          33.1       266
  • Every message to every online member: 985,145 events and 591.0 MB, and one message in the largest channel (17,750 members) made 5,325 pushes.

  • Message to viewers, a dot for each message: the same 985,145 events but 68.8 MB, since a dot is small.

  • Message to viewers, one dot per channel: 94,116 events and 33.1 MB, 10.5 times fewer events and 17.9 times fewer bytes than the first design, and 266 pushes for one message in the largest channel.

The client tells the server which channel it is looking at when it changes, and the fan-out keeps that in memory. Opening a channel fetches the latest messages over HTTP, so a missed push costs nothing. The first dot for a channel is sent only when it goes from read to unread, and the client then holds the badge until the member opens the channel. This is the same push-or-pull split as in Design Message Queue, applied to people.

7. Search with Permissions

Search must return messages from the member's workspace only, and from private channels only if they belong to them. Where the permission check runs matters a great deal. Three ways: one global index where the newest 50 matches are cut first and the disallowed ones removed afterwards, one global index where the permission check is part of the search, and one index for each workspace with the same check.

The third program has 20 workspaces (workspace 0 and 1 hold 3 in 10 messages each) and 60,000 messages. 3 in 10 messages are in private channels (5 per workspace), and a student is a member of each private channel with chance 4 in 10. A search word appears in 2 in 100 messages, which is 1,211 messages. 1,000 searches ask for the 20 newest results the student may see.

Output

1211 messages contain the search word. 1000 searches, 20 results wanted each.

how the search runs                    results per search searches with a short postings read per search
                                       (of 20)            page (20+ existed)   
global index, filter the newest 50     7.5                757                  50.0
global index, filter in the search     19.0               0                    524.6
index for each workspace               19.0               0                    23.9
  • Global index, filter the newest 50: 7.5 results of 20 on average, and 757 of 1,000 searches came up short although 20 or more allowed results existed.

  • Global index, check inside the search: 19.0 results (the rest had fewer than 20 allowed matches) and no short pages, but 524.6 postings read for each search, because most matches belong to other workspaces.

  • An index for each workspace: the same 19.0 and 0 short pages with 23.9 postings read, 22 times fewer.

A separate index per workspace gives isolation by construction, and small workspaces can share an index node while huge ones get their own. Index private-channel membership as part of the query (a list of channel ids the member may read), and update it when membership changes, which also means removing a member from a channel must stop their results at once. Deletion and retention must remove documents from the index too, not only from the database.

8. Sharding

The first instinct is to shard by workspace, which keeps a tenant's data together. That fails when one tenant is huge. Sharding by channel spreads the load, and makes a sidebar, which needs many channels of the same workspace, read many shards. A middle way splits only the huge workspaces by channel.

The fourth program has 400 workspaces on 16 shards: 1 with 50,000 users and 400 channels, 4 with 5,000 users and 100 channels, 45 with 500 users and 25 channels, and 350 with 10 to 50 users and 5 channels. Each user sends 5 to 15 messages a day, and 3 in 10 messages in a workspace go to its first (general) channel.

Output

400 workspaces on 16 shards. The even share would be 6.2 percent of the load.

how data is placed                 busiest shard's share  shards read for a sidebar shards read for a sidebar
                                   of all messages        (huge workspace)         (medium workspace)
shard by workspace                 53.4 percent           1.0                      1.0
shard by channel                   19.8 percent           13.6                     6.9
split big workspaces by channel    20.4 percent           13.6                     1.0
  • Shard by workspace: the busiest shard held 53.4% of all messages (an even share is 6.2%), because the huge workspace lives on one shard. A sidebar reads 1 shard.

  • Shard by channel: the busiest shard fell to 19.8%, but a sidebar in the huge workspace read 13.6 of 16 shards, and 6.9 in a medium workspace.

  • Split only big workspaces by channel: 20.4% on the busiest shard, 1.0 shard for a medium workspace, and still 13.6 for the huge one.

The remaining 19.8% to 20.4% is almost entirely the general channel of the huge workspace, which is about a fifth of all messages on its own. No shard key based on channels can split one channel. To handle it, keep the log of a hot channel in a partitioned store, cache its recent messages, and fan out from memory. A sidebar that reads 13 shards can be served from a per-member cache of badges, since each badge is two numbers. See Consistent Hashing for moving channels between shards without a mass move.

9. Notifications and Mentions

If every message notifies, people mute the app, and then they miss the one message meant for them. The rule should separate the personal from the ordinary: direct messages, mentions by name and broadcasts like @channel are interruptions, and the rest should only raise the unread count or arrive as a digest.

The fifth program has 2,000 students in 25 channels each. In an hour each channel gives 0 to 39 messages, 1 in 100 mentions the student, a student gets 0 to 5 direct messages, and each channel has a 3 in 100 chance of an @channel message. A student has been active in 3 in 10 of their channels. It compares four rules.

Output

2000 students, one hour.

notification rule                    per student  DMs and      @channel     single msgs  messages only in
                                     per hour     mentions     per student  per student  the unread count
every message                        491.7        7.3          0.7          483.6        0
mentions, DMs, @channel              8.0          7.3          0.7          0.0          967289
same + a digest every 15 min         12.0         7.3          0.7          0.0          0
same, @channel to active members     7.5          7.3          0.2          0.0          967289
  • Every message: 491.7 notifications a student an hour, 483.6 of them for ordinary messages.

  • Direct messages, mentions and @channel: 8.0 an hour, 98.4% fewer. 967,289 messages in total were only in the unread count.

  • Plus a digest every 15 minutes: 12.0 an hour, with the ordinary messages arriving as 4 summaries.

  • @channel to active members only: 7.5 an hour, because broadcasts fell from 0.7 to 0.2 for each student.

The @channel case needs care in big channels. One @channel in a 20,000-member channel is 20,000 notifications at once, so rate limit who may use it, ask for confirmation above a size, and send it in batches. Keep a per-member and per-channel preference (all, mentions only, mute) and a do-not-disturb schedule, and send a notification to a phone only if no desktop session is active, since the member would see it twice.

10. Loading History

Members scroll back through history, and new messages keep arriving and old ones are deleted while they scroll. A page asked for by position (skip 50, skip 100) shifts whenever the top of the list changes. A page asked for by time can lose messages that share a second. A cursor made of time and message id has an exact boundary.

The sixth program has 5,000 students who scroll back through 6 pages of 50 messages. Between two page loads 0 to 8 new messages arrive, in 2 of 10 gaps a loaded message is deleted, and in 15 of 100 loads the oldest loaded message shares its second with 1 or 2 older ones.

Output

5000 students scroll back 5 pages, 25000 page loads after the first.

how the next page is asked for         messages shown     messages never     students who saw
                                       twice              shown              a problem
by position (skip 50, 100 ...)         100086             5029               5000
older than this time (seconds)         0                  5687               2774
older than this message (time + id)    0                  0                  0
  • By position: 100,086 messages were shown twice (the new arrivals pushed old ones down into the next page) and 5,029 were never shown (a deletion pulled the next page past them). All 5,000 students saw a problem.

  • By time in whole seconds: nothing repeated, but 5,687 messages were never shown, for 2,774 students, because everything with the time of the boundary was skipped.

  • By time and message id: 0 repeated, 0 skipped, for everyone.

Dry run. A member has loaded messages down to (10:15:02, id 7001). The next request is before=(10:15:02, 7001). The server returns messages whose time is earlier, or whose time is the same and whose id is below 7001. A message with the same second and id 6999 is included, and nothing newer is, so new arrivals and deletions above the boundary do not change the answer.

11. Apps, Files and Retention

Workplace tools post into channels through webhooks, bots and slash commands. Each app token belongs to one workspace, and apps are rate limited by workspace and by channel, so that a build bot that posts 500 messages cannot bury a conversation (see Design a Rate Limiter). Files go to a blob store, and the message holds a pointer. The file inherits the permissions of the channel, and a download link is a short-lived signed URL (see Design Object Storage).

Retention is a workspace policy: keep for 90 days, keep forever, or keep with a legal hold. A deletion must reach the message log, the search index, the caches and the backups in a defined time. Admin export reads the log in order, which is easy because the log is already numbered per channel.

12. Choosing the Design

ConcernChoiceWatch for
OrderA number per channel from the channel serviceClocks and arrival order
UnreadLatest number and a read position per memberCounters that drift
Live deliveryMessage to viewers, one dot per channel to the restPushing text to every member
SearchPermission check in the query, an index per workspaceFiltering after the cut
ShardingBy workspace, huge ones split by channelOne channel that stays hot
AlertsPersonal messages notify, others count or digestNotifying every message
HistoryCursor of time and message idOffsets and bare timestamps

13. Trade-offs to Mention

  • A read position makes unread cheap and exact, and mentions still need their own rows.

  • Sending the text only to viewers saves traffic and needs the client to report which channel is open.

  • An index per workspace isolates tenants and many small indexes cost more to operate.

  • Sharding by channel balances load and makes a sidebar read many shards, which a cache of badges hides.

  • Strict notification rules reduce noise, and a member may miss something that was not marked as personal.

  • A cursor is exact and cannot jump to an arbitrary page number.

14. Common Mistakes

  • Writing an unread row or counter for every member of every message.

  • Pushing the full text of a message in a big channel to every online member.

  • Filtering search results for permissions after cutting the list.

  • Letting one workspace's data share a cache key or an index with another.

  • Sharding by workspace only, with no plan for a huge workspace.

  • Notifying every message and every @channel.

  • Paging history by offset or by a bare timestamp.

15. Interview Questions

  • How is the unread count kept right? As the latest number of the channel minus the member's read position, with mentions in their own rows.

  • How does a message in a 20,000-member channel get delivered? The text goes to members looking at it, one small dot per channel goes to the rest, and everyone else fetches when they open it.

  • How do you keep search permissions correct? The query carries the allowed channels, the index is per workspace, and membership changes update it.

  • How do you shard? By workspace for the many small ones, by channel for the huge ones, and cache the hot channel.

  • How do you avoid notification overload? Notify for DMs, mentions and limited @channel, count and digest the rest.

  • How do you page history? With a cursor of time and message id.

  • How are threads stored? As messages in the same channel log with a parent number, and a reply count on the parent.

16. Practice Problems

  • A channel has latest number 400, and a member's read position is 380 but 3 of the 20 are their own messages. What is the badge? Answer: 20 by subtraction, or 17 if own messages are skipped, and the second needs a rule that moves the read position when the member posts.

  • Why not shard only by channel? Answer: a sidebar reads about 14 of 16 shards, and one busy channel cannot be split anyway, so use channels only where a workspace is too big.

  • A member leaves a private channel. What else must change? Answer: the channel's member list, their read position, their allowed-channel list in search, their notification subscriptions, and cached badges.

  • A webhook posts 300 messages in 10 seconds. What do you do? Answer: rate limit the app per channel, queue the excess, and group the rest into one summary message.

Summary

  • A numbered log for each channel, and a read position for each member.

  • Text to viewers, a dot to the rest, and a fetch when a channel opens.

  • Search with permissions inside the query, and one index for each workspace.

  • Shard by workspace, split the huge workspaces by channel, and cache the hot channel.

  • Notify for personal messages only, and count or digest the rest.

  • History by a cursor of time and message id.

Conclusion

Slack looks like WhatsApp with channels, and the differences are where the interesting questions are: the unread badge that is a subtraction, the message that must not be pushed to 17,750 people, the search that must not leak or run short, the shard key that has to cope with one huge tenant and one hot channel, the notification rule that decides whether people keep the app, and the cursor that keeps history exact. In an interview, state the numbers (messages a second, channel sizes, workspace sizes), then show how each choice follows from them.

Previous topic: Design WhatsApp.

References and further reading

  • Public engineering posts from workplace-messaging companies on message flow, search and sharding by workspace, and on how channel read markers work.

  • Elasticsearch documentation on filtered queries, routing and index per tenant patterns.

  • Documentation on cursor-based (keyset) pagination, for example in the PostgreSQL and Stripe API guides.

  • Firebase Cloud Messaging and Apple Push Notification service documentation, for notification delivery and collapsing.

  • Design WhatsApp, Design Message Queue, Design a Rate Limiter and Consistent Hashing on this site, for the pieces used here.