Redis Cache - kdwivedi1985/system-design GitHub Wiki

How does Redis works?

Redis is open-source in-memory key-value data store. It provide fast access to frequently read/cached data.

  • Stores data as String, list, set, hashes, bitmap etc.
  • Redis clusters are built based on primary and secondary (slave/replicas) based architecture for high availability. Data can be partitioned across multiple nodes for scalability and performance. It means you can have many masters and many slaves with a subset of data.
  • According to CAP-Theorem it is considered consistent and partition-tolerant, because availability is eventual if any node looses network connection, will become unavailable before new master is selected from replicas.
  • It is considered as consistent because by default read and write both happens through Master.
  • Replicas are for high-availability and failover. Data synchronization between primary and secondary nodes happen asynchronously.
  • Redis supports single-threaded atomic transactions. Meaning it executes requests one by one sequentialy.
  • No race condition will occur if we use atomic commands.
  • Reds can be manually configured from secondary nodes/replicas. Dirty reads can happen if read requests are served by replicas.(Not Default)

If you have 8 nodes:

Configuration Masters Shards/Slaves Replicas per Master Notes
8 masters 8 8 0 Max capacity, no redundancy
4 masters + 4 replicas 4 4 1 High availability (recommended)
3 masters + 5 replicas 3 3 Variable More redundancy, less capacity

How does Redis partitions the data?

  • Redis has 16384 slots. if you have 4 shards than the slot ranges will be assigned to each one of those. SLOT = CRC16(key) % 16384 Each master will manage 16,384 / 4 = 4,096 slots Shard 1 β†’ slots 0–4095 Shard 2 β†’ slots 4096–8191 Shard 3 β†’ slots 8192–12287 Shard 4 β†’ slots 12288–16383

for user: 123 slot = CRC16("user:123") % 16384 This gives a number between 0 and 16383 β€” the hash slot. and data will be stored in respective shard.

  • Redis Cluster is setup manually

AD_4nXdTLQNxu-lOzFlMfgPpLHLyLbZg6-1AQKuif6KDFAQcQxjVwrzJuiuWdM9GaP8jlBPogtpXdfj3_bCtFUnYL8L4b-aJ6ho6Z5wA1blIWfRL6Yga9W7YC810


How does Redis holds meta-data about nodes and find the respective node?

  • In the redis cluster each shard holds the meta-data about itself and other nodes. When the client makes the first call, that goes to any random node, and it responds with all the details about hash-slots and nodes. Then client makes a call to the respective shard for retrieval/write.

Does Redis supports custom partitoning e.g. range-based, geo-based? Custom partitioning at single Redis cluster is not supported. You need to build separate clusters and handle the partitioning logic at application layer. e.g. If you have a cluster of 8 nodes. It will use internal hash-slot mechanism for data distribution. This can't be chnaged.


How are atomic transactions handled in Redis?

  • Redis command like SET, GET, INCR, DECR, LPOP, etc. are fully atomic. There are pop, increase, decrese etc.

  • Redis support batch processing and (LUA)light weight scripting as well. Lua is better as it is scripting which handles conditions etc. Good for use cases like inventory updates

  • Atomic stock update, With atomic commands: DECR stands for "Decrement" - decrease the value of a key by 1 (or more using DECRBY). SET stock:product123 10 DECR stock:product123

  • Lua is a lightweight scripting language supported natively in Redis local stock = tonumber(redis.call("GET", KEYS[1])) if stock and stock > 0 then return redis.call("DECR", KEYS[1]) else return -1 -- Or some error code end

  • Lua is better if need conditional logic, and treats whole script as atomic, read+write [Inventory check + update logic]

  • Lua script can be written as String in code and pass to Redis. e.g. Object result = jedis.eval(luaScript, Collections.singletonList(key), Collections.emptyList());


Redis Distributed lock?

  • Redis provides Readlock (Distributed locking algoritm) for distributed nodes, redundancy or multi-DC clusters for failover.
  • If one node holding the lock goes down, lock will be lost. Redlock handles that by acquiring the lock by different unique values at multiple nodes. it uses quorum consensus to manage locks safely.

What is geo spatial feature in Redis?

  • Redis’s geospatial feature allows you to store, index, and query geographic locations (longitude/latitude points) efficiently. This is especially useful for applications like location-based services, ride-sharing, delivery tracking, or finding nearby places (e.g., restaurants, stores). It is supported from 3.2+.

  • Geospatial commands: GEOADD, GEODIST, GEOSEARCH --Add locations-- GEOADD locations 12.361329 31.232256 "chicago" 16.067269 41.902671 "nashville"

    --Find all locations within 5 miles of chicago-- GEOSEARCH locations FROM Chicago 14.361219 36.115246 BYRADIUS 5 mi

Use redis for :

  • caching the frequently accessed items
  • distributed locking
  • geospatial/ location based query
  • inventory updates scenarios etc.

---MemCache---