{"canonical_url":"https://engineer.company/glossary/redis/","content_markdown":"# Redis\n\n## What it is\n\nRedis is a database server that keeps its whole dataset in memory as typed\nstructures and lets many application processes read and change them over the\nnetwork, one short command at a time. The project describes itself as a data\nstructure server. The freely available edition is called Redis Open Source,\nand Redis Ltd also sells Redis Cloud and Redis Software under commercial\nlicences.\n\nEvery key holds one value of one type. Strings are plain bytes and double as\ncounters, hashes are records of fields and values, lists keep the order in\nwhich items arrived, sets hold unique members, and sorted sets rank their\nmembers by a score. The list of types runs on to streams, JSON documents,\ngeospatial indexes, time series, vector sets and probabilistic structures such\nas Bloom filters and HyperLogLog.\n\nCommands work on those types directly. `INCR` adds one to a counter and\nreturns the new value, `SADD` puts members into a set and `SPOP` takes one out,\nand any key can carry a timeout after which Redis deletes it by itself. Redis\ncalls such a key volatile. When several steps have to happen as one, a Lua\nscript runs inside the server, and Redis guarantees that nothing else runs\nwhile it does.\n\nMemory is where the data lives, and disk is optional. Redis can write\npoint-in-time snapshots, known as RDB files, or log every write to an\nappend-only file (AOF) that it replays at startup, or do both, or neither.\nReplicas copy a primary and can serve reads. A memory ceiling, `maxmemory`,\nand an eviction policy decide what happens once the data outgrows the space\nit was given.\n\nThe licence changed with Redis 8. From that release on, Redis Open Source is\noffered under a choice of three licences: RSALv2, SSPLv1 and AGPLv3. The first\ntwo are source-available licences, and AGPLv3 is the open source option.\n\n## Why it matters\n\nA web application rarely runs as one process. Behind a load balancer sit\nseveral instances, each with its own memory, and anything kept in that memory\n— a count of login attempts, a session, a cached page — exists only in the\nprocess that happened to handle the request. A limit counted that way can be\nescaped by landing on another instance. Redis gives all the instances one\nshared place for that kind of state, fast enough to consult on every request.\n\nThe atomic commands matter as much as the speed. Two instances that each read\na counter, add one and write it back can both write the same number, and an\nincrement disappears. `INCR` does the read and the write inside the server as\none step, so a hundred concurrent requests leave the counter exactly a hundred\nhigher. A Lua script extends the same guarantee to a short sequence: a check\nand a change that must not be split apart, such as confirming there is still\nroom and taking the place, run as one.\n\nExpiry is the third piece. A rate-limit window, a session or a one-time code\nought to disappear when its time is up, and with a timeout on the key the\ncleanup is the store's job rather than a scheduled task's.\n\nThe price is durability, and it is worth knowing in numbers before choosing.\nWith the append-only file and its default policy of one fsync per second, a\ncrash can cost about a second of writes. With snapshots alone, which usually run\nevery few minutes, the documentation says you \"should be prepared to lose the\nlatest minutes of data\". Replication is asynchronous by default, so a primary\ncan acknowledge a write and stop before any replica has received it. That makes\nRedis a good home for state that can be rebuilt, or lost without harm, and a\npoor home for the only copy of an order, a payment or a balance. The design\nquestion that matters most is what the application does when Redis is\nunreachable, or has forgotten something it was told.\n\n## What it looks like in practice\n\n[22 rate limiters keep their state in Redis](/portfolio/built-fail-closed-rate-limiting-and-anti-abuse/)\non a public directory of companies and professionals, so that a limit holds\nacross every instance instead of living in one process, where it would be\ntrivially escaped. Each limiter is built for the path it protects, because a\nlogin attempt, a search and a bulk export are abusive at very different rates.\nThe deciding choice is what happens when Redis is missing: the limiters fail\nclosed, and traffic is refused rather than waved through. That choice has a\nvisible cost. A Redis problem becomes a problem users can see, accepted on\npurpose, because the alternative is a Redis problem becoming a billing one.\n\nSelling limited stock puts Redis in front of a database as a fast gate:\n[a Redis counter decremented by a Lua script](/notes/selling-limited-stock-without-overselling/)\nadmits about as many buyers as there are units and turns everyone else away\nin microseconds. The script makes the check and the decrement one step. The\ngate promises less than it may seem to. Redis replicates\nasynchronously, so after a failover the new primary may have forgotten some\nadmissions and let a few too many through. A conditional `UPDATE` in the\ndatabase still decides each sale, and a reconciler resets the gate from the\ndatabase's number. When units are few and distinct, such as numbered seats,\ntheir ids can sit in a Redis set, and `SPOP` hands them out one at a time with\nno counter to fight over.\n\nThe most ordinary role is a plain cache. An Azure estate defined in Bicep\n[builds each environment from the same templates](/portfolio/provisioned-azure-infrastructure-as-code/),\ntesting, staging and product alike, and each one has Redis for caching beside\na PostgreSQL Flexible Server, the Container Apps that run the api and the\nwebsite, and Front Door with a WAF policy. Because the cache is declared in\nthe templates with everything else, changing it is a pull request like any\nother change to an environment.\n\n## Where it goes wrong\n\nMost Redis trouble comes from treating a fast, forgetful store as a durable\none, or from assuming it remembers what it was told about time.\n\n**A key that should expire stays forever.** `SET` replaces the value and\ndiscards any timeout the key had, so code that refreshes a cached value with a\nplain `SET` turns a ten-minute entry into a permanent one:\n\n```text\n\u003e SET session:42 \"v1\" EX 600\nOK\n\u003e SET session:42 \"v2\"\nOK\n\u003e TTL session:42\n(integer) -1\n```\n\nPass the timeout on every write that needs one, or add `KEEPTTL`, available\nsince 6.0.0, to keep the old one. The same leak hides in a counter built from\n`INCR` followed by a separate `EXPIRE`: when the second command never arrives,\nthe key never expires. Send both inside one `MULTI` and `EXEC` block, or in\none Lua script.\n\n**Writes start returning errors while reads still work.** The data has reached\n`maxmemory`, and the eviction policy is `noeviction`, or a `volatile-` policy\non a dataset where no key has a timeout, which behaves the same way. Choose\nthe policy for the job: an `allkeys-` policy for a pure cache, where any key\nmay go, and a `volatile-` one only when cached keys share an instance with\nkeys that must stay. For that mix, the documentation's advice is to consider\ntwo separate instances.\n\n**Writes that Redis acknowledged are missing after a restart or a failover.**\nA crash landed inside the fsync window, or a replica was promoted before the\nwrite reached it. That is how Redis is designed, so the fix lies in what is\nkept there. Store anything that must survive in a database with durable\ncommits, put Redis in front of it as a gate or a cache, and reconcile the two\nfrom the database's side, as the stock note does.\n\n**Every client stalls at the same moment.** A long Lua script is running, and\nRedis blocks all other server activity for the whole of a script's runtime.\nKeep scripts short and bounded, and keep loops over large collections out of\nthem.\n\n**A limiter waves traffic through during an outage.** The client code treats\nan error from Redis as \"limit not reached\", so the limiter opens exactly when\nits store is unreachable. Decide the failure mode on purpose and test it by\nstopping Redis: a limiter guarding a costly path should refuse, as the 22\nlimiters above do, with the cost that a Redis problem becomes visible to\nusers.\n\n## Further reading\n\n- [Develop with Redis](https://redis.io/docs/latest/develop/)\n  — the project's starting point: commands to try, client libraries by\n  language, and the data types.\n- [Redis persistence](https://redis.io/docs/latest/operate/oss_and_stack/management/persistence/)\n  — snapshots, the append-only file, fsync policies and what each can lose.\n- [Redis replication](https://redis.io/docs/latest/operate/oss_and_stack/management/replication/)\n  — how replicas follow a primary, and why a window for data loss remains.\n- [Key eviction](https://redis.io/docs/latest/develop/reference/eviction/)\n  — `maxmemory`, every eviction policy, and advice on choosing one.\n- [EXPIRE](https://redis.io/docs/latest/commands/expire/)\n  — key timeouts, and which commands clear them or leave them in place.\n- [Scripting with Lua](https://redis.io/docs/latest/develop/programmability/eval-intro/)\n  — running a script atomically on the server, and what it blocks meanwhile.\n\n\u003chttps://engineer.company/glossary/redis/\u003e\n","date_modified":"2026-10-20T21:59:50Z","description":"Redis holds counters, sets and expiring keys in memory for many servers to share: quick enough to gate or cache, too forgetful to be the record.","formats":{"gemtext":"https://engineer.company/glossary/redis/index.gmi","gopher":"https://engineer.company/glossary/redis/gophermap","html":"https://engineer.company/glossary/redis/","json":"https://engineer.company/glossary/redis/index.json","markdown":"https://engineer.company/glossary/redis/index.md","pdf":"https://engineer.company/glossary/redis/index.pdf"},"kind":"page","language":"en","title":"What is Redis?","url":"https://engineer.company/glossary/redis/","version":1}