BosskuAI Redis Caching and Queues
Use this skill when Redis, queues, caching, sessions, distributed locks, rate limits, or background workers affect correctness or performance.
How this differs from nearby skills
bosskuai-database-engineering: database-backed queues (SKIP LOCKED) and the slow query a cache would hide.
bosskuai-performance-profiling: proves a cache is needed.
bosskuai-aws-deployment: ElastiCache and SQS provisioning.
Operating principles
- Cache only when the source of truth and invalidation rule are clear.
- Make queued jobs idempotent, observable, and safe to retry.
- Use locks for coordination, not as a substitute for database constraints.
- Separate queue names by priority and workload type.
- Queues, locks, and sessions need
maxmemory-policy noeviction; caches use allkeys-lru. Split them when they share memory, or jobs vanish under pressure. Check the live values with CONFIG GET maxmemory-policy and CONFIG GET appendfsync, not the config file.
Checklist
- Versions:
INFO server returns redis_version, the client version comes from composer.lock (predis, or the phpredis extension through phpversion('redis')), and the Laravel version comes from the composer files. Check the latest stable release on redis.io and in the Laravel release notes, and write down the date.
- Cache key names include tenant/user/context where needed.
- TTL and invalidation rule are defined.
- Job timeout, retry count, backoff, uniqueness, and failure handling are explicit.
- Workers are supervised and can be restarted safely during deployment. Laravel workers keep the code they loaded, so run
php artisan queue:restart after each deploy (or php artisan horizon:terminate when you use Horizon).
- Each job's
timeout is shorter than the connection's retry_after (with a margin), or the job runs twice.
- Long-running jobs chunk work and do not serialize heavy Eloquent models blindly.
- Rate limits protect expensive endpoints and third-party APIs.
- Redis is not publicly exposed and has persistence/backup settings appropriate to its role.
- Walk the scenarios in
../../references/checklists/engineering-quality-bar.md section 3. Done when each scenario that can reach the change is tested or named untested. Queues: concurrent workers, retries, a deploy mid-run. Cache keys: another tenant, each role.
Guardrails
- Do not cache authorization-sensitive data without tenant/user scoping.
- Do not use cache to hide slow queries before checking indexes.
- Do not rely on Redis as the only durable source of business-critical truth.
- Do not let failed jobs pile up without alerting.
Output format
Workload: [cache / queue / lock / rate limit / session]
Versions and live settings: [Redis, client, Laravel; maxmemory-policy, appendfsync]
Correctness risk: [main risk]
Recommended pattern: [Laravel/Redis pattern]
Operational settings: [workers, TTL, retry, monitoring]
Verification: [tests/commands/logs]
References
../../references/playbooks/bosskuai-redis-caching-queues-playbook.md
../../references/checklists/redis-caching-queues-checklist.md
../../references/checklists/expert-cofounder-stack-checklist.md
../../references/checklists/engineering-quality-bar.md
Pattern details
- Cache-aside for read-heavy data that tolerates slight staleness; write-through (write the database first, then refresh the cache) when stale reads are not acceptable. Always set a TTL; keys without one grow until memory pressure hits. Match TTL to data: sessions about a day, API response caches minutes, rate-limit keys the window length, short-lived tokens 5-10 minutes.
- Invalidate groups of related keys by registering them under a tag set with its own TTL, then delete the members and the set together.
- A single command is atomic; multi-step flows need a Lua script or MULTI/EXEC. Rate limits: fixed window (INCR plus EXPIRE in one transaction) for low-traffic endpoints, sliding window in a Lua script when accuracy matters.
- Locks: acquire with a unique token via
SET key token NX PX ttl; release only if the stored token still matches (compare-and-delete in Lua). Set the TTL above the expected job duration and release in finally. Single-node locks only; multi-node needs a vetted Redlock implementation, and a lock never replaces a database constraint.
- Pub/Sub is fire-and-forget. Use Streams when you need delivery guarantees, consumer groups or replay: cap length (
maxlen), read through a consumer group, and acknowledge only after processing succeeds.
- Cache-miss stampede on a cold or just-expired hot key: lock around the refill and re-check the cache after acquiring the lock (a cross-process lock when multiple workers run), or expire early with jitter.
- Avoid
KEYS * in production (use SCAN), blobs above roughly 100 KB (store a reference to object storage), and mass deletes with a broad flush command; scope deletes by key pattern.
- Client config: connection pool sized to the workload with connect and socket timeouts; eviction policy per instance as above.
- Keys and shapes change on deploy. Version a key (for example a
v2: segment) when its value shape changes, because old entries stay until their TTL. Prefer arrays or scalars for cached values. If you must cache an object, version its key whenever its class is renamed, moved or removed. Otherwise the old class name comes back as an object of __PHP_Incomplete_Class, and calls to its methods fail.
- Jobs queued before a deploy keep their class name and data. Renaming or removing the job class makes those jobs fail, so keep the old class until the queue is empty, or drain the queue before the deploy. A job that uses
SerializesModels reloads its model when it runs; if the row was deleted, the job fails with ModelNotFoundException. A job dispatched inside an open transaction can run before the commit and miss the row; use ->afterCommit() on the dispatch.
- Replication is asynchronous. After a failover, a lock or a queued job that the old primary had acknowledged can be missing.
WAIT narrows that gap but does not close it, so keep the database as the final guard. With appendfsync everysec, a crash can lose about one second of writes.
Adapted in part from ECC skills/redis-patterns (affaan-m/ECC, MIT).
1---2name: bosskuai-redis-caching-queues3description: Use when Redis, queues, or caching affect correctness or performance. Covers Redis caching, Laravel queues, cache invalidation, locks, rate limits, sessions, Horizon-style worker operations, and queue performance debugging.4metadata:5 source: https://skillmd.com/skills/wankimmy/bosskuai-redis-caching-queues6---78# BosskuAI Redis Caching and Queues910Use this skill when Redis, queues, caching, sessions, distributed locks, rate limits, or background workers affect correctness or performance.1112## How this differs from nearby skills1314- **`bosskuai-database-engineering`**: database-backed queues (`SKIP LOCKED`) and the slow query a cache would hide.15- **`bosskuai-performance-profiling`**: proves a cache is needed.16- **`bosskuai-aws-deployment`**: ElastiCache and SQS provisioning.1718## Operating principles1920- Cache only when the source of truth and invalidation rule are clear.21- Make queued jobs idempotent, observable, and safe to retry.22- Use locks for coordination, not as a substitute for database constraints.23- Separate queue names by priority and workload type.24- Queues, locks, and sessions need `maxmemory-policy noeviction`; caches use `allkeys-lru`. Split them when they share memory, or jobs vanish under pressure. Check the live values with `CONFIG GET maxmemory-policy` and `CONFIG GET appendfsync`, not the config file.2526## Checklist2728- Versions: `INFO server` returns `redis_version`, the client version comes from `composer.lock` (predis, or the phpredis extension through `phpversion('redis')`), and the Laravel version comes from the composer files. Check the latest stable release on redis.io and in the Laravel release notes, and write down the date.29- Cache key names include tenant/user/context where needed.30- TTL and invalidation rule are defined.31- Job timeout, retry count, backoff, uniqueness, and failure handling are explicit.32- Workers are supervised and can be restarted safely during deployment. Laravel workers keep the code they loaded, so run `php artisan queue:restart` after each deploy (or `php artisan horizon:terminate` when you use Horizon).33- Each job's `timeout` is shorter than the connection's `retry_after` (with a margin), or the job runs twice.34- Long-running jobs chunk work and do not serialize heavy Eloquent models blindly.35- Rate limits protect expensive endpoints and third-party APIs.36- Redis is not publicly exposed and has persistence/backup settings appropriate to its role.37- Walk the scenarios in `../../references/checklists/engineering-quality-bar.md` section 3. Done when each scenario that can reach the change is tested or named untested. Queues: concurrent workers, retries, a deploy mid-run. Cache keys: another tenant, each role.3839## Guardrails4041- Do not cache authorization-sensitive data without tenant/user scoping.42- Do not use cache to hide slow queries before checking indexes.43- Do not rely on Redis as the only durable source of business-critical truth.44- Do not let failed jobs pile up without alerting.4546## Output format4748```text49Workload: [cache / queue / lock / rate limit / session]50Versions and live settings: [Redis, client, Laravel; maxmemory-policy, appendfsync]51Correctness risk: [main risk]52Recommended pattern: [Laravel/Redis pattern]53Operational settings: [workers, TTL, retry, monitoring]54Verification: [tests/commands/logs]55```5657## References5859- `../../references/playbooks/bosskuai-redis-caching-queues-playbook.md`60- `../../references/checklists/redis-caching-queues-checklist.md`61- `../../references/checklists/expert-cofounder-stack-checklist.md`62- `../../references/checklists/engineering-quality-bar.md`6364## Pattern details6566- Cache-aside for read-heavy data that tolerates slight staleness; write-through (write the database first, then refresh the cache) when stale reads are not acceptable. Always set a TTL; keys without one grow until memory pressure hits. Match TTL to data: sessions about a day, API response caches minutes, rate-limit keys the window length, short-lived tokens 5-10 minutes.67- Invalidate groups of related keys by registering them under a tag set with its own TTL, then delete the members and the set together.68- A single command is atomic; multi-step flows need a Lua script or MULTI/EXEC. Rate limits: fixed window (INCR plus EXPIRE in one transaction) for low-traffic endpoints, sliding window in a Lua script when accuracy matters.69- Locks: acquire with a unique token via `SET key token NX PX ttl`; release only if the stored token still matches (compare-and-delete in Lua). Set the TTL above the expected job duration and release in `finally`. Single-node locks only; multi-node needs a vetted Redlock implementation, and a lock never replaces a database constraint.70- Pub/Sub is fire-and-forget. Use Streams when you need delivery guarantees, consumer groups or replay: cap length (`maxlen`), read through a consumer group, and acknowledge only after processing succeeds.71- Cache-miss stampede on a cold or just-expired hot key: lock around the refill and re-check the cache after acquiring the lock (a cross-process lock when multiple workers run), or expire early with jitter.72- Avoid `KEYS *` in production (use `SCAN`), blobs above roughly 100 KB (store a reference to object storage), and mass deletes with a broad flush command; scope deletes by key pattern.73- Client config: connection pool sized to the workload with connect and socket timeouts; eviction policy per instance as above.74- Keys and shapes change on deploy. Version a key (for example a `v2:` segment) when its value shape changes, because old entries stay until their TTL. Prefer arrays or scalars for cached values. If you must cache an object, version its key whenever its class is renamed, moved or removed. Otherwise the old class name comes back as an object of `__PHP_Incomplete_Class`, and calls to its methods fail.75- Jobs queued before a deploy keep their class name and data. Renaming or removing the job class makes those jobs fail, so keep the old class until the queue is empty, or drain the queue before the deploy. A job that uses `SerializesModels` reloads its model when it runs; if the row was deleted, the job fails with `ModelNotFoundException`. A job dispatched inside an open transaction can run before the commit and miss the row; use `->afterCommit()` on the dispatch.76- Replication is asynchronous. After a failover, a lock or a queued job that the old primary had acknowledged can be missing. `WAIT` narrows that gap but does not close it, so keep the database as the final guard. With `appendfsync everysec`, a crash can lose about one second of writes.7778Adapted in part from ECC skills/redis-patterns (affaan-m/ECC, MIT).