Library / Case Studies

Moving persistent data out of Redis at Github

Historically, we have used Redis in two ways at GitHub: We used it as an LRU cache to conveniently store the results of expensive computations over data originally persisted in…

By Github engineers · Github · Added 2026-10-12

Open original source

All content belongs to Github engineers / Github. We only link to it and give credit. Report a problem

Learn this case study

About 37 min left. Your work is saved as you go.

What you are about to study

Github explains a real system. You should know the basics: caching, queues and replication.

Ideas you will meet

Caching
Cache-aside, write-through, eviction and invalidation strategies.
Redis
In-memory data structures for caching, queues and leaderboards.
Distributed Systems
Consensus, time, failure and the fundamentals of scale-out.

What we already know about this

Our own lessons on these ideas. Read them here before you start, so it makes sense.

  • Core Concepts: How Things Actually Work · 8 minWhy Redis is fast, and Redis versus MemcachedIn-memory storage, a single-threaded event loop and rich data structures, plus how persistence and replication work.Read here

    Why it is fast

    1. Everything lives in memory. No disk seek on the read path.
    2. Simple, efficient data structures (hash tables, skip lists, compact encodings) with predictable costs.
    3. A single-threaded event loop for command execution: no locks between threads, no context switching, and each command runs atomically. I/O multiplexing lets one thread serve thousands of connections.
    4. A lean protocol and in-process memory management.

    (Newer versions use extra threads for network I/O, but command execution stays ordered.)

    Memory modules in the slots of a computer motherboard: in-memory data is read in nanosecondsImage: Mixabest · Public domain · Source · Wikimedia Commons: Dual-channel DDR memory use 6026

    How the pieces fit

    Figure SDA-CORE-09-1 · Redis: event loop, memory, persistence and replication · © forcsguy.com

    More than a cache

    Redis stores typed values:

    TypeUse
    Stringcached values, counters, flags
    Hashobjects with fields (a user profile)
    Listqueues, recent items
    Setunique members, tags, set maths
    Sorted setleaderboards, priority queues, time-ordered data
    Othersstreams, bitmaps, HyperLogLog, geospatial

    Because operations run on the server, you can increment, push, pop or intersect without a read-modify-write race.

    Figure SDA-CORE-09-2 · Redis data types and what they are for · © forcsguy.com

    Durability options

    Memory is volatile, so Redis offers:

    • Snapshots (RDB): periodic point-in-time dumps. Compact, but recent writes can be lost.
    • Append-only file (AOF): logs every write; replays on restart. Safer, bigger and a little slower.
    • Or none at all when it is purely a cache.

    Scaling and availability

    • Replication: a primary copies to replicas (asynchronously), serving reads and enabling failover.
    • Sentinel / managed failover promotes a replica when the primary dies.
    • Cluster: splits keys across nodes by hash slot, for data larger than one machine.

    Redis versus Memcached

    RedisMemcached
    Data typesmanystrings only
    Persistenceoptionalnone
    Replicationyesnot built in
    Threadingone for commandsmulti-threaded
    Best forcache plus data structures, queues, countersa very simple, large, pure cache

    For most new projects Redis is the more flexible choice. Memcached remains fine for a straightforward cache that must use many CPU cores.

    Pitfalls

    • Memory is finite: set a maxmemory and an eviction policy (for example LRU).
    • Avoid slow commands on huge collections.
    • A very hot key still concentrates load on one node.

    More on this site

    Open this lesson with its quiz and exercise

  • Search, Analytics and Geo · 10 minNearby search with Redis GEOHow Redis stores locations in a sorted set using geohash scores, how GEOSEARCH finds nearby members, how to expire stale entries, and when to move to a spatial database.Read here

    The problem

    "Show the nearest available drivers", "stores within 3 km", "friends nearby". The data is a large set of points that change often, and the question is always about a small neighbourhood. You want a fast, in-memory structure.

    How Redis does it

    Redis stores locations in an ordinary sorted set. Each member (a driver id) has a score that is a 52-bit geohash of its longitude and latitude. Geohash interleaves the bits of the two coordinates so that nearby places have nearby scores. A search turns a radius into a few score ranges, reads those ranges, then filters by exact distance.

    Figure SDA-SAG-09-1 · A nearby query in Redis · © forcsguy.com

    Typical commands:

    GEOADD drivers:blr 77.5946 12.9716 driver:42
    GEOSEARCH drivers:blr FROMLONLAT 77.60 12.97 BYRADIUS 2 km ASC COUNT 20 WITHDIST
    

    Design details

    • Partition the key. One sorted set for the whole planet is a hot spot and slow to search. Use a key per city, region or grid cell so each set is small and local, and shard keys across nodes.
    • Updates are cheap. Moving a member is another GEOADD, about O(log n). 75,000 updates per second is manageable on a few nodes.
    • Freshness. Sorted set members have no individual expiry. Keep a second sorted set lastseen with the update time as score, and run a job that removes members older than, say, 30 seconds from both sets.
    • Availability filter. Store only drivers who are available, or look up status after the search; keep the hot set small.
    • Accuracy. Redis uses a spherical model of the Earth, so distances can be off by a fraction of a percent, and results near a cell boundary rely on the exact filter. That is fine for "within 2 km", not for surveying.
    • Durability. Treat Redis as a rebuildable copy. The system of record is a database, and clients re-send their location every few seconds anyway.

    Figure SDA-SAG-09-2 · Keeping the hot set fresh · © forcsguy.com

    When Redis GEO is not enough

    Move to a spatial database such as PostGIS, or a purpose-built index, when you need polygons and fences, joins with other tables, constraints (opening hours, vehicle type) together with distance, or durable queries over history. Many systems combine them: a database for rich queries and Redis for the live, hot position data.

    Other grid systems are covered in the geofencing lesson and in the real-world reading: H3 (hexagons, good for aggregation and neighbours) and S2 (cells on a sphere).

    Real-world reading

    Open this lesson with its quiz and exercise

  • Advanced Distributed Systems · 12 minRedis: persistence, replication, Sentinel and ClusterWhat keeps an in-memory database from losing data and from being a single point of failure: snapshots, the append-only file, replicas, Sentinel and hash slots.Read here

    Fast because it is in memory, durable because of logs

    Redis keeps its dataset in RAM (the reason it is so fast; see the Redis and Memcached lesson). To survive a restart it offers two persistence mechanisms, which can be combined.

    Snapshots (RDB)

    Redis periodically writes a point-in-time snapshot of the whole dataset to disk, using a forked child process so the main process keeps serving. Snapshots are compact and quick to load, but anything written since the last snapshot is lost on a crash.

    Append-only file (AOF)

    Redis appends every write command to a log. On restart it replays the log. The fsync policy sets the loss window:

    PolicyBehaviourRisk
    alwaysFlush after every commandSlowest, almost no loss
    everysecFlush once per secondAt most about a second lost (common default)
    noLet the OS decideFastest, larger loss

    The AOF grows, so Redis periodically rewrites it into a compact form. Many deployments use AOF with everysec, sometimes plus snapshots for fast backups.

    Figure SDA-ADS-04-1 · Redis persistence choices · © forcsguy.com

    Replication and Sentinel

    A Redis primary replicates to one or more replicas asynchronously. Replicas serve reads (possibly slightly stale) and can take over. Sentinel is a small set of monitor processes that:

    1. watch the primary and replicas,
    2. agree by quorum that the primary is down,
    3. promote a replica and tell clients where the new primary is.

    Figure SDA-ADS-04-2 · Sentinel failover · © forcsguy.com

    Because replication is asynchronous, a failover can lose the last few acknowledged writes. Redis offers settings to reduce this (for example requiring a minimum number of replicas to acknowledge), at the cost of availability.

    Redis Cluster: sharding

    When data or traffic outgrow one node, Redis Cluster shards the keyspace into 16,384 hash slots. A key's slot is a hash of the key modulo that number; each node owns a range of slots, and each primary has replicas. Clients learn the slot map and talk to the right node; a wrong guess gets a redirect.

    • Resharding moves slots (and their keys) between nodes while the cluster runs.
    • Multi-key operations only work when all keys are in the same slot. Hash tags (a {tag} part of the key) force related keys into one slot.
    • Failover is built in: replicas of a failed primary are promoted by the cluster.
    • This is the "fixed number of slots" cousin of consistent hashing (see the consistent-hashing lesson).

    Which setup when

    NeedChoice
    Pure cache, loss acceptableSingle node or replicas, little or no persistence
    Must survive restartAOF (everysec) and snapshots
    Must survive node lossReplica plus Sentinel
    Data larger than one node, or very high throughputRedis Cluster

    Also keep memory in mind: set a max memory and an eviction policy (see the caching lessons) so Redis behaves predictably when full, and watch for large keys and slow commands, which block a single-threaded executor.

    Real-world reading

    Open this lesson with its quiz and exercise

Go deeper on this

A path through videos, articles, other case studies and code on the same ideas as Moving persistent data out of Redis at Github.

  1. Warm upGet the vocabulary first.

  2. WatchHow others solve the same problem, on video.

    Videos belong to their creators and play from YouTube. More on YouTube (opens in a new tab)

  3. ReadEngineers explain it in their own words.

  4. See it in another companyThe same ideas, a different system.

  5. Read the codeSee how it looks when it is real.

Keep learning

Loading community…