How Key value Stores Work (Redis, DynamoDB, Memcached)?

ByteByteGo · Beginner ·🏗️ Systems Design & Architecture ·11mo ago

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 What happens when you type a URL into your browser?
What happens when you type a URL into your browser?
ByteByteGo
2 System Design: Why is Kafka fast?
System Design: Why is Kafka fast?
ByteByteGo
3 System Design: How to store passwords in the database?
System Design: How to store passwords in the database?
ByteByteGo
4 Big Misconceptions about Bare Metal, Virtual Machines, and Containers
Big Misconceptions about Bare Metal, Virtual Machines, and Containers
ByteByteGo
5 FAANG System Design Interview: Design A Location Based Service (Yelp, Google Places)
FAANG System Design Interview: Design A Location Based Service (Yelp, Google Places)
ByteByteGo
6 Scan To Pay in 2 Minutes
Scan To Pay in 2 Minutes
ByteByteGo
7 Consistent Hashing | Algorithms You Should Know #1
Consistent Hashing | Algorithms You Should Know #1
ByteByteGo
8 System Design: Why is single-threaded Redis so fast?
System Design: Why is single-threaded Redis so fast?
ByteByteGo
9 HTTP/1 to HTTP/2 to HTTP/3
HTTP/1 to HTTP/2 to HTTP/3
ByteByteGo
10 What Is REST API? Examples And How To Use It: Crash Course System Design #3
What Is REST API? Examples And How To Use It: Crash Course System Design #3
ByteByteGo
11 The Secret Sauce Behind NoSQL: LSM Tree
The Secret Sauce Behind NoSQL: LSM Tree
ByteByteGo
12 Bloom Filters | Algorithms You Should Know #2 | Real-world Examples
Bloom Filters | Algorithms You Should Know #2 | Real-world Examples
ByteByteGo
13 Back-Of-The-Envelope Estimation / Capacity Planning
Back-Of-The-Envelope Estimation / Capacity Planning
ByteByteGo
14 How To Choose The Right Database?
How To Choose The Right Database?
ByteByteGo
15 How Does Live Streaming Platform Work? (YouTube live, Twitch, TikTok Live)
How Does Live Streaming Platform Work? (YouTube live, Twitch, TikTok Live)
ByteByteGo
16 Latency Numbers Programmer Should Know: Crash Course System Design #1
Latency Numbers Programmer Should Know: Crash Course System Design #1
ByteByteGo
17 What Are Microservices Really All About? (And When Not To Use It)
What Are Microservices Really All About? (And When Not To Use It)
ByteByteGo
18 How Does Apple/Google Pay Work?
How Does Apple/Google Pay Work?
ByteByteGo
19 Proxy vs Reverse Proxy (Real-world Examples)
Proxy vs Reverse Proxy (Real-world Examples)
ByteByteGo
20 What is API Gateway?
What is API Gateway?
ByteByteGo
21 What Is GraphQL? REST vs. GraphQL
What Is GraphQL? REST vs. GraphQL
ByteByteGo
22 What Is Single Sign-on (SSO)? How It Works
What Is Single Sign-on (SSO)? How It Works
ByteByteGo
23 What Is A CDN? How Does It Work?
What Is A CDN? How Does It Work?
ByteByteGo
24 What is RPC? gRPC Introduction.
What is RPC? gRPC Introduction.
ByteByteGo
25 SSL, TLS, HTTPS Explained
SSL, TLS, HTTPS Explained
ByteByteGo
26 FANG Interview Question | Process vs Thread
FANG Interview Question | Process vs Thread
ByteByteGo
27 What is OSI Model | Real World Examples
What is OSI Model | Real World Examples
ByteByteGo
28 CAP Theorem Simplified
CAP Theorem Simplified
ByteByteGo
29 Kubernetes Explained in 6 Minutes | k8s Architecture
Kubernetes Explained in 6 Minutes | k8s Architecture
ByteByteGo
30 CI/CD In 5 Minutes | Is It Worth The Hassle: Crash Course System Design #2
CI/CD In 5 Minutes | Is It Worth The Hassle: Crash Course System Design #2
ByteByteGo
31 Why Is System Design Interview Important?
Why Is System Design Interview Important?
ByteByteGo
32 8 Key Data Structures That Power Modern Databases
8 Key Data Structures That Power Modern Databases
ByteByteGo
33 System Design Interview: A Step-By-Step Guide
System Design Interview: A Step-By-Step Guide
ByteByteGo
34 Top 5 Redis Use Cases
Top 5 Redis Use Cases
ByteByteGo
35 Debugging Like A Pro
Debugging Like A Pro
ByteByteGo
36 But What Is Cloud Native Really All About?
But What Is Cloud Native Really All About?
ByteByteGo
37 Everything You Need to Know About DNS: Crash Course System Design #4
Everything You Need to Know About DNS: Crash Course System Design #4
ByteByteGo
38 The Most Beloved Burger for Developers
The Most Beloved Burger for Developers
ByteByteGo
39 10+ Key Memory & Storage Systems: Crash Course System Design #5
10+ Key Memory & Storage Systems: Crash Course System Design #5
ByteByteGo
40 Cache Systems Every Developer Should Know
Cache Systems Every Developer Should Know
ByteByteGo
41 Top 7 ChatGPT Developer Hacks
Top 7 ChatGPT Developer Hacks
ByteByteGo
42 How ChatGPT Works Technically | ChatGPT Architecture
How ChatGPT Works Technically | ChatGPT Architecture
ByteByteGo
43 10 Key Data Structures We Use Every Day
10 Key Data Structures We Use Every Day
ByteByteGo
44 Top 7 Most-Used Distributed System Patterns
Top 7 Most-Used Distributed System Patterns
ByteByteGo
45 Secret To Optimizing SQL Queries - Understand The SQL Execution Order
Secret To Optimizing SQL Queries - Understand The SQL Execution Order
ByteByteGo
46 Amazon Prime Video Ditches AWS Serverless, Saves 90%
Amazon Prime Video Ditches AWS Serverless, Saves 90%
ByteByteGo
47 Top 6 Most Popular API Architecture Styles
Top 6 Most Popular API Architecture Styles
ByteByteGo
48 Top 5 Most-Used Deployment Strategies
Top 5 Most-Used Deployment Strategies
ByteByteGo
49 How Discord Stores TRILLIONS of Messages
How Discord Stores TRILLIONS of Messages
ByteByteGo
50 Uncovering Stack Overflow's Shocking Architecture
Uncovering Stack Overflow's Shocking Architecture
ByteByteGo
51 OAuth 2 Explained In Simple Terms
OAuth 2 Explained In Simple Terms
ByteByteGo
52 Demystifying the Unusual Evolution of the Netflix API Architecture
Demystifying the Unusual Evolution of the Netflix API Architecture
ByteByteGo
53 1 Year Of YouTube | Best System Design Series
1 Year Of YouTube | Best System Design Series
ByteByteGo
54 DevOps vs SRE vs Platform Engineering | Clear Big Misconceptions
DevOps vs SRE vs Platform Engineering | Clear Big Misconceptions
ByteByteGo
55 Top 7 Ways to 10x Your API Performance
Top 7 Ways to 10x Your API Performance
ByteByteGo
56 Why Google and Meta Put Billion Lines of Code In 1 Repository?
Why Google and Meta Put Billion Lines of Code In 1 Repository?
ByteByteGo
57 Git MERGE vs REBASE: Everything You Need to Know
Git MERGE vs REBASE: Everything You Need to Know
ByteByteGo
58 Top 6 Load Balancing Algorithms Every Developer Should Know
Top 6 Load Balancing Algorithms Every Developer Should Know
ByteByteGo
59 Algorithms You Should Know Before System Design Interviews
Algorithms You Should Know Before System Design Interviews
ByteByteGo
60 Top 5 Most Used Architecture Patterns
Top 5 Most Used Architecture Patterns
ByteByteGo

This video explains how key-value stores work, covering Redis, DynamoDB, and Memcached, and is relevant for tech interview prep and systems design. Key-value stores are a fundamental concept in systems design, and understanding how they work is crucial for building scalable systems. By watching this video, viewers can gain a deeper understanding of key-value stores and how to design and implement them.

Key Takeaways
  1. Understand the basics of key-value stores
  2. Learn how Redis works
  3. Learn how DynamoDB works
  4. Learn how Memcached works
  5. Design a key-value store for a given use case
  6. Implement a key-value store using Redis or DynamoDB
💡 Key-value stores are a type of NoSQL database that stores data as a collection of key-value pairs, allowing for fast lookup and retrieval of data.

Related Reads

📰
MCP Series (08): Enterprise Governance — Registry, Routing, and Observability
Learn to implement enterprise governance in MCP series with registry, routing, and observability to manage multiple servers efficiently
Dev.to · WonderLab
📰
Where is the hosts file on Mac, Windows, and Linux?
Learn the location of the hosts file on Mac, Windows, and Linux to edit and configure network settings
Dev.to · Locahl
📰
Presentation: The Rust High Performance Talk You Did Not Expect
Learn how Rust's performance capabilities and compile-time safety can improve delivery velocity and reduce engineering overhead in high-performance caching services
InfoQ AI/ML
📰
I Use These Two Books for System Design & DSA — That’s It, No Clutter
Learn the two essential books for system design and data structures and algorithms to crack interviews at top tech companies
Medium · Programming
Up next
Forget Fable 5: Build Your SaaS with Opus 4.8! #shorts
Income stream surfers
Watch →