Google SWE teaches systems design | EP26: Redis and Memcached Explained (While Drunk?)

Jordan has no life
30 Apr 202208:50

Summary

TLDRIn this candid video, the speaker, despite being inebriated, dives into the technical differences between Redis and Memcached, two popular caching solutions for large-scale distributed systems. Redis, an enhanced version of Memcached, offers additional features like data persistence, transactions, and support for various data types. The video also touches on Memcached's lack of built-in replication and Redis's high availability through its cluster feature. The speaker emphasizes the importance of choosing the right tool based on specific system requirements, highlighting the trade-offs between flexibility and built-in functionality.

Takeaways

  • πŸ•’ The video was recorded at 1:39 a.m. on a Saturday by a speaker who was intoxicated, taking advantage of having the house to themselves.
  • πŸ“Ή The speaker initially recorded a video while drunk but decided to re-record it the next morning for clarity.
  • πŸ”‘ Both Memcached and Redis are used for caching in large-scale distributed systems, primarily because they store data in RAM for faster access.
  • πŸ”„ Memcached allows building a distributed hash map but nodes are unaware of each other, requiring consistent hashing to route requests correctly.
  • 🚫 Memcached lacks built-in failure handling, replication, or availability measures, often requiring custom solutions like Facebook's 'gutter rat' instance strategy.
  • 🌟 Redis offers more features than Memcached, including support for various data types, transactions, range queries, and disk persistence.
  • πŸ’Ύ Redis supports disk persistence through checkpointing or write-ahead logging, offering different trade-offs between speed and data integrity.
  • πŸ”„ Redis Cluster provides high availability and consistency through replication and a gossip protocol for node communication and failover.
  • πŸ”‘ Redis uses a fixed number of partitions (16,384) to ensure even load distribution and avoid hotspots in a distributed setup.
  • πŸ€” Choosing between Redis and Memcached depends on specific needs; Redis offers more out-of-the-box features, but Memcached allows for more customized solutions.
  • πŸ’‘ The importance of understanding the differences between Redis and Memcached is highlighted for making informed decisions in system design and interviews.

Q & A

  • What are the primary use cases for Redis and Memcached?

    -Redis and Memcached are primarily used for caching in large-scale distributed systems. They are beneficial because they store data in RAM, which allows for the creation of a distributed hash map with fast access times.

  • Why is RAM used for caching in Redis and Memcached?

    -RAM is used because it provides faster access times compared to databases on disk. This is crucial for high-performance caching in distributed systems.

  • What is the main difference between Memcached and Redis?

    -Memcached is more limited in features compared to Redis. While both can build a distributed hash map, Redis offers additional features such as support for various data types, transactions, range queries, and disk persistence.

  • What is consistent hashing and how is it used in Memcached?

    -Consistent hashing is a technique used to distribute keys across multiple nodes in a way that minimizes reorganization when nodes are added or removed. In Memcached, it helps in directing requests to the correct node.

  • What is the LRU (Least Recently Used) cache and its role in Memcached?

    -LRU cache is an eviction policy used when a cache instance reaches its capacity. It removes the least recently used items to make room for new data. In Memcached, LRU helps manage memory by evicting old data when necessary.

  • How does Redis differ from Memcached in terms of data types and structures?

    -Redis supports a variety of data types and structures, such as strings, lists, sets, and sorted sets, along with atomic operations. This is in contrast to Memcached, which is primarily a hash map from strings to strings.

  • What is the significance of transactions in Redis?

    -Transactions in Redis allow multiple write operations to be executed serially and as an atomic unit on a single node. This ensures data integrity and consistency.

  • What is disk persistence in Redis and why is it important?

    -Disk persistence in Redis is the ability to save the dataset to disk, which makes Redis more viable as a database. It helps prevent data loss and allows for data recovery in case of a system failure.

  • How does Redis Cluster provide high availability and consistency?

    -Redis Cluster provides high availability through built-in replication and automatic failover. It ensures consistency through a fixed number of partitions and a gossip protocol that prevents split brain scenarios.

  • Why might someone choose Memcached over Redis despite its fewer features?

    -One might choose Memcached over Redis if they require a simpler system or need to implement custom features such as strong consistency, alternate replication patterns, or a coordination service for partition management.

  • What is the importance of caching in large-scale distributed systems as illustrated by Facebook's use case?

    -Caching is crucial in large-scale distributed systems to reduce load on databases and improve performance. Facebook's example shows that caching handles 99% of their read requests, demonstrating its significant impact on system efficiency.

Outlines

00:00

🍻 Drunk Tech Talk: Redis and Memcache Basics

In this humorous and candid video, the speaker, despite being inebriated, attempts to discuss Redis and Memcache, two popular caching solutions used in large-scale distributed systems. The speaker acknowledges their state and promises a more coherent re-recording the next day. They explain that both systems store data in RAM to create a distributed hash map for fast data access. The main difference is that Memcache is a simpler system without built-in replication or availability features, while Redis offers more complex data structures, transactions, and disk persistence options.

05:01

πŸ”‘ Redis vs. Memcache: Features and Use Cases

This paragraph delves deeper into the technical differences between Redis and Memcache. It explains that Memcache operates with a client library directing requests to the appropriate node using consistent hashing and an LRU cache for eviction policies. However, it lacks failure handling. Redis, on the other hand, is described as an enhanced version of Memcache with support for various data types, transactions, range queries, and disk persistence. The paragraph also touches on Redis Cluster for high availability and consistency, using a gossip protocol and fixed partitioning. The speaker concludes by discussing scenarios where one might prefer Memcache over Redis due to its simplicity and flexibility in custom implementations.

Mindmap

Keywords

πŸ’‘Redis

Redis is an open-source, in-memory data structure store, used as a database, cache, and message broker. It supports various data structures such as strings, hashes, lists, sets, and more. In the video, Redis is discussed as a more feature-rich alternative to Memcached, with capabilities like transactions, range queries, and disk persistence, making it suitable for more complex caching and data storage scenarios.

πŸ’‘Memcached

Memcached is a high-performance, distributed memory caching system intended for use in speeding up dynamic web applications by alleviating database load. It is simpler than Redis and primarily used for caching purposes. The script mentions that Memcached is like a subset of Redis, lacking some of the advanced features but being sufficient for certain use cases where a simpler system is preferred.

πŸ’‘Caching

Caching is the process of storing copies of data in a cache, close to the point of use, to reduce the need to repeatedly access the original data from a slower location. In the video, caching is the central theme, with both Redis and Memcached being discussed as solutions for caching in large-scale distributed systems to improve performance by reducing database load.

πŸ’‘Distributed Hash Map

A distributed hash map is a data structure that provides a key-value store and is spread across multiple nodes in a network. In the script, the concept is used to describe how both Redis and Memcached can be used to create a fast-access data store by utilizing RAM, allowing for quick key-value retrieval and storage.

πŸ’‘Consistent Hashing

Consistent hashing is a method of distributing a dataset across multiple nodes in a way that minimizes reorganization when nodes are added or removed. The script explains that consistent hashing is used in Memcached to direct requests to the appropriate node in the cache system.

πŸ’‘LRU Cache

LRU stands for 'Least Recently Used,' a cache eviction policy that discards the least recently used items first. In the context of the video, LRU cache is mentioned as a feature of Memcached, which helps manage memory by evicting the least recently accessed data to make space for new data.

πŸ’‘Replication

Replication in the context of databases and caching systems refers to the process of copying data to multiple locations to ensure redundancy and fault tolerance. The video script contrasts the lack of built-in replication in Memcached with Redis's support for replication, which enhances data availability and durability.

πŸ’‘Failover

Failover is the process of switching to a redundant or standby system in the event of a failure. The script discusses Redis's automatic failover in its replication system, which helps maintain high availability by quickly promoting a replica to take over if the primary node fails.

πŸ’‘Gossip Protocol

A gossip protocol is a method of communication between nodes in a distributed system where each node shares information with a few other nodes, which in turn share it with others. The script explains that Redis Cluster uses a gossip protocol to manage node state and facilitate failover by sharing heartbeats and partition information.

πŸ’‘Partitioning

Partitioning in databases is the process of dividing a large dataset into smaller, more manageable pieces, often to distribute the load across multiple servers. The video describes Redis Cluster's use of fixed number partitioning, where data is evenly distributed across a set number of partitions to prevent hotspots and balance the load.

πŸ’‘Checkpointing

Checkpointing is a process used in data storage to save the state of a system at regular intervals, allowing recovery to the last checkpoint in case of a failure. The script mentions checkpointing as one of the methods Redis uses for disk persistence, trading off between speed and data integrity.

πŸ’‘Write-Ahead Logging

Write-Ahead Logging (WAL) is a technique used in database management systems to ensure that changes are written to a durable storage medium before they are written to the database itself. The video script describes Redis's use of WAL for disk persistence, which ensures data integrity at the cost of slower write operations.

Highlights

Redis and Memcache are both used for caching in large-scale distributed systems, storing data in RAM for fast access.

The speaker is recording while intoxicated, aiming to discuss Redis and Memcache despite the state.

The Balmer curve is mentioned as a way to gauge the speaker's ability to discuss coding topics after drinking.

Memcache allows building a distributed hash map with nodes unaware of each other, using consistent hashing.

Memcache uses LRU cache for eviction when necessary, lacking built-in failure handling or replication.

Facebook's approach to Memcache instance failure involves adding new instances without data replication.

Redis is a modification of Memcache with additional features, supporting various data types and atomic operations.

Redis supports transactions and range queries on a single partition, enhancing its capabilities over Memcache.

Redis offers disk persistence through checkpointing or write-ahead logging, increasing its viability as a database.

Redis Cluster provides high availability and consistency with replication and automatic failover.

Redis uses a gossip protocol for node communication and heartbeat sharing, crucial for failover processes.

Redis prevents split brain using a quorum of master nodes and epoch numbers during failover.

Redis employs fixed partitioning with 16,384 partitions, unlike other databases using consistent hashing.

Redis's built-in features can make it harder to diverge from its design pattern compared to Memcache.

The choice between Redis and Memcache depends on specific needs like strong consistency or replication patterns.

Caching is crucial for large-scale distributed systems, with Facebook handling 99% of read requests through cache.

The video concludes by emphasizing the importance of understanding the differences between Redis and Memcache.

Transcripts

play00:01

hello everyone it is uh i think 1 39 a.m

play00:05

on a saturday morning if you can't tell

play00:07

i am actively hammered

play00:09

why am i doing this right now it's

play00:11

because my roommate is at his

play00:12

girlfriend's house and uh i have the

play00:15

free house to record so you know what

play00:17

screw it let's get this done um

play00:20

i'm gonna talk about redis and memcache

play00:22

today and assuming my brain can support

play00:24

it because i guess the balmer curve

play00:26

tells me that i can have a couple drinks

play00:29

and and you know talk about code then uh

play00:32

hopefully this video will be coherent

play00:33

that being said i'm also about eight or

play00:36

nine drinks in so i'm a little bit past

play00:37

the balmer curve but

play00:39

you know what i can still talk about

play00:41

computer science so let's get it done

play00:44

hey yeah so this is me from the next

play00:46

morning uh

play00:48

i actually did record that video and uh

play00:50

i looked back on it and i was like damn

play00:52

i am slurring a lot of words right now

play00:54

so

play00:55

um we're gonna do a re-recording and uh

play00:58

hopefully i'll explain a little bit more

play00:59

concisely this time so let's do memcache

play01:02

and redis

play01:04

so basically in terms of what memcache

play01:06

and redis are they're both solutions

play01:08

that are typically used for caching in

play01:09

large-scale distributed systems the

play01:11

reason for this is that they generally

play01:13

speaking store their data in ram random

play01:15

access memory and that allows you to

play01:17

basically build something called a

play01:19

distributed hash map which can access

play01:21

keys and set keys at of one time which

play01:24

is great because obviously databases on

play01:26

disk can't do that

play01:29

as we can see though there are pretty

play01:30

significant differences between the two

play01:32

services themselves

play01:33

mainly in the sense that

play01:35

memcache is basically like a subset of

play01:37

redis but you know sometimes it's good

play01:39

to have a more limited feature set

play01:40

because you can build out more

play01:42

so

play01:43

what is memcache like i said memcache

play01:45

allows you to build a distributed hash

play01:47

map amongst a bunch of nodes however the

play01:49

nodes basically don't know about one

play01:51

another so generally speaking you're

play01:53

actually kind of just using this client

play01:55

library to go ahead and wire all those

play01:57

requests to the proper node how do we

play01:59

wire each request to the right node well

play02:01

generally speaking we're using

play02:02

consistent hashing so basically you give

play02:05

all of your you know application servers

play02:06

that are going to be using memcache

play02:08

instances a list of all the nodes that

play02:11

are running memcached and then that will

play02:13

allow them to create a consistent

play02:14

hashing ring

play02:16

additionally you have an lru cache so if

play02:20

each memcache instance gets too big or

play02:22

basically there's too much data in there

play02:24

and you need to evict something in order

play02:25

to make room for a new element you're

play02:27

using the lru algorithm and then you

play02:29

know a couple of other features that are

play02:31

built in are

play02:32

you know basically just like compare and

play02:34

set um generally speaking there's not

play02:37

really any failure handling for memcache

play02:39

they don't have any built-in like

play02:40

replication or availability measures so

play02:43

um what actually a company like facebook

play02:46

did and i'll link this lecture

play02:48

from like mit basically in the video

play02:49

description is that they use something

play02:51

called like a gutter rattus instance

play02:53

where every time um a red is not a redis

play02:56

instance um a memcache instance failed

play02:59

they basically go ahead and throw a new

play03:01

memcache instance in there to just take

play03:03

its place and then eventually it'll get

play03:05

repopulated over time but there's no

play03:06

like you know copying over of the data

play03:09

from one instance to another which is

play03:11

interesting

play03:13

okay so what is redis well at least on a

play03:15

single node

play03:16

redis is a modification of memcache that

play03:19

basically has the following features so

play03:22

obviously it's an lru distributed hash

play03:24

map but it's got some other stuff too so

play03:26

instead of just being a hash map from

play03:28

strings to strings you can actually have

play03:30

other data types as values in the hash

play03:32

maps so those can be sets

play03:34

you know strings but with atomic

play03:36

operations like appending to the string

play03:39

maps and lists and you can also even

play03:41

have transactions so if you want to make

play03:43

multiple writes to a single node and

play03:45

make sure that those are

play03:46

executed both serially and also as an

play03:49

atomic unit you can do that you can also

play03:52

even make range queries on a single

play03:54

partition and there's also a way to kind

play03:56

of

play03:57

go ahead and hash keys using only part

play04:00

of the key

play04:01

doing something called the hashtag which

play04:03

allows you to kind of have some control

play04:06

over where each key is going to be sent

play04:08

to in a partitioning scheme

play04:11

then finally there's also a concept of

play04:12

actual disk persistence which makes

play04:14

redis a little bit more viable as an

play04:16

actual database for your application as

play04:18

well and you can do disk persistence via

play04:20

checkpointing which is probably faster

play04:22

but obviously comes with the cost of

play04:24

losing some rights if you don't

play04:26

checkpoint everything or you can use a

play04:28

write ahead log

play04:30

where basically every single write is

play04:31

going to the disk before it's written in

play04:33

memory and this obviously comes at the

play04:35

cost of slower writes

play04:38

in terms of redis cluster redis cluster

play04:41

is basically what redis calls

play04:43

you know running a bunch of radis nodes

play04:44

in a distributed manner so basically the

play04:47

point here is that this provides both

play04:49

high availability and consistency so how

play04:51

do they provide high ability well unlike

play04:53

memcache they actually support

play04:55

replication out of the box so that uses

play04:57

single leader replication with an

play04:59

automatic failover

play05:00

the thing with single leader replication

play05:02

here is that some rights can be lost so

play05:04

if the leader has some rights and it's

play05:06

replicating them asynchronously just

play05:08

some of its replicas and then the leader

play05:10

goes ahead and fails before all of those

play05:12

rights get properly sent out to all of

play05:14

the followers then those rights are

play05:16

probably just going to be lost um so how

play05:18

do we actually do a failover well

play05:20

there's a gossip protocol between all

play05:22

the nodes where you're basically sharing

play05:24

heartbeats that convey the you know kind

play05:26

of the state of the node as well as the

play05:28

partitions that it's holding and then uh

play05:30

in order now to basically you know put a

play05:34

new replica to the master what needs to

play05:36

happen is that a quorum of masternodes

play05:38

amongst all partitions need to basically

play05:41

go ahead and agree that this new

play05:43

follower is going to become the leader

play05:45

and they use an epoch number to do that

play05:47

and so this basically allows us to

play05:49

prevent split brain because obviously a

play05:51

quorum of nodes can't make a

play05:53

conflicting decision for a single epoch

play05:55

so that's pretty smart there how they

play05:56

make sure to prevent split brain and

play05:58

then finally

play06:00

in order to kind of do partitioning

play06:03

unlike a bunch of other database

play06:04

solutions which most of which we've kind

play06:07

of just seen using

play06:08

consistent hashing so far this actually

play06:10

uses the fixed number of partition

play06:12

solution that i discussed from ddia

play06:15

where there are exactly 16 384 fixed

play06:18

partitions with fixed ranges and then

play06:20

your job is basically to make sure that

play06:23

you know you're putting the right number

play06:24

of partitions on each node such that

play06:27

there aren't too many hotspots and

play06:29

obviously you know due to the fact that

play06:31

there's replication if there are certain

play06:32

hotkeys hopefully the load won't be too

play06:35

great on them because you'll actually be

play06:37

able to serve a lot of requests from

play06:38

those replicas as opposed to just having

play06:40

to serve them all from one instance

play06:43

okay so in terms of a comparison between

play06:46

memcached and redis

play06:48

as you can see um redis is basically

play06:50

just memcached with a bunch of

play06:52

additional features built in out of the

play06:53

box so why would you ever not want to

play06:55

use it well

play06:57

by virtue of having all these features

play06:59

built in it makes it harder to kind of

play07:01

diverge from that design pattern so

play07:03

let's say you needed strongly consistent

play07:04

data maybe you'd be better off just

play07:06

using memcache and then kind of building

play07:08

out your own system using something with

play07:10

like a coordination service in order to

play07:12

ensure strong consistency

play07:14

maybe you want alternate replication

play07:16

patterns like a leaderless replication

play07:18

schema which you know kind of resembles

play07:20

a dynamo database then perhaps you'd be

play07:23

better off using memcache and then you

play07:25

know kind of implementing that yourself

play07:27

or maybe even you want to use just a

play07:28

coordination service in general for all

play07:31

of that partition management as opposed

play07:33

to just using a gossip protocol

play07:35

because gossip protocol even though it

play07:37

does generally work you know it's just a

play07:39

little bit harder to reason about

play07:40

sometimes

play07:42

you know you can do that too

play07:45

so in conclusion both redis and memcache

play07:47

are super useful systems for

play07:49

implementing caching and in an interview

play07:52

i imagine it probably won't really come

play07:54

up if you just said yeah i'm going to

play07:56

use redis or memcache in order to

play07:57

implement my cache but i think it's

play07:59

important to know the subtle differences

play08:00

between the two of them because that's

play08:02

kind of what this channel is all about

play08:04

is being able to you know hear all of

play08:06

the names of these different technology

play08:07

services and basically go and say okay

play08:09

well actually now i understand why one

play08:11

is different from the other

play08:13

obviously caching is hugely beneficial

play08:15

for any large scale distributed system

play08:17

and as a result of that you should

play08:19

basically be using it whenever you can

play08:22

until it pretty much prices you out so

play08:24

you know as long as you can afford it

play08:26

you should be using caching

play08:28

that video i mentioned about caching at

play08:30

facebook basically says that pretty much

play08:32

99 of their read requests are handled by

play08:35

their cash in order to take load off

play08:37

their databases and allow them to keep

play08:39

operating so okay i hope this video was

play08:42

useful sorry i couldn't post the drunk

play08:44

one but it was probably double as long

play08:46

and very incoherent but uh have a good

play08:48

one guys

Rate This
β˜…
β˜…
β˜…
β˜…
β˜…

5.0 / 5 (0 votes)

Related Tags
MemcacheRedisCachingDistributed SystemsDatabaseHigh AvailabilityReplicationComputer ScienceTech TalkPerformance Optimization