System Design
System Design
Vinh Hoang
Single Server Setup
Database
Load Balancer
Database Replication
• Better performance
• Reliability
• High availability
Load Balancer And Database Replication
Cache
CDN
System with Cache and CDN
Stateful Vs. Stateless
System with Stateless Architecture
Data Centers
Message Queue
Vertical Scaling Vs. Horizontal Scaling
Case Studies – New Feed System
• Functional Requirements:
1. Given a URL, our service should generate a shorter and unique alias of it. This is called a short link.
This link should be short enough to be easily copied and pasted into applications.
2. When users access a short link, our service should redirect them to the original link.
3. Users should optionally be able to pick a custom short link for their URL.
4. Links will expire after a standard default timespan. Users should be able to specify the expiration
time.
• Non-Functional Requirements:
1. The system should be highly available. This is required because, if our service is down, all the URL
redirections will start failing.
2. URL redirection should happen in real-time with minimal latency.
3. Shortened links should not be guessable (not predictable).
• Extended Requirements:
1. Analytics; e.g., how many times a redirection happened?
2. Our service should also be accessible through REST APIs by other services.
Capacity Estimation and Constraints
Traffic estimates: Assuming, we will have 500M new URL shortenings per
month, with 100:1 read/write ratio, we can expect 50B redirections during the
same period:
100 * 500M => 50B
What would be Queries Per Second (QPS) for our system? New URLs
shortenings per second:
500 million / (30 days * 24 hours * 3600 seconds) = ~200 URLs/s
Considering 100:1 read/write ratio, URLs redirections per second will be:
100 * 200 URLs/s = 20K/s
Capacity Estimation and Constraints
Storage estimates: Let’s assume we store every URL shortening request (and
associated shortened link) for 5 years. Since we expect to have 500M new
URLs every month, the total number of objects we expect to store will be 30
billion:
500 million * 5 years * 12 months = 30 billion
Let’s assume that each stored object will be approximately 500 bytes (just a
ballpark estimate–we will dig into it later). We will need 15TB of total storage:
30 billion * 500 bytes = 15 TB
Capacity Estimation and Constraints
Bandwidth estimates: For write requests, since we expect 200 new URLs every
second, total incoming data for our service will be 100KB per second:
200 * 500 bytes = 100 KB/s
For read requests, since every second we expect ~20K URLs redirections, total
outgoing data for our service would be 10MB per second:
20K * 500 bytes = ~10 MB/s
Capacity Estimation and Constraints
Memory estimates: If we want to cache some of the hot URLs that are
frequently accessed, how much memory will we need to store them? If we
follow the 80-20 rule, meaning 20% of URLs generate 80% of traffic, we would
like to cache these 20% hot URLs.
Since we have 20K requests per second, we will be getting 1.7 billion requests
per day:
20K * 3600 seconds * 24 hours = ~1.7 billion
High-level estimates: Assuming 500 million new URLs per month and 100:1
read:write ratio, following is the summary of the high level estimates for our
service:
System Api
• Functional Requirements
1. Users should be able to upload/download/view photos.
2. Users can perform searches based on photo/video titles.
3. Users can follow other users.
4. The system should generate and display a user’s News Feed consisting of top photos from all
the people the user follows.
• Non-functional Requirements
1. Our service needs to be highly available.
2. The acceptable latency of the system is 200ms for News Feed generation.
3. Consistency can take a hit (in the interest of availability) if a user doesn’t see a photo for a
while; it should be fine.
4. The system should be highly reliable; any uploaded photo or video should never be lost.
Capacity Estimation and Constraints
• Let’s assume we have 500M total users, with 1M daily active users.
• 2M new photos every day, 23 new photos every second.
• Average photo file size => 200KB
• Total space required for 1 day of photos
2M * 200KB => 400 GB
• Total space required for 10 years:
400GB * 365 (days a year) * 10 (years) ~= 1425TB
High Level System Design
Database Schema
Database Estimation
1. The system should support the booking of different room types like standard, deluxe,
family suite, etc.
2. Guests should be able to search the room inventory and book any available room.
3. The system should be able to retrieve information, such as who booked a particular
room, or what rooms were booked by a specific customer.
4. The system should allow customers to cancel their booking - and provide them with
a full refund if the cancelation occurs before 24 hours of the check-in date.
5. The system should be able to send notifications whenever the booking is nearing the
check-in or check-out date.
6. The system should maintain a room housekeeping log to keep track of all
housekeeping tasks.
7. Any customer should be able to add room services and food items.
8. Customers can ask for different amenities.
9. The customers should be able to pay their bills through credit card, check or cash
Use Case Diagram
1. Any library member should be able to search books by their title, author, subject category as well
by the publication date.
2. Each book will have a unique identification number and other details including a rack number
which will help to physically locate the book.
3. There could be more than one copy of a book, and library members should be able to check-out
and reserve any copy. We will call each copy of a book, a book item.
4. The system should be able to retrieve information like who took a particular book or what are the
books checked-out by a specific library member.
5. There should be a maximum limit (5) on how many books a member can check-out.
6. There should be a maximum limit (10) on how many days a member can keep a book.
7. The system should be able to collect fines for books returned after the due date.
8. Members should be able to reserve books that are not currently available.
9. The system should be able to send notifications whenever the reserved books become available,
as well as when the book is not returned within the due date.
10.Each book and member card will have a unique barcode. The system will be able to read
barcodes from books and members’ library cards.
Use Case Diagram
• Library: The central part of the organization for which this software has
been designed. It has attributes like ‘Name’ to distinguish it from any
other libraries and ‘Address’ to describe its location.
• Book: The basic building block of the system. Every book will have
ISBN, Title, Subject, Publishers, etc.
• BookItem: Any book can have multiple copies, each copy will be
considered a book item in our system. Each book item will have a unique
barcode.
• Account: We will have two types of accounts in the system, one will be a
general member, and the other will be a librarian.
• LibraryCard: Each library user will be issued a library card, which will be
used to identify users while issuing or returning books.
• BookReservation: Responsible for managing reservations against book
items.
• BookLending: Manage the checking-out of book items.
• Catalog: Catalogs contain list of books sorted on certain criteria. Our
system will support searching through four catalogs: Title, Author,
Subject, and Publish-date.
• Fine: This class will be responsible for calculating and collecting fines
from library members.
• Author: This class will encapsulate a book author.
• Rack: Books will be placed on racks. Each rack will be identified by a
rack number and will have a location identifier to describe the physical
location of the rack in the library.
• Notification: This class will take care of sending notifications to library
members.
Case Study – Design an ATM
• The main components of the ATM that will • The user can have two types of accounts:
affect interactions between the ATM and its 1) Checking, and 2) Savings, and should
users are:
be able to perform the following five
1. Card reader: to read the users’ ATM cards.
transactions on the ATM:
2. Keypad: to enter information into the ATM
e.g. PIN. cards. 1. Balance inquiry: To see the amount of
3. Screen: to display messages to the users. funds in each account.
4. Cash dispenser: for dispensing cash. 2. Deposit cash: To deposit cash.
5. Deposit slot: For users to deposit cash or 3. Deposit check: To deposit checks.
checks.
6. Printer: for printing receipts. 4. Withdraw cash: To withdraw money from
7. Communication/Network Infrastructure: it their checking account.
is assumed that the ATM has a 5. Transfer funds: To transfer funds to
communication infrastructure to communicate another account.
with the bank upon any transaction or activity.
Use Case Diagram
ATM: The main part of the system for which this software has been designed.
It has attributes like ‘atmID’ to distinguish it from other available ATMs, and
‘location’ which defines the physical address of the ATM.
CardReader: To encapsulate the ATM’s card reader used for user
authentication.
CashDispenser: To encapsulate the ATM component which will dispense
cash.
Keypad: The user will use the ATM’s keypad to enter their PIN or amounts.
Screen: Users will be shown all messages on the screen and they will select
different transactions by touching the screen.
Printer: To print receipts.
DepositSlot: User can deposit checks or cash through the deposit slot.
Bank: To encapsulate the bank which ownns the ATM. The bank will hold all
the account information and the ATM will communicate with the bank to
perform customer transactions.
Account: We’ll have two types of accounts in the system: 1)Checking and
2)Saving.
Customer: This class will encapsulate the ATM’s customer. It will have the
customer’s basic information like name, email, etc.
Card: Encapsulating the ATM card that the customer will use to authenticate
themselves. Each customer can have one card.
Transaction: Encapsulating all transactions that the customer can perform
on the ATM, like BalanceInquiry, Deposit, Withdraw, etc.
CASE STUDY
How to design a successful eCommerce system for
Amazon, eBay, FilPCart and Walmart