I made a small Slack app called Team Connection. It pairs coworkers (or club members) for coffee chats every week or two, and posts birthdays and work anniversaries in one morning message. I wanted it to run for $0, so it lives on Cloudflare Workers' free plan with D1 as the database.
The free plan has three limits that shaped almost every design decision:
- Slack wants an HTTP 200 within 3 seconds or it retries and shows the user an error.
- A Worker invocation can make at most 50 outgoing requests (on the free plan).
- Each invocation gets about 10 ms of CPU.
Here's what those limits pushed me into, plus three bugs that only showed up under stress tests and a real install.
1. Answer Slack first, work later
Every endpoint (events, slash commands, button clicks) verifies the request and then answers immediately. Anything slow goes into ctx.waitUntil() or into a job row in D1.
Verification is the standard v0 HMAC with a 5-minute window. The two details that matter are reading the raw body exactly once and comparing in constant time:
const ts = req.headers.get("x-slack-request-timestamp") ?? "";
const sig = req.headers.get("x-slack-signature") ?? "";
if (!/^\d{1,12}$/.test(ts) || Math.abs(nowSec - Number(ts)) > 300) return reject(401);
const body = await req.text();
const expected = "v0=" + (await hmacSha256Hex(signingSecret, `v0:${ts}:${body}`));
if (!timingSafeEqual(expected, sig)) return reject(401);
Opening a modal is the one place you can't defer for long: trigger_id expires in about 3 seconds. So the handler acks and calls views.open inside waitUntil right away, and any admin check happens in that same deferred task.
2. Treat the 50-request limit as a budget, not a wall
A coffee round for a 500-person channel needs to read the channel, look up each member (to skip bots and deactivated accounts), then open a group DM and post a message for each group. That's well over 50 requests.
So every Slack call goes through a small Budget object, and big work is a job that saves its progress:
class Budget {
used = 0;
constructor(readonly limit: number) {}
canSpend(n = 1) { return this.used + n <= this.limit; }
spend(n = 1) { if (!this.canSpend(n)) throw new BudgetExceeded(); this.used += n; }
}
One cron trigger runs every minute with a budget of 45 (leaving headroom). Each run picks up jobs, does as much as the budget allows, and writes where it stopped. A 500-person round finishes in a few minutes, and no single run comes close to the limit.
For CPU, I avoided users.list on big workspaces (parsing hundreds of KB of JSON eats the 10 ms fast) and used users.info per member with a 7-day cache in D1. Each response is tiny, and the second round in a week costs almost nothing.
3. Send at most once, even if the Worker dies
This was my first real bug. I wrote a test that kills the Worker right after a message is delivered, before it hears back. The job restarted from its last saved state, which was before the groups were saved. It rebuilt the groups, and because the "avoid recent pairs" history now included this round's own pairs, it built different groups. Some people got two intros.
The fixes:
- Save the job's phase and the generated groups before sending anything.
- Write a cursor before each send (at-most-once): if the Worker dies mid-send, that one group is skipped instead of messaged twice.
- Lease each job for 3 minutes so two overlapping cron runs can't process it.
- Claim the daily celebration post with a conditional
UPDATE ... WHERE next_celebration_at = ?, so only one run can post it.
For a social app, a rare missed intro is much better than a duplicate. Duplicates make the bot look broken.
4. Don't give up on jobs that are making progress
The stress test runs 30 simulated workspaces (about 2,600 people) for 6 simulated weeks with 3% network errors, 1% rate limits and random Worker kills. The first run reported 275 missed intros.
The cause: with 4% of calls failing, most minute-batches hit at least one error. Each of those counted as a failed attempt, even when the batch had sent 40 messages first. After 8 "failures" in a row the job was abandoned. Now a run that made any successful call resets the failure count. The second stress run had 0 missed, 0 duplicates and 0 crashes.
5. Unit tests don't validate Block Kit
All the view-building code had tests checking block counts and text lengths. The first real install still showed Slack's "This is still a work in progress" placeholder instead of my Home tab. views.publish returned invalid_arguments.
The culprit: buttons with value: "". Slack rejects an empty value; leave the field out instead. Logging response_metadata.messages from Slack errors would have pointed straight at it, and now it does.
6. Smaller things worth knowing
-
Avoiding repeat pairs is a greedy pass with a short look-ahead plus random swaps, seeded by
team + roundso it's deterministic. With 25+ people and 6 rounds of history it never repeats. For small teams it reaches the true minimum (checked by brute force). - Feb 29 birthdays get celebrated on Feb 28 in non-leap years. Weekend birthdays can move to Friday or Monday.
-
Timezones:
Intl.DateTimeFormatworks in Workers, so "9:00 in the workspace's timezone" is a small helper that converts and re-checks the offset. That handles DST days and zones like +5:45. -
Uninstall (
app_uninstalled, ortokens_revokedfor the bot) deletes every row for that workspace. In the live test that took about 10 seconds. - Tokens are encrypted in D1 with AES-GCM using a Worker secret.
The result
The whole thing is TypeScript with no runtime dependencies. It runs on a single Worker plus D1, and hosting costs $0. The test suite uses vitest with Miniflare's local D1, and also runs the bundled Worker inside workerd.
If you run a club, community or team Slack and want to try it, it's a free beta: Team Connection. It never reads messages, and clubs, schools and nonprofits will stay free. Feedback on what's confusing is very welcome.
Top comments (1)
The 3-second acknowledgement window for Slack slash commands is always where people get tripped up when moving to serverless / edge runtimes.
I really like how you turned the free tier constraints into better architecture. Having to separate the immediate 200 OK from the actual background execution forces you into patterns you honestly should be using in production anyway. Great breakdown of working within edge limits!