|
| 1 | +# How To Choose A State Backend |
| 2 | + |
| 3 | +Runtime state is required: the runtime stores subscribed-thread membership, |
| 4 | +event dedupe marks, and thread lock leases in a `chat.State`. It is |
| 5 | +coordination state, not product state — keep your application's own data in |
| 6 | +your own database keyed by `ThreadID`. |
| 7 | + |
| 8 | +Four implementations ship today: |
| 9 | + |
| 10 | +| Backend | Module | Use for | |
| 11 | +| --- | --- | --- | |
| 12 | +| Memory | `github.com/coder/chat/state/memory` (in the core module) | Tests and local demos. Lost on restart. | |
| 13 | +| Redis | `github.com/coder/chat/state/redis` | Production, horizontally scaled deployments. | |
| 14 | +| Postgres | `github.com/coder/chat/state/postgres` | Production, when Postgres is already your coordination store. | |
| 15 | +| NATS JetStream | `github.com/coder/chat/state/nats` | Production, when you already run NATS with JetStream. | |
| 16 | + |
| 17 | +Redis, Postgres, and NATS live in separate Go modules so applications that only |
| 18 | +need core, Slack, or memory state do not pull their dependencies. |
| 19 | + |
| 20 | +## Memory |
| 21 | + |
| 22 | +```go |
| 23 | +import "github.com/coder/chat/state/memory" |
| 24 | + |
| 25 | +bot, err := chat.New(ctx, |
| 26 | + chat.WithState(memory.New()), |
| 27 | + chat.WithAdapter(adapter), |
| 28 | +) |
| 29 | +``` |
| 30 | + |
| 31 | +Memory state is for tests and local development only. Subscriptions and dedupe |
| 32 | +data vanish when the process exits, so a restarted bot forgets which threads it |
| 33 | +was in and may re-handle redelivered events. |
| 34 | + |
| 35 | +## Redis |
| 36 | + |
| 37 | +```sh |
| 38 | +go get github.com/coder/chat/state/redis |
| 39 | +``` |
| 40 | + |
| 41 | +```go |
| 42 | +import ( |
| 43 | + "github.com/redis/go-redis/v9" |
| 44 | + |
| 45 | + chatredis "github.com/coder/chat/state/redis" |
| 46 | +) |
| 47 | + |
| 48 | +redisState, err := chatredis.New(ctx, chatredis.Options{ |
| 49 | + Client: redis.NewClient(&redis.Options{Addr: os.Getenv("REDIS_ADDR")}), |
| 50 | + Prefix: "mybot", // see "One namespace per bot application" below |
| 51 | +}) |
| 52 | +``` |
| 53 | + |
| 54 | +The runnable example, including a `compose.yaml` for a local Redis, is |
| 55 | +[`examples/slack-redis-state`](../../examples/slack-redis-state/README.md). |
| 56 | + |
| 57 | +## Postgres |
| 58 | + |
| 59 | +```sh |
| 60 | +go get github.com/coder/chat/state/postgres |
| 61 | +``` |
| 62 | + |
| 63 | +```go |
| 64 | +import ( |
| 65 | + "github.com/jackc/pgx/v5/pgxpool" |
| 66 | + |
| 67 | + chatpostgres "github.com/coder/chat/state/postgres" |
| 68 | +) |
| 69 | + |
| 70 | +pool, err := pgxpool.New(ctx, os.Getenv("DATABASE_URL")) |
| 71 | +if err != nil { |
| 72 | + return err |
| 73 | +} |
| 74 | + |
| 75 | +pgState, err := chatpostgres.New(ctx, chatpostgres.Options{ |
| 76 | + Pool: pool, |
| 77 | + Namespace: "mybot", // see "One namespace per bot application" below |
| 78 | +}) |
| 79 | +``` |
| 80 | + |
| 81 | +The Postgres state initializes its own schema (subscription, event, and lock |
| 82 | +tables) on startup. The runnable example is |
| 83 | +[`examples/slack-postgres-state`](../../examples/slack-postgres-state/README.md). |
| 84 | + |
| 85 | +## NATS JetStream |
| 86 | + |
| 87 | +```sh |
| 88 | +go get github.com/coder/chat/state/nats |
| 89 | +``` |
| 90 | + |
| 91 | +```go |
| 92 | +import ( |
| 93 | + natsgo "github.com/nats-io/nats.go" |
| 94 | + |
| 95 | + chatnats "github.com/coder/chat/state/nats" |
| 96 | +) |
| 97 | + |
| 98 | +conn, err := natsgo.Connect(os.Getenv("NATS_URL")) |
| 99 | +if err != nil { |
| 100 | + return err |
| 101 | +} |
| 102 | + |
| 103 | +natsState, err := chatnats.New(ctx, chatnats.Options{ |
| 104 | + Conn: conn, |
| 105 | + Prefix: "mybot", // see "One namespace per bot application" below |
| 106 | + // DedupeTTL and ThreadLockTTL default to the runtime defaults (24h and |
| 107 | + // 2m) and must match your RuntimeOptions. |
| 108 | +}) |
| 109 | +``` |
| 110 | + |
| 111 | +NATS state stores subscriptions, dedupe marks, and locks in three JetStream |
| 112 | +Key-Value buckets with bucket-level TTLs (see |
| 113 | +[ADR 0014](../adr/0014-nats-state-adapter.md)). Because JetStream TTLs are |
| 114 | +per-bucket, the dedupe and lock TTLs are fixed at construction time. The |
| 115 | +runnable example is |
| 116 | +[`examples/slack-nats-state`](../../examples/slack-nats-state/README.md). |
| 117 | + |
| 118 | +## One Namespace Per Bot Application |
| 119 | + |
| 120 | +The `Prefix`/`Namespace` options default to `chat`. If two *independent* bot |
| 121 | +applications share one Redis, Postgres, or NATS service with the default, |
| 122 | +their subscription, dedupe, and lock records collide — thread IDs carry |
| 123 | +platform tenant/channel identity but no application identity, so app A |
| 124 | +subscribing a thread can route that thread's follow-ups into app B's |
| 125 | +`OnSubscribedMessage`, and one app's locks can suppress the other's events. |
| 126 | +Give every bot application its own stable namespace, shared only by that |
| 127 | +app's replicas (replicas must share the namespace — that is what makes |
| 128 | +dedupe and locking work across them). |
| 129 | + |
| 130 | +## How To Decide |
| 131 | + |
| 132 | +- Writing tests or following the tutorial: use memory. |
| 133 | +- Already running Redis: use Redis. Same for Postgres and NATS — the backends |
| 134 | + are contract-equivalent, so pick the one you already operate. |
| 135 | +- Running more than one bot replica: any of the durable backends works; all |
| 136 | + three implement the same token-owned lock lease and dedupe contract, which is |
| 137 | + what makes horizontal scaling safe. |
| 138 | + |
| 139 | +All backends are exercised by the same conformance suite; Redis and Postgres |
| 140 | +integration tests run against real backends via Testcontainers, and NATS tests |
| 141 | +run against an embedded JetStream server. |
0 commit comments