Skip to content

Commit fdca2b0

Browse files
tlongwell-blocknpub1t2tgm7d8f995uqvmnm8h88sg3wnpp9a5xysjf6dg3tjmgt3ltulqdp8ehr
andauthored
perf(presence): reduce heartbeat frequency (#3783)
## Summary - send desktop presence heartbeats every 60 seconds instead of every 30 seconds - extend presence TTL from 90 to 180 seconds to preserve the existing three-heartbeat expiry window - add mutation-sensitive tests that pin the one-minute / three-window timing contract - update presence documentation to match This halves steady-state **desktop** presence `SET` + `PUBLISH` traffic while retaining tolerance for two missed heartbeats. Mobile already uses a 60-second heartbeat, so the fleet-wide reduction depends on desktop's share of connected clients. ## Rollout order Deploy the relay TTL increase before shipping the desktop heartbeat change. Old desktop + new relay is safe; new desktop + old relay leaves only a 90-second TTL on a 60-second cadence and can flap after one missed heartbeat. ## Verification At initial live-test commit `00816e233b187bc5ba12c667d675ed050a8cc1c9`: - isolated clean-room relay built from the exact SHA against fresh Postgres, Redis, and MinIO - live Redis `MONITOR` observed kind-20001 writes as `SET ... EX 180`, global `PUBLISH`, and clean-disconnect / explicit-offline `DEL` - normal workflows passed: channel create/update/archive/unarchive; message send/get/reply/thread/search; archived-channel write rejection and resumed write after unarchive At follow-up commit `bf38a8c5c96f196ff8ee46e48d4141ee7811f186`: - `pnpm -C desktop test` — 3829 passed - `pnpm -C desktop typecheck` - `cargo test -p buzz-pubsub` — 24 passed, 11 Redis-dependent tests ignored - mutation probes fail when the server TTL changes to `999999` or the desktop heartbeat changes back to 30 seconds - `git diff --check` The pre-push suite's relevant checks passed, but its unrelated Tauri clippy step fails on current `origin/main`: `desktop/src-tauri/src/linux_media.rs` has three dead-code warnings on macOS. This PR does not modify that file, so the branch was pushed after independently running the suites above. ## Buzz context Originating channel: `buzz-redis-cluster-mode` (`f4e36d32-afdb-447f-8c87-ab003e069d18`) --------- Signed-off-by: npub1t2tgm7d8f995uqvmnm8h88sg3wnpp9a5xysjf6dg3tjmgt3ltulqdp8ehr <5a968df9a7494b4e019b9ecf739e088ba61097b4312124e9a88ae5b42e3f5f3e@buzz.block.builderlab.xyz> Co-authored-by: npub1t2tgm7d8f995uqvmnm8h88sg3wnpp9a5xysjf6dg3tjmgt3ltulqdp8ehr <5a968df9a7494b4e019b9ecf739e088ba61097b4312124e9a88ae5b42e3f5f3e@buzz.block.builderlab.xyz>
1 parent dc8cf1b commit fdca2b0

6 files changed

Lines changed: 32 additions & 9 deletions

File tree

‎ARCHITECTURE.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ The subscriber uses a **dedicated** `redis::aio::PubSub` connection — not from
447447

448448
**Reconnection:** exponential backoff 1s → 30s (`backoff_secs * 2`). Backoff resets to 1s only after a clean stream end, not on each reconnect attempt.
449449

450-
**Presence:** `SET buzz:presence:{pubkey_hex} {status} EX 90` — 90-second TTL (3× the 30-second heartbeat interval). Single missed heartbeat does not cause presence flap.
450+
**Presence:** `SET buzz:presence:{pubkey_hex} {status} EX 180` — 180-second TTL (3× the 60-second heartbeat interval). Single missed heartbeat does not cause presence flap.
451451

452452
**Typing indicators:**
453453
```
@@ -797,7 +797,7 @@ Docker Compose provides the full local development stack. All services include h
797797
| Pattern | Type | TTL | Purpose |
798798
|---------|------|-----|---------|
799799
| `buzz:channel:{uuid}` | Pub/Sub channel | — | Event fan-out (single-community form; shared multi-community Redis must use `buzz:{community}:channel:{uuid}` or equivalent) |
800-
| `buzz:presence:{pubkey_hex}` | String | 90s | Online/away status (single-community form; shared multi-community Redis must scope by community) |
800+
| `buzz:presence:{pubkey_hex}` | String | 180s | Online/away status (single-community form; shared multi-community Redis must scope by community) |
801801
| `buzz:typing:{channel_uuid}` | Sorted Set | 60s | Active typers (5s window; shared multi-community Redis must scope by community) |
802802
803803
### Full-Text Search (Postgres FTS)

‎crates/buzz-pubsub/src/lib.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ impl PubSubManager {
328328
publisher::publish_event(&self.pool, ctx, topic, event).await
329329
}
330330

331-
/// Set presence with 60s TTL. Call on connect and every 30s heartbeat.
331+
/// Set presence with 180s TTL. Call on connect and every 60s heartbeat.
332332
pub async fn set_presence(
333333
&self,
334334
ctx: &TenantContext,

‎crates/buzz-pubsub/src/presence.rs‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Presence tracking — online/away status with TTL.
22
//!
3-
//! Stored as `SET buzz:{community}:presence:{pubkey_hex} "online" EX 90`.
4-
//! TTL is 3x the 30s heartbeat interval so a single missed heartbeat doesn't
3+
//! Stored as `SET buzz:{community}:presence:{pubkey_hex} "online" EX 180`.
4+
//! TTL is 3x the 60s heartbeat interval so a single missed heartbeat doesn't
55
//! cause presence flap. Clean disconnect deletes immediately.
66
77
use buzz_core::TenantContext;
@@ -12,8 +12,8 @@ use std::collections::HashMap;
1212
use crate::error::PubSubError;
1313
use crate::topic::BUZZ_PREFIX;
1414

15-
/// 3x the 30s heartbeat — single missed heartbeat won't cause presence flap.
16-
pub const PRESENCE_TTL_SECS: u64 = 90;
15+
/// 3x the 60s heartbeat — single missed heartbeat won't cause presence flap.
16+
pub const PRESENCE_TTL_SECS: u64 = 180;
1717

1818
/// Returns the Redis key for the presence entry of `pubkey` under `ctx`.
1919
pub fn presence_key(ctx: &TenantContext, pubkey: &PublicKey) -> String {
@@ -109,6 +109,12 @@ mod tests {
109109
TenantContext::resolved(CommunityId::from_uuid(Uuid::from_u128(id)), host)
110110
}
111111

112+
#[test]
113+
fn presence_ttl_is_three_one_minute_heartbeat_windows() {
114+
assert_eq!(PRESENCE_TTL_SECS, 180);
115+
assert_eq!(PRESENCE_TTL_SECS, 3 * 60);
116+
}
117+
112118
#[test]
113119
fn test_presence_key_format() {
114120
let pubkey = make_pubkey();

‎desktop/src/features/presence/hooks.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ import {
1111
mergePresenceUpdate,
1212
parseLivePresenceEvent,
1313
presenceQueryWantsPubkey,
14+
PRESENCE_HEARTBEAT_INTERVAL_MS,
15+
PRESENCE_TTL_SECONDS,
1416
resolveAutomaticPresenceStatus,
1517
} from "@/features/presence/lib/presence";
1618
import type { PresenceLookup, PresenceStatus } from "@/shared/api/types";
1719

18-
const PRESENCE_HEARTBEAT_INTERVAL_MS = 30_000;
1920
const PRESENCE_STATUS_TICK_INTERVAL_MS = 30_000;
2021
const PRESENCE_ACTIVITY_THROTTLE_MS = 1_000;
21-
const PRESENCE_TTL_SECONDS = 90;
2222
const PRESENCE_PREFERENCE_STORAGE_KEY = "buzz-presence-preference";
2323

2424
type PresencePreference = "auto" | "away" | "offline" | null;

‎desktop/src/features/presence/lib/presence.test.mjs‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,25 @@ import {
55
mergePresenceUpdate,
66
parseLivePresenceEvent,
77
presenceQueryWantsPubkey,
8+
PRESENCE_HEARTBEAT_INTERVAL_MS,
89
PRESENCE_IDLE_TIMEOUT_MS,
10+
PRESENCE_TTL_SECONDS,
911
resolveAutomaticPresenceStatus,
1012
} from "./presence.ts";
1113

1214
const WILL = "8e39cba681211b3782d0e4483e9343719b9b7be66515252da5491f26421896b1";
1315
const OTHER =
1416
"44b8e82baaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
1517

18+
test("presence heartbeat is one minute with a three-window TTL", () => {
19+
assert.equal(PRESENCE_HEARTBEAT_INTERVAL_MS, 60_000);
20+
assert.equal(PRESENCE_TTL_SECONDS, 180);
21+
assert.equal(
22+
PRESENCE_TTL_SECONDS,
23+
3 * (PRESENCE_HEARTBEAT_INTERVAL_MS / 1000),
24+
);
25+
});
26+
1627
test("merge adds an absent pubkey going online (the core bug)", () => {
1728
const old = {};
1829
const next = mergePresenceUpdate(old, WILL, "online");

‎desktop/src/features/presence/lib/presence.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ export function mergePresenceUpdate(
3636
return { ...old, [pubkey]: status };
3737
}
3838

39+
// Keep the local optimistic cache and relay expiry at three heartbeat windows.
40+
// The relay owns the authoritative TTL; deploy its TTL increase before shipping
41+
// a desktop build with a slower heartbeat.
42+
export const PRESENCE_HEARTBEAT_INTERVAL_MS = 60_000;
43+
export const PRESENCE_TTL_SECONDS = 3 * (PRESENCE_HEARTBEAT_INTERVAL_MS / 1000);
44+
3945
// Away means "human not at the machine" (Slack/Discord semantics), never
4046
// "Buzz is not the focused window". OS-wide idle is authoritative when the
4147
// platform exposes it; otherwise fall back to in-app activity.

0 commit comments

Comments
 (0)