How Key value Stores Work (Redis, DynamoDB, Memcached)?
Skills:
Systems Design Basics90%
Key Takeaways
Key-value stores such as Redis, DynamoDB, and Memcached are explained in the context of systems design.
Full Transcript
Today we're looking at key value stores, a type of database that keeps track of everything from your shopping cart to your chat messages. And what starts as a simple idea quickly becomes a hot problem in software engineering. So what is a key value store? Think of it as a giant dictionary. You have a key, maybe user 1 2 3 4 5 cart and a value. All the items in that user's shopping cart. You can put data in, you can get data out. Here's the thing, though. Amazon stores everything. We're talking about terabytes of data per major region with billions of key value pairs that need to be accessed millions of times per second. No single computer can handle that load. So, you need to spread it across thousands of servers. But now you have a problem. When someone asks for user 1 2 3 4 5 cart, how do you know which server has it? Your first instinct might be just hash the key and use modulo. Hash the key, divide by the number of servers, use the remainders to pick a server. Simple math. But here's where it gets tricky. What happens when you need to add a new server? Suddenly, you're dividing by a different number and almost every key maps to a different server. You would have to move nearly all your data just to add one machine. This is where consistent hashing comes in, and it's pretty clever. Instead of mapping keys directly to servers, imagine both keys and servers living on a giant circle. Think of a clock face, but instead of 12 hours, you have millions of positions. You place your servers at random spots around this circle. Maybe server A is at position 100. Server B is at position 1,000. Server C is at position 5,000. Now, when you want to store user 1 2 3 4 5 cart, you hash that key to get a position on that circle. Let's say position 750. You site at that spot and walk clockwise until you hit the first server. In this case, that would be server B at position a th00and. The magic happens when you add a new server. Say you place server D at position 500. Now keys that hash to position 101 through 500 go to server D instead of server B. But everything else stay exactly where it was. You only move a fraction of your data. But wait, what happens when server B crashes? Now all those keys has nowhere to go. This is where you need copies. Instead of storing each piece of data on just one server, you store it on multiple servers. One way to do this is to keep using that circle. When user 1 2 3 4 5 card hashes to position 750, you don't just store it on server B. You also store copies on the next two servers clockwise. Maybe server C and server A. Now, if server B goes down, you still have the data. Great. Now, your data is safe, but you've created a much bigger headache. Let's say two people are shopping for the same family account. They both add items to the cart at the exact same time, but their requests hit different servers. Now, you have two different versions of the same shopping cart. Which one is correct? This brings us to one of the fundamental concepts of distributed systems. You cannot have perfect consistency, perfect availability, and perfect network reliability all at the same time. You have to pick two. If you choose consistency, making sure everyone always sees the latest data. You might have to refuse requests when server can't communicate. Banks do this because showing the wrong account balance is unacceptable. If you choose availability, keeping the system running no matter what, you might occasionally serve stale data. Most web apps do this because it's better to show an old shopping cart than no shopping cart at all. The solution most big systems use is called eventual consistency. The idea is simple. Given enough time, all the copies will match up. But right now, this instance, they might be a little different. This creates a new challenge. How do you handle conflicting versions? There are different approaches to this problem. One clever solution is called vector clocks. Think of it like a version number but smarter. Every time a piece of data gets modified, it gets tagged with information about which server did the modification and when. When you detected two conflicting versions, you have a few options. Sometimes you can automatically merge them like combining two shopping carts to include all the items. Sometimes you have to ask the users to choose. Sometimes you just pick the most recent one and hope for the best. Next problem. In a system with thousands of servers, machines are failing constantly. How do you even know when a server is down? The naive approach is to have every server ping every other server. But with thousand servers, that's nearly a million connections. It doesn't scale. One solution that works well is called a gossip protocol. Each server keeps a list of all the other servers and occasionally shares that list with a few random neighbors. If server X stops responding, the gossip spreads throughout the entire cluster. It's exactly like how rumor spreads in high school. Surprisingly effective, and you don't need everyone talking to everyone. What started as remember this shopping cart turns into a master class in distributed systems engineering. And this is just the beginning. We haven't even touched storage engines, data center failures, or performance optimization across thousands of machines. The next time you add something to your cart and it just works, you'll know there's an intricate dance happening across data center around the world. All to make sure your data is exactly where you expect it to be. Ready to ace your next technical interview? Join our community where we offer comprehensive courses on system design, coding, behavioral questions, machine learning, and object-oriented design. Learn more at byitebico.com.
Original Description
We just launched the all-in-one tech interview prep platform, covering coding, system design, OOD, and machine learning.
Launch sale: 50% off. Check it out: https://bit.ly/bytebyego-yt-desc
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from ByteByteGo · ByteByteGo · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
What happens when you type a URL into your browser?
ByteByteGo
System Design: Why is Kafka fast?
ByteByteGo
System Design: How to store passwords in the database?
ByteByteGo
Big Misconceptions about Bare Metal, Virtual Machines, and Containers
ByteByteGo
FAANG System Design Interview: Design A Location Based Service (Yelp, Google Places)
ByteByteGo
Scan To Pay in 2 Minutes
ByteByteGo
Consistent Hashing | Algorithms You Should Know #1
ByteByteGo
System Design: Why is single-threaded Redis so fast?
ByteByteGo
HTTP/1 to HTTP/2 to HTTP/3
ByteByteGo
What Is REST API? Examples And How To Use It: Crash Course System Design #3
ByteByteGo
The Secret Sauce Behind NoSQL: LSM Tree
ByteByteGo
Bloom Filters | Algorithms You Should Know #2 | Real-world Examples
ByteByteGo
Back-Of-The-Envelope Estimation / Capacity Planning
ByteByteGo
How To Choose The Right Database?
ByteByteGo
How Does Live Streaming Platform Work? (YouTube live, Twitch, TikTok Live)
ByteByteGo
Latency Numbers Programmer Should Know: Crash Course System Design #1
ByteByteGo
What Are Microservices Really All About? (And When Not To Use It)
ByteByteGo
How Does Apple/Google Pay Work?
ByteByteGo
Proxy vs Reverse Proxy (Real-world Examples)
ByteByteGo
What is API Gateway?
ByteByteGo
What Is GraphQL? REST vs. GraphQL
ByteByteGo
What Is Single Sign-on (SSO)? How It Works
ByteByteGo
What Is A CDN? How Does It Work?
ByteByteGo
What is RPC? gRPC Introduction.
ByteByteGo
SSL, TLS, HTTPS Explained
ByteByteGo
FANG Interview Question | Process vs Thread
ByteByteGo
What is OSI Model | Real World Examples
ByteByteGo
CAP Theorem Simplified
ByteByteGo
Kubernetes Explained in 6 Minutes | k8s Architecture
ByteByteGo
CI/CD In 5 Minutes | Is It Worth The Hassle: Crash Course System Design #2
ByteByteGo
Why Is System Design Interview Important?
ByteByteGo
8 Key Data Structures That Power Modern Databases
ByteByteGo
System Design Interview: A Step-By-Step Guide
ByteByteGo
Top 5 Redis Use Cases
ByteByteGo
Debugging Like A Pro
ByteByteGo
But What Is Cloud Native Really All About?
ByteByteGo
Everything You Need to Know About DNS: Crash Course System Design #4
ByteByteGo
The Most Beloved Burger for Developers
ByteByteGo
10+ Key Memory & Storage Systems: Crash Course System Design #5
ByteByteGo
Cache Systems Every Developer Should Know
ByteByteGo
Top 7 ChatGPT Developer Hacks
ByteByteGo
How ChatGPT Works Technically | ChatGPT Architecture
ByteByteGo
10 Key Data Structures We Use Every Day
ByteByteGo
Top 7 Most-Used Distributed System Patterns
ByteByteGo
Secret To Optimizing SQL Queries - Understand The SQL Execution Order
ByteByteGo
Amazon Prime Video Ditches AWS Serverless, Saves 90%
ByteByteGo
Top 6 Most Popular API Architecture Styles
ByteByteGo
Top 5 Most-Used Deployment Strategies
ByteByteGo
How Discord Stores TRILLIONS of Messages
ByteByteGo
Uncovering Stack Overflow's Shocking Architecture
ByteByteGo
OAuth 2 Explained In Simple Terms
ByteByteGo
Demystifying the Unusual Evolution of the Netflix API Architecture
ByteByteGo
1 Year Of YouTube | Best System Design Series
ByteByteGo
DevOps vs SRE vs Platform Engineering | Clear Big Misconceptions
ByteByteGo
Top 7 Ways to 10x Your API Performance
ByteByteGo
Why Google and Meta Put Billion Lines of Code In 1 Repository?
ByteByteGo
Git MERGE vs REBASE: Everything You Need to Know
ByteByteGo
Top 6 Load Balancing Algorithms Every Developer Should Know
ByteByteGo
Algorithms You Should Know Before System Design Interviews
ByteByteGo
Top 5 Most Used Architecture Patterns
ByteByteGo
More on: Systems Design Basics
View skill →Related Reads
📰
📰
📰
📰
MCP Series (08): Enterprise Governance — Registry, Routing, and Observability
Dev.to · WonderLab
Where is the hosts file on Mac, Windows, and Linux?
Dev.to · Locahl
Presentation: The Rust High Performance Talk You Did Not Expect
InfoQ AI/ML
I Use These Two Books for System Design & DSA — That’s It, No Clutter
Medium · Programming
🎓
Tutor Explanation
DeepCamp AI