日本語

Where sessions are stored

MemoryStore keeps the sessions inside one process, so a server that runs more than one instance keeps them in PostgreSQL, SQLite, MySQL, or Redis.

Sessions in memory

MemoryStore keeps the sessions in the process's memory. A restart signs every visitor out, and a second process does not see them, so a request that a load balancer sends to another instance is signed out. That is the same limit as a broadcast channel in Updates with more than one process.

Sessions in a database

On a host that runs more than one instance, or that starts and stops instances as requests come as Vercel does, pass the layer a store that keeps the sessions in a database. SessionManagerLayer::new takes any store that implements SessionStore, so the rest of the code stays as it is.

tower-sessions-sqlx-store keeps them in PostgreSQL, SQLite, or MySQL through sqlx 0.8, so an application on sqlx 0.9 builds both versions and cannot hand the store its own pool. tower-sessions-redis-store connects through the fred client, so it does not share a connection with the redis crate that examples/redis uses.

On Cloudflare Workers

On Cloudflare Workers, tower-sessions 0.14 needs two features in the worker's Cargo.toml: getrandom = { version = "0.2", features = ["js"] }, without which it does not build for wasm32-unknown-unknown, and time = { version = "0.3", features = ["wasm-bindgen"] }, without which saving a session panics with time not implemented on this platform. With both, the layer kept a session under wrangler dev. A worker's isolates share no memory, so MemoryStore there signs a visitor out whenever a request reaches another isolate. A store that keeps the sessions in D1 or KV implements SessionStore, and this guide has not written one.

Next

  • Storing data keeps the application's own data in the same databases.
  • Deploying covers the hosts that run more than one instance.