Simplified
Simplified

L1.1How does your photo reach your friend?

YOU

How does your photo reach your friend?

Shareyour phoneyour friend's phone✕?Instagram'sserverrestart?your photo
server
a computer that runs an app for everyone who uses it
1your phoneyour friend's phone✕?Instagram'sserverlikes: 0restart?like: your photofrom: your friend
Love thisyour phoneLove thisyour friend's phone✕?Instagram'sserverLove this photo!restart?the DM
Shareyour phoneyour friend's phone✕?Instagram'sserverrestart?your photo

Databases · Lesson 1.1

Servers and Databases

How does your photo reach your friend?

Simplified
0:00 / 1:22

You take a photo, tap Share on Instagram, and put your phone away.

A minute later, your friend opens Instagram, and your photo is in their feed.

But how did your photo get from your phone to theirs?

Well, your phone never sent it to your friend's phone.

It sent it over the internet, to one of Instagram's computers.

That computer kept your photo, and sent it to your friend's phone when their app asked.

That computer is called a server.

A server is a computer that runs an app for everyone who uses it.

Shareyour phoneyour friend's phone✕?Instagram'sserverrestart?your photo

Now what happens when your friend likes your photo?

1your phoneyour friend's phone✕?Instagram'sserverlikes: 1restart?like: your photofrom: your friend

They press the like button, and their phone sends Instagram's server a short message.

It says which photo they liked, and who they are.

The server keeps a count of likes for every photo.

So it adds one to your photo’s count, and the count is now 1.

Later, you open Instagram, and your phone asks the server for your photo.

The server sends it back with its count, and you see 1 like.

And what happens when your friend sends you a DM?

Love thisyour phoneLove thisyour friend's phone✕?Instagram'sserverLove this photo!restart?the DM

They type "Love this photo!", and their phone sends it to Instagram's server.

The server keeps it until your phone asks for your messages.

Your phone could even be switched off when they send it.

The server still has the message, so it shows up when you turn your phone on.

So two phones never talk directly.

They all talk to a server, which stays on and keeps everything until someone asks for it.

Example: How does your photo reach your friend?

You post a photo of your dog, then switch your phone off. An hour later, your sister opens Instagram and sees the photo.

Where did her phone get the photo from?

Explanation

First, your phone sent the photo to Instagram's server when you posted it.

Then, the server kept it, and sent it to your sister's phone when her app asked.

Your phone was off, so it couldn't have sent anything.

Therefore, her phone got it from Instagram's server.

Question 1

You open a MrBeast video on YouTube.

Which computer sends the video to your phone?

What survives a restart?

Shareyour phoneyour friend's phone✕?Instagram'sserverrestart?your photo
Instagram's serverrestart
your deskwhat you need right nowquick, but cleared when you're doneyour bookshelfwhat you might need laterslower, but it stays
Instagram's serverrestartmemorywhat it needs right nowfast, but wiped on a restartfeed pagediskwhat it might need laterslower, but survives a restartyour photoDMs
level 3: 60%before the restartrestartmenuafter the restart
Instagram's serverrestartmemorywhat it needs right nowfast, but wiped on a restartfeed pagediskwhat it might need laterslower, but survives a restartyour photoDMs

Databases · Lesson 1.2

Servers and Databases

What survives a restart?

Simplified
0:00 / 1:39

So your photo went from your phone to Instagram's server, and the server sends it to anyone who asks.

But servers restart often, for an update or after a crash.

So what happens to your photo when the server restarts?

That depends on where the server put it.

Because, like you, a server has two places to keep things.

Think about your desk and your bookshelf.

your deskwhat you need right nowquick, but cleared when you're doneyour bookshelfwhat you might need laterslower, but it stays

Your desk holds what you need right now, like tonight's homework.

It's quick to grab, but when you're done, you clear it, and nothing stays.

Your bookshelf holds what you might need later, like last year's notes.

It's slower to find things there, but they stay until you take them out.

A server stores information the same way.

What it needs right now goes on its desk, which is called memory. It's fast, but a restart wipes it.

What it might need later goes on its bookshelf, which is called disk. It's slower, but it survives a restart.

Instagram's serverrestartmemorywhat it needs right nowfast, but wiped on a restartfeed pagediskwhat it might need laterslower, but survives a restartyour photoDMs

You've seen this on your phone.

Restart it halfway through a game, and the game starts again from its menu.

That's because the game was in memory.

But your camera roll photos are all still there.

That's because they were on disk.

level 3: 60%before the restartrestartmenuafter the restart

So where should the server keep your photo?

Your friends will want to see your photo later, even after a restart, so it goes on disk.

Your DMs go on disk too, so a restart can't wipe a conversation.

But the feed page the server is building for you right now can sit in memory.

If the server restarts, it just builds the page again, and memory makes that fast.

Instagram's serverrestartmemorywhat it needs right nowfast, but wiped on a restartfeed pagediskwhat it might need laterslower, but survives a restartyour photoDMs

So memory is the server's desk, where it keeps what it needs right now.

And disk is its bookshelf, where it keeps what it might need later.

Example: What survives a restart?

Instagram's server restarts at 3 am. Just before, it held a half-built feed page in memory, and your new photo on disk.

After the restart, which one is still there, and why?

Explanation

First, the feed page was in memory, and memory is wiped on a restart.

Then, your photo was on disk, and disk survives a restart.

Therefore, your photo is still there, because it was on disk.

Question 1

A chat app's server keeps a list of who is online right now in memory, and every message on disk. The server crashes and restarts.

What is lost?

Question 2

A photo app's server keeps every photo in memory, because memory is faster. Then it restarts for an update.

What happens to the photos people posted?

Question 3

A running app's server records your 5 km run. You want to see it in your history next month, and the server will restart several times before then.

Where must the server keep your run?

Question 4

Disk survives a restart.

So why doesn't a server keep everything on disk?

Question 5

A server has 4 new photos on disk and 3 half-built feed pages in memory. It restarts.

How many of those 7 things are still there?

Question 6

You're halfway through a level of a game and restart your phone. The game starts again from the menu, but your camera roll photos are all still there.

Where was the half-finished level kept?

Finding your photo among millions

Instagram's serverrestartmemorywhat it needs right nowfast, but wiped on a restartfeed pagediskwhat it might need laterslower, but survives a restartyour photoDMs
sam_leesam_leeyour phoneInstagram's serverdisklikescommentsDMsyour photosmillions of accounts?
the librariankeeps every book in its placefinds the book you ask for
database
a program whose whole job is to keep data on disk and answer questions about it
Blinding Lightsnow playingyour phoneSpotify's serverdisklikescommentsDMsyour photosover 100 million songs?
Instagram'sserverdatabasediskasks✕

Databases · Lesson 1.3

Servers and Databases

Finding your photo among millions

Simplified
0:00 / 1:14

So the server saves your photo to disk. That way it's still there after a restart.

But Instagram keeps photos for millions of people, plus their likes, comments and DMs.

When your friend opens your profile, the server must find just your photos among them.

So who keeps all that data on disk, and finds yours again?

Search Instagram for a friend's username, and their profile appears almost at once.

Something had to find their account and their photos among millions, instantly.

sam_leesam_leeyour phoneInstagram's serverdisklikescommentsDMsyour photosmillions of accounts?

Think of the bookshelf again, but with millions of people's photos on it, like a huge library.

In a library, a librarian keeps every book in its place, and finds any book you ask for.

the librariankeeps every book in its placefinds the book you ask for

The server has a librarian like that too, a program called a database.

A database is a program whose whole job is to keep data on disk and answer questions about it.

Now what happens when you search Spotify for a song?

Blinding Lightsnow playingyour phoneSpotify's serverdisklikescommentsDMsyour photosover 100 million songs?

Spotify has over 100 million songs, and yours starts playing in a moment.

Spotify's server asks its database, which finds that one song among millions.

Like a librarian, the database keeps track of where everything is on disk.

Instagram'sserverdatabasediskasks✕

So only the database touches the files on disk. The server just asks it.

Example: Finding your photo among millions

You search YouTube for a video you watched last year, and it appears in a moment. YouTube's servers have restarted many times since.

Which program kept it, and found it again?

Explanation

First, YouTube's server needed that one video among millions, even after many restarts.

Then, the program whose job is to keep data on disk and find it is the database.

Therefore, YouTube's database kept it and found it.

Question 1

Spotify's server hands every new playlist to one program. That program keeps the playlist on disk and sends it back whenever someone opens it.

What is that program?

Question 2

Instagram's server needs a photo that's kept on disk by its database.

Who opens the file on disk?

Question 3

A chat app's server keeps every message in a list in its memory, and has no database. It restarts after a crash.

What happens to the messages?

Question 4

A notes app's server saves every note in its database, which keeps them on disk. The server restarts overnight. In the morning, you open a note you wrote last year.

What happens?

Question 5

Which of these is a database's job?

Question 6

You search Gmail and find an email from three years ago in a second. Gmail's servers have restarted many times since it arrived.

Where was the email kept all that time?

The two jobs of a database

Instagram'sserverdatabasestoringretrieving?
the librarianputs a new book on the shelffetches the book you asked for
Shareyour phoneInstagram'sserverdatabase
youthe librarian
storing
the database saves the data the server gives it
your profileyour friendInstagram'sserverdatabase
youthe librarian
retrieving
the database finds data and sends it back to the server
1your friend likes your photostoring2you search for a usernameretrieving3you change your biostoring
Instagram'sserverdatabasestoringretrieving?

Databases · Lesson 1.4

Servers and Databases

The two jobs of a database

Simplified
0:00 / 1:09

Instagram's database keeps millions of photos on disk.

So what can the server ask the database to do?

Just two things, and they're the same two things a librarian does.

the librarianputs a new book on the shelffetches the book you asked for

When you post a photo, the server hands it to the database to keep.

This is like handing a librarian a new book, and they put it on the shelf.

This job is called storing.

Storing means the database saves the data the server gives it.

When your friend opens your profile, the server asks the database for your photos.

This is like asking a librarian for a book, and they go fetch it for you.

This job is called retrieving.

Retrieving means the database finds data and sends it back to the server.

Instagram'sserverdatabasestoringretrieving?

So which job is each thing you do on Instagram?

1your friend likes your photostoring2you search for a usernameretrieving3you change your biostoring

When your friend likes your photo, the server sends the new like count to the database to save. That's storing.

When you search for a username, the server asks the database to find that account. That's retrieving.

When you change your bio, the server sends your new bio to the database to save. That's storing.

So when data goes into the database, that's storing. When data comes back out, that's retrieving.

Example: The two jobs of a database

You change your profile picture, and the server hands the new picture to the database. A minute later, your friend opens your profile and sees it.

Which two jobs did the database do, in order?

Explanation

First, the server gave the database your new picture to save. That's storing.

Then, the server asked the database for it, for your friend. That's retrieving.

Therefore, storing, then retrieving.

Question 1

You leave a comment on a friend's Instagram post. The server hands your comment to the database to keep.

Which job is the database doing?

Question 2

You open Instagram and scroll your feed. The server asks the database for your friends' newest posts.

Which job is the database doing?

Question 3

On Instagram, which of these makes the server ask the database to retrieve data?

Question 4

On Instagram, you post 2 photos and like 3 of a friend's photos.

How many times does the server ask the database to store something?

times

Question 5

A chat app's server hands your message to its database, which stores it on disk. But the server never asks the database for it again.

Will your friend ever see the message?

Question 6

You post 1 photo on Instagram. Then 3 friends each open your profile and see it.

How many times does the database retrieve your photo?

times

How a server asks for your photos

Instagram'sserverdatabasestoringretrieving?
Instagram'sserverdatabaseGive me the photoson this profile.result
the librarianputs a new book on the shelffetches the book you asked for
query
a question or an instruction sent to the database
Instagram'sserverdatabasequeryGive me the photoson this profile.
Instagram'sserverdatabasequeryKeep this new photo.Kept.result
Instagram'sserverdatabasequeryGive me the photoson this profile.result

Databases · Lesson 1.5

Servers and Databases

How a server asks for your photos

Simplified
0:00 / 0:58

When your friend opens your profile, the server retrieves your photos from the database.

But how does the server tell the database which photos it wants?

It sends the database a message, like "Give me the photos on this profile."

It's like asking a librarian for the book you want.

This message is called a query.

A query is a question or an instruction sent to the database.

The database finds those photos on disk and sends them back.

That answer is called the result, and every query gets one.

Instagram'sserverdatabasequeryGive me the photoson this profile.result

What does the server send when you post a photo?

Instagram'sserverdatabasequeryKeep this new photo.Kept.result

This time, the query is an instruction, "Keep this new photo."

The database stores the photo on disk.

Then it sends back a result saying the photo is kept.

The server tells your phone it worked, and your photo appears on your profile.

Whenever the server wants something from the database, it sends a query, which is either a question or an instruction.

And whatever the query, the database sends back a result.

Example: How a server asks for your photos

Your friend opens your profile. The server sends the database "Give me the photos on this profile.", and your 3 photos come back.

What are the 3 photos called?

Explanation

First, the message the server sent is the query.

Then, the answer the database sends back is the result.

Therefore, the 3 photos are the result.

Question 1

You open your Instagram DMs with Sam. The server sends the database the message "Give me the messages between these two people."

What is that message called?

Question 2

A server sends its database the message "Delete this comment." It's an instruction, not a question.

Is it a query?

Question 3

Instagram's server sends its database 5 queries, 3 questions and 2 instructions.

How many results come back?

results

Question 4

Leo taps Ana's profile on Instagram. Put these in order.

(1) the database sends back Ana's photos, (2) Leo's phone asks Instagram's server, (3) the server sends the database a query, (4) the server sends the photos to Leo's phone.

Question 5

Which of these is NOT a query a server sends to its database?

Question 6

Instagram's server wants a photo that its database keeps on disk.

Why does the server send the database a query, instead of opening the photo's file itself?

Assistant

Highlight lesson text and click Ask AI, or type a question below.