replica.go91 lines, 2.7 KB
| 1 | // Package replica sends reads to a copy of the database, unless they must see |
| 2 | // a write that the copy may not have yet. |
| 3 | package replica |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "log/slog" |
| 8 | "strconv" |
| 9 | "sync/atomic" |
| 10 | "time" |
| 11 | |
| 12 | "github.com/jackc/pgx/v5/pgxpool" |
| 13 | "github.com/redis/go-redis/v9" |
| 14 | ) |
| 15 | |
| 16 | // Fresh is how long a user's reads go to the primary after they wrote. |
| 17 | const Fresh = 10 * time.Second |
| 18 | |
| 19 | // Reads decides where a read goes: to the replica, or to the primary. |
| 20 | type Reads struct { |
| 21 | primary *pgxpool.Pool |
| 22 | replica *pgxpool.Pool // nil: there is none, and every read goes to the primary |
| 23 | rdb *redis.Client |
| 24 | usable atomic.Bool // the replica answers, and it is not too far behind |
| 25 | } |
| 26 | |
| 27 | func New(primary, replica *pgxpool.Pool, rdb *redis.Client) *Reads { |
| 28 | return &Reads{primary: primary, replica: replica, rdb: rdb} |
| 29 | } |
| 30 | |
| 31 | // key is where Redis remembers, for every server, that a user just wrote. |
| 32 | func key(user int64) string { |
| 33 | return "wrote:" + strconv.FormatInt(user, 10) |
| 34 | } |
| 35 | |
| 36 | // Wrote marks a user who is about to write. It comes before the write, |
| 37 | // so a read that follows the answer finds the mark. |
| 38 | func (r *Reads) Wrote(ctx context.Context, user int64) { |
| 39 | // If Redis is away, For can't ask either, and it sends the reads to the primary. |
| 40 | r.rdb.Set(ctx, key(user), 1, Fresh) |
| 41 | } |
| 42 | |
| 43 | // For returns the pool for a read that is made for a user, or for nobody (0). |
| 44 | func (r *Reads) For(ctx context.Context, user int64) *pgxpool.Pool { |
| 45 | if r.replica == nil || !r.usable.Load() { |
| 46 | return r.primary |
| 47 | } |
| 48 | if user != 0 { |
| 49 | // Did they write a moment ago? If Redis can't say, we take it as a yes. |
| 50 | if n, err := r.rdb.Exists(ctx, key(user)).Result(); err != nil || n > 0 { |
| 51 | return r.primary |
| 52 | } |
| 53 | } |
| 54 | return r.replica |
| 55 | } |
| 56 | |
| 57 | // Behind is how far the replica may fall behind before nobody reads from it. |
| 58 | // It is well under Fresh, so a write is on the replica before its mark is gone. |
| 59 | const Behind = 3 * time.Second |
| 60 | |
| 61 | // Watch asks the replica once a second if it can take reads, until ctx is done. |
| 62 | func (r *Reads) Watch(ctx context.Context) { |
| 63 | for r.replica != nil { |
| 64 | r.check(ctx) |
| 65 | select { |
| 66 | case <-ctx.Done(): |
| 67 | return |
| 68 | case <-time.After(time.Second): |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | // check asks the replica: are you a replica, and have you caught up, or is |
| 74 | // what you haven't replayed yet younger than Behind? |
| 75 | func (r *Reads) check(ctx context.Context) { |
| 76 | var ok bool |
| 77 | err := r.replica.QueryRow(ctx, ` |
| 78 | select pg_is_in_recovery() and coalesce( |
| 79 | pg_last_wal_receive_lsn() = pg_last_wal_replay_lsn() |
| 80 | or now() - pg_last_xact_replay_timestamp() < $1 * interval '1 second', false)`, |
| 81 | Behind.Seconds()).Scan(&ok) |
| 82 | ok = err == nil && ok |
| 83 | if r.usable.Swap(ok) == ok { |
| 84 | return |
| 85 | } |
| 86 | if ok { |
| 87 | slog.Info("reads go to the replica") |
| 88 | } else { |
| 89 | slog.Warn("reads go to the primary", "error", err) |
| 90 | } |
| 91 | } |