Skip to content
Tutorials Center
My learning

Topics

Lesson 25 of 37 ยท Scaling and Performance

Caching with the Key-Value Store

Use Heroku's managed Redis-compatible Key-Value Store for caching and queues: provisioning, REDIS_URL, eviction policy, TTLs, cache-aside and common mistakes.

The fastest query is the one you never run. A cache keeps the result of expensive work in memory so the next request can reuse it. On Heroku the usual cache is the Heroku Key-Value Store, a managed, Redis-compatible in-memory database sold as an add-on (older articles call it Heroku Data for Redis or Heroku Redis). It also serves as a queue backend for job libraries such as Sidekiq, RQ or BullMQ.

This lesson shows how to provision it, how to use it as a cache, and the habits that stop a cache from causing bugs. The service cannot run on this site; the CLI lines are read-only, and the Python stand-ins implement the same ideas with a dictionary.

Provisioning

Terminal
heroku addons:create heroku-redis:mini --app my-app
heroku redis:info --app my-app
heroku redis:cli --app my-app
heroku redis:maxmemory --policy allkeys-lru --app my-app

The add-on sets the config var REDIS_URL (the variable keeps this name). Connection strings use redis:// or, for TLS, rediss:// (note the double s). Plan names and sizes change, so check the current plan table. The mini plan is the small entry level; production plans add more memory, persistence and high availability.

Treat a cache as disposable. A cache can be emptied by eviction, a restart or a plan change, so the application must work, only slower, when a key is missing. Never store the only copy of important data in it.

Cache-aside

The most common pattern is cache-aside: look in the cache; on a miss, compute or load from the database, store the result with an expiry, and return it.

flowchart LR
    A["Request"] --> B{"In cache?"}
    B -->|hit| C["Return cached value"]
    B -->|miss| D["Query database"]
    D --> E["Store with TTL"]
    E --> C

On a miss the app does the work once and stores the result for next time.

Python Stand-in: cache-aside with expiry

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The first call misses, the next three hit, and at 61 seconds the entry has expired, so the database is queried again (2 database calls in total).

Choosing a TTL

A time to live (TTL) bounds how stale data can be. Short TTLs mean fresh data and more database load; long TTLs the reverse. Choose from what the user would notice:

  • A product description: hours.
  • A stock count on a checkout page: seconds, or do not cache it.
  • A rendered home page fragment: minutes.

Add a little random jitter to TTLs (for example plus or minus 10 percent) so thousands of keys do not expire in the same second and flood the database (a "stampede").

Invalidation

Phil Karlton's joke says cache invalidation is one of the two hard problems. Two practical approaches:

  1. Expire only. Accept staleness up to the TTL. Simple and robust.
  2. Delete on write. When the app updates a row, it also deletes the cache key (DEL user:7). The next read repopulates it. Remember to do this in every code path that changes the row, including admin tools and background jobs.

Namespaced keys make bulk invalidation easy: put a version in the key (products:v3:list) and bump the version when the shape changes.

Memory and eviction

Redis keeps everything in RAM, and the plan has a memory limit. When it is full, the eviction policy decides what happens. For a cache use allkeys-lru (remove the least recently used key, any key). The default policy on some plans is noeviction, which makes writes fail with an out-of-memory error when full; that is right for a queue and wrong for a pure cache.

Python Stand-in: LRU eviction

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Key b is evicted because a was just read. That is what allkeys-lru does at a much larger scale.

Other uses

  • Sessions and rate limits. Counters with INCR and EXPIRE are cheap and quick.
  • Job queues. Libraries store jobs in Redis lists. Here, use a plan and a policy that never evicts the queue.
  • Pub/sub. Redis can push messages between processes, for example to broadcast chat messages across dynos.

For a queue and a cache use separate instances, so a full cache can never drop jobs.

Connection and security notes

  • Use REDIS_URL from the environment; on newer plans it uses TLS (rediss://), and some clients need an option to accept Heroku's self-signed certificate. Follow the Dev Center article for your library.
  • Redis has no row-level access control: anyone who can connect can read every key. Keep the URL secret and never put personal data into keys.
  • Heroku has a limit on connections per plan, like Postgres; pool them (connections).

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which environment variable does the Key-Value Store add-on set?

    The add-on keeps the familiar name REDIS_URL.
  2. 2. What is cache-aside?

    The application reads the cache first and fills it on a miss, so the cache holds only what was needed.
  3. 3. Which eviction policy fits a pure cache?

    allkeys-lru drops the least recently used keys when memory is full, which is what a cache wants.
  4. 4. Why use a TTL with jitter?

    Random variation spreads expirations so the database does not get a spike of simultaneous misses.

Summary

  • The Key-Value Store is managed Redis; REDIS_URL is the connection string.
  • Use cache-aside with a TTL, and design the app to survive a missing key.
  • Invalidate by expiry, by deleting on write, or by versioned keys.
  • Choose allkeys-lru for caches, and keep queues on a separate instance that never evicts.

Next: Router Metrics and Request Timeouts