Every web app repeats the same work: the same homepage query, the same product list, the same user profile, fetched again and again for each visitor. Redis caching for beginners starts with that simple idea, that you can store the result of slow work in fast memory and serve it again without redoing the database query or the API call. Redis is an in-memory store, and a basic setup takes about an hour, including the install, a few cache calls in your app, and a check that the cache is actually being used.
Redis caching for beginners: what it does first
Think of Redis as a shelf next to your app where you put answers you already worked out. When a request comes in, your app checks the shelf first. If the answer is there, it serves it straight away. If it is not there, the app does the slow work, puts the answer on the shelf, and serves it. The next visitor with the same request gets the stored answer instead. That pattern is called cache-aside, and it covers most first uses of Redis. A redis cache web app setup does not replace your database; the database stays the source of truth, and Redis holds copies that can be rebuilt or thrown away at any time.
Install Redis and check it answers
On Ubuntu, install Redis from the official packages, then check that the service is running and answers a ping. The default setup listens only on localhost, which is the safe starting point for a first install, and you should keep it that way until you have a clear reason to open it to another machine.
sudo apt update sudo apt install redis-server -y sudo systemctl enable --now redis-server redis-cli ping redis-cli info server | head -20
The ping should return PONG. If it does not, check the service status and the log at /var/log/redis/redis-server.log before changing config. In /etc/redis/redis.conf, confirm the bind line still points at localhost and protected mode is on. Set a strong password with the requirepass line if anything other than your app on the same machine will connect. For a wider view of how caching fits a whole site, this guide to Nginx caching for WordPress at scale shows where a server cache ends and an app cache begins.
What to cache and what to leave alone
Cache the things that are slow to build, read far more often than they change, and safe to serve slightly out of date. Good first candidates are product lists, article pages, category pages, and user profiles that change rarely. Leave alone anything tied to a single user session that changes on every request, payment data, and anything where serving a stale copy would cause a real mistake, such as stock counts at checkout. A page that changes once an hour is a better cache candidate than a page that changes every ten seconds, because a longer expiry means fewer rebuilds and more cache hits. Start with one page, not the whole app: pick the slowest page you can measure, add caching there, and compare load times before and after. When you plan which data to cache, it helps to compare database choices too, so this comparison of MySQL versus PostgreSQL on a VPS with limited RAM is worth a read.
Keys, expiry, and naming that stays sane
Good keys read like a path: a type, an identifier, and sometimes a version, joined with colons. Examples are user:104, product:42, and homepage:featured:v2. A version suffix lets you change the shape of a cached value without hunting down old keys, because you simply start writing under a new version and let the old keys expire. Avoid keys that grow without a limit, such as one key per search term typed by a user. This is where redis caching patterns explained in plain terms helps most: cache-aside for reads you can rebuild, write-through for data you want in the cache the moment it is written, and a short time-to-live for anything that changes often.
Expiry is your safety net. Set a time-to-live on every key with the EX option, for example 300 seconds for a product list and 3600 seconds for a rarely changed page. Without an expiry, a key that is never deleted stays in memory forever, and a small VPS will slowly fill up. Redis can also evict keys when memory runs out, using a policy such as allkeys-lru, but eviction is a backstop, not a plan. Set maxmemory in the config to a number below your free RAM, and check memory use with redis-cli info memory once a week.
Read and write cache from your app
Most languages have a Redis client library, and the calls you need for a first version are only a handful: connect, get, set with an expiry, and delete. The pattern is the same in Node.js, Python, or PHP, and the code below shows it in Python with the redis package. Your app tries the cache first, falls back to the database on a miss, stores the result, and returns it.
import json
import redis
r = redis.Redis(host="localhost", port=6379, decode_responses=True)
def get_product(product_id):
key = f"product:{product_id}"
cached = r.get(key)
if cached:
return json.loads(cached)
product = load_from_database(product_id)
r.set(key, json.dumps(product), ex=300)
return product
Notice that the cache stores a plain JSON string, which keeps the code simple and easy to inspect with redis-cli get product:42. If Redis itself is down, your app should fall back to the database rather than failing the whole request, because a cache is a speed layer, not a single point of failure. For apps that will grow past one server, managed options are worth knowing about, so compare them in this guide to managed databases: RDS versus Cloud SQL versus PlanetScale.
When the cache goes stale
Stale data is the trade-off of every cache, and you handle it with expiry plus deletes on write. When a product price changes, delete the key for that product at the same time you save the new price, so the next read rebuilds it from the database. When a whole list changes, delete the list key or bump its version. The redis cache invalidation basics are simple once you write them down: every write path that changes data must also clear the keys that hold copies of that data, and every key must have an expiry so a missed delete still fixes itself within minutes. Check your hit rate with redis-cli info stats and look at keyspace_hits and keyspace_misses. A hit rate above 80 percent on a read-heavy page is a good sign. For server-wide health alongside your cache numbers, these Linux server monitoring basics will help you watch RAM, disk, and load while Redis runs.
How much RAM does Redis need for a small app?
Most small apps run well with 256 MB to 512 MB set as maxmemory, which leaves room for the operating system, the app, and the database on the same VPS. Start at 256 MB, watch the used memory figure over a week, and raise it only if evictions are frequent and you still have free RAM to give.
Should I cache every database query?
No. Cache the slow and repeated queries first, and leave fast, rarely repeated queries alone. Caching everything adds code and keys you must maintain, and a query that runs in a few milliseconds from an indexed table gains nothing from a cache round trip.
Your next step is a one-hour test: install Redis, cache a single slow page with a five-minute expiry, and measure that page before and after. If the cached version is clearly faster and the data stays fresh enough for your users, keep the key, add a delete on the write path, and move on to the next slow page.