Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Why Databases Persist Data
📖 Scenario: You are building a simple FastAPI app to store and retrieve user notes. This app will show how data is saved persistently using a database instead of temporary memory.
🎯 Goal: Create a FastAPI app that saves notes in a dictionary and then add a configuration to simulate saving notes persistently. Finally, implement a route to add notes and a route to get all saved notes.
📋 What You'll Learn
Create a dictionary called notes_db to store notes with keys as note IDs and values as note texts
Add a variable called next_id starting at 1 to assign unique IDs to notes
Write a POST route /add_note that accepts a note text and saves it in notes_db with the current next_id, then increments next_id
Write a GET route /notes that returns all saved notes from notes_db
💡 Why This Matters
🌍 Real World
Many web apps need to save user data like notes, messages, or profiles so it stays available after the app restarts or users come back later.
💼 Career
Understanding how to persist data is essential for backend developers building APIs and services that manage user data reliably.
Progress0 / 4 steps
1
Create the initial data structure
Create a dictionary called notes_db initialized as empty and a variable called next_id set to 1 to hold note IDs.
FastAPI
Hint
Use notes_db = {} to create an empty dictionary and next_id = 1 to start IDs from 1.
2
Add configuration variable
Add a FastAPI app instance called app by importing FastAPI and creating app = FastAPI().
FastAPI
Hint
Import FastAPI and create an app instance with app = FastAPI().
3
Implement core logic to add notes
Write a POST route /add_note using @app.post("/add_note") that accepts a JSON body with a text field. Inside the function, add the note text to notes_db with the current next_id as key, then increment next_id by 1. Return the note ID in the response.
FastAPI
Hint
Use @app.post("/add_note") decorator and an async function that reads JSON from the request. Use global next_id to modify the variable.
4
Add route to get all notes
Write a GET route /notes using @app.get("/notes") that returns the entire notes_db dictionary.
FastAPI
Hint
Use @app.get("/notes") decorator and return the notes_db dictionary directly.
Practice
(1/5)
1. Why do databases persist data in applications like FastAPI?
easy
A. To keep data safe even if the app stops or restarts
B. To make the app run faster
C. To delete old data automatically
D. To prevent users from accessing the app
Solution
Step 1: Understand what persistence means
Persistence means data stays saved even after the program stops running.
Step 2: Connect persistence to databases in FastAPI
Databases store data on disk, so FastAPI can retrieve it later, even after restarts.
Final Answer:
To keep data safe even if the app stops or restarts -> Option A
Quick Check:
Persistence means data stays saved [OK]
Hint: Persistence means data stays saved after app stops [OK]
Common Mistakes:
Thinking databases speed up the app only
Confusing persistence with data deletion
Believing databases block user access
2. Which of the following is the correct way to save data to a database in FastAPI?
easy
A. Use a database session to add and commit the data
B. Print the data to the console
C. Store data in a local variable only
D. Use a global variable to hold data
Solution
Step 1: Identify how FastAPI interacts with databases
FastAPI uses database sessions to add and commit data to save it permanently.
Step 2: Compare options for saving data
Printing or using variables does not save data persistently; only committing via session does.
Final Answer:
Use a database session to add and commit the data -> Option A
Quick Check:
Commit data with session to save [OK]
Hint: Commit data with session to save persistently [OK]
Common Mistakes:
Thinking printing saves data
Using variables instead of database commit
Skipping the commit step
3. Given this FastAPI code snippet, what will happen when the app restarts?