### REST API Call Example Source: https://github.com/donnemartin/system-design-primer/blob/master/solutions/system_design/social_graph/README.md Demonstrates how to make a GET request to a social network's REST API to search for friends. ```bash $ curl https://social.com/api/v1/friend_search?person_id=1234 ``` -------------------------------- ### Sample RPC GET Request Source: https://github.com/donnemartin/system-design-primer/blob/master/README.md Illustrates a basic GET request format used in RPC for invoking an operation. ```http GET /someoperation?data=anId ``` -------------------------------- ### Sample REST GET Request Source: https://github.com/donnemartin/system-design-primer/blob/master/README.md Shows a typical GET request in REST for retrieving a resource identified by an ID. ```http GET /someresources/anId ``` -------------------------------- ### Search API Request Source: https://github.com/donnemartin/system-design-primer/blob/master/solutions/system_design/twitter/README.md Example of a client making a search request to a REST API. Ensure the query parameters are correctly formatted. ```bash $ curl https://twitter.com/api/v1/search?query=hello+world ``` -------------------------------- ### Fetch Popular Products by Category (REST API) Source: https://github.com/donnemartin/system-design-primer/blob/master/solutions/system_design/sales_rank/README.md This example demonstrates how to fetch popular products for a given category using a REST API. Ensure the category ID is valid. ```bash $ curl https://amazon.com/api/v1/popular?category_id=1234 ``` -------------------------------- ### REST API Response Example Source: https://github.com/donnemartin/system-design-primer/blob/master/solutions/system_design/social_graph/README.md Shows a sample JSON response from a REST API, listing person details. ```json { "person_id": "100", "name": "foo", "link": "https://social.com/foo", }, { "person_id": "53", "name": "bar", "link": "https://social.com/bar", }, { "person_id": "1234", "name": "baz", "link": "https://social.com/baz", } ``` -------------------------------- ### REST API Search Query Example Source: https://github.com/donnemartin/system-design-primer/blob/master/solutions/system_design/web_crawler/README.md This is an example of a client-side request to a search API using curl. The response format is JSON, containing title, snippet, and link for each search result. ```bash $ curl https://search.com/api/v1/search?query=hello+world ``` -------------------------------- ### Twitter-like API - Get Home Timeline Source: https://context7.com/donnemartin/system-design-primer/llms.txt Example using curl to retrieve a user's home timeline, which includes tweets from followed users. The API endpoint requires the user ID as a query parameter. ```bash # Get home timeline (tweets from followed users) $ curl https://twitter.com/api/v1/home_timeline?user_id=123 ``` -------------------------------- ### REST API POST Request for Account Creation Source: https://github.com/donnemartin/system-design-primer/blob/master/solutions/system_design/mint/README.md Example of a cURL command to send account information to a REST API endpoint for creating a new financial account. Includes sample JSON payload. ```bash $ curl -X POST --data '{ "user_id": "foo", "account_url": "bar", "account_login": "baz", "account_password": "qux" }' https://mint.com/api/v1/account ``` -------------------------------- ### Popular Products Response Example Source: https://github.com/donnemartin/system-design-primer/blob/master/solutions/system_design/sales_rank/README.md This is a sample JSON response structure for popular products, showing product ID, category ID, and total sold count. Note the trailing comma in the last object, which might indicate an incomplete or malformed response in some parsers. ```json { "id": "100", "category_id": "1234", "total_sold": "100000", "product_id": "50" }, { "id": "53", "category_id": "1234", "total_sold": "90000", "product_id": "200" }, { "id": "75", "category_id": "1234", "total_sold": "80000", "product_id": "3" } ``` -------------------------------- ### URL Shortener/Pastebin API - Create Paste Source: https://context7.com/donnemartin/system-design-primer/llms.txt Example using curl to create a new paste with an expiration time. The API endpoint expects a JSON payload with paste contents and expiration details. ```bash # Create a new paste with expiration $ curl -X POST --data '{ "expiration_length_in_minutes": "60", \ "paste_contents": "Hello World!" }' \ https://pastebin.com/api/v1/paste ``` -------------------------------- ### Initialize Seller Category Map Source: https://github.com/donnemartin/system-design-primer/blob/master/solutions/system_design/mint/README.md Initializes a dictionary to map sellers to their categories. This map can be pre-seeded with popular sellers. ```python seller_category_map = {} seller_category_map['Exxon'] = DefaultCategories.GAS seller_category_map['Target'] = DefaultCategories.SHOPPING ... ``` -------------------------------- ### GET /api/v1/user_timeline - Get User Timeline Source: https://github.com/donnemartin/system-design-primer/blob/master/solutions/system_design/twitter/README.md Retrieves the timeline of tweets posted by a specific user. This API fetches data directly from the SQL database. ```APIDOC ## GET /api/v1/user_timeline ### Description Retrieves the timeline of tweets posted by a specific user. This endpoint fetches all tweets authored by the specified user, typically from a relational database. ### Method GET ### Endpoint /api/v1/user_timeline ### Parameters #### Query Parameters - **user_id** (string) - Required - The ID of the user whose timeline is to be retrieved. ### Request Example ```bash curl https://twitter.com/api/v1/user_timeline?user_id=123 ``` ### Response #### Success Response (200) Returns a list of tweets posted by the specified user, similar in structure to the home timeline response. - **user_id** (string) - The ID of the user who posted the tweet. - **tweet_id** (string) - The ID of the tweet. - **status** (string) - The content of the tweet. #### Response Example ```json [ { "user_id": "123", "tweet_id": "tweet1", "status": "My first tweet!" }, { "user_id": "123", "tweet_id": "tweet2", "status": "Another thought." } ] ``` ``` -------------------------------- ### Generate Short URL using MD5 and Base 62 Source: https://github.com/donnemartin/system-design-primer/blob/master/solutions/system_design/pastebin/README.md Illustrates how to generate a short URL by taking the first 7 characters of a Base 62 encoded MD5 hash of the user's IP address and timestamp. This method ensures uniqueness and creates a URL-friendly string. ```python url = base_encode(md5(ip_address+timestamp))[:URL_LENGTH] ``` -------------------------------- ### GET /api/v1/home_timeline - Get Home Timeline Source: https://github.com/donnemartin/system-design-primer/blob/master/solutions/system_design/twitter/README.md Retrieves the home timeline for a given user, displaying tweets from users they follow. This API is optimized for fast reads using a memory cache. ```APIDOC ## GET /api/v1/home_timeline ### Description Retrieves the home timeline for a specified user. This timeline consists of tweets from users that the requesting user follows. The data is primarily fetched from a memory cache for performance. ### Method GET ### Endpoint /api/v1/home_timeline ### Parameters #### Query Parameters - **user_id** (string) - Required - The ID of the user whose home timeline is to be retrieved. ### Request Example ```bash curl https://twitter.com/api/v1/home_timeline?user_id=123 ``` ### Response #### Success Response (200) Returns a list of tweets, where each tweet object contains: - **user_id** (string) - The ID of the user who posted the tweet. - **tweet_id** (string) - The ID of the tweet. - **status** (string) - The content of the tweet. #### Response Example ```json [ { "user_id": "456", "tweet_id": "123", "status": "foo" }, { "user_id": "789", "tweet_id": "456", "status": "bar" }, { "user_id": "789", "tweet_id": "579", "status": "baz" } ] ``` ``` -------------------------------- ### Clone Repository Source: https://github.com/donnemartin/system-design-primer/blob/master/CONTRIBUTING.md Clone the forked repository to your local machine. Navigate into the cloned directory. ```bash $ git clone git@github.com:YourLogin/system-design-primer.git $ cd system-design-primer ``` -------------------------------- ### Sample RPC POST Request Source: https://github.com/donnemartin/system-design-primer/blob/master/README.md Demonstrates a POST request with a JSON payload for an RPC operation. ```http POST /anotheroperation { "data":"anId"; "anotherdata": "another value" } ``` -------------------------------- ### REST API Search Response Example Source: https://github.com/donnemartin/system-design-primer/blob/master/solutions/system_design/web_crawler/README.md This JSON structure represents the response from a search API, providing details for each relevant document found. ```json { "title": "foo's title", "snippet": "foo's snippet", "link": "https://foo.com", }, { "title": "bar's title", "snippet": "bar's snippet", "link": "https://bar.com", }, { "title": "baz's title", "snippet": "baz's snippet", "link": "https://baz.com", } ``` -------------------------------- ### Post Tweet REST API Response Source: https://github.com/donnemartin/system-design-primer/blob/master/solutions/system_design/twitter/README.md Example response structure after successfully posting a tweet. It includes creation timestamp, status, and tweet ID. ```json { "created_at": "Wed Sep 05 00:37:15 +0000 2012", "status": "hello world!", "tweet_id": "987", "user_id": "123", ... } ``` -------------------------------- ### Person Server Implementation Source: https://github.com/donnemartin/system-design-primer/blob/master/solutions/system_design/social_graph/README.md Manages a collection of Person objects, allowing for adding new people and retrieving them by their IDs. Assumes a 'people' dictionary stores person objects keyed by their IDs. ```python class PersonServer(object): def __init__(self): self.people = {} # key: person_id, value: person def add_person(self, person): ... def people(self, ids): results = [] for id in ids: if id in self.people: results.append(self.people[id]) return results ``` -------------------------------- ### Twitter-like API - Search Tweets Source: https://context7.com/donnemartin/system-design-primer/llms.txt Example using curl to search for tweets based on a query. The API endpoint accepts the search query as a URL parameter. ```bash # Search tweets $ curl https://twitter.com/api/v1/search?query=hello+world ``` -------------------------------- ### URL Shortener/Pastebin API - Retrieve Paste Source: https://context7.com/donnemartin/system-design-primer/llms.txt Example using curl to retrieve paste contents using a shortlink. The API endpoint expects a shortlink as a query parameter. ```bash # Retrieve paste contents $ curl https://pastebin.com/api/v1/paste?shortlink=foobar ``` -------------------------------- ### Lookup Service Implementation Source: https://github.com/donnemartin/system-design-primer/blob/master/solutions/system_design/social_graph/README.md Provides a mechanism to map person IDs to the specific Person Server that stores their data. Requires initialization of the lookup table. ```python class LookupService(object): def __init__(self): self.lookup = self._init_lookup() # key: person_id, value: person_server def _init_lookup(self): ... def lookup_person_server(self, person_id): return self.lookup[person_id] ``` -------------------------------- ### Budget Class for Budget Template Source: https://github.com/donnemartin/system-design-primer/blob/master/solutions/system_design/mint/README.md Defines a Budget class that initializes a budget template based on income. Use this to create a default budget structure. ```python class Budget(object): def __init__(self, income): self.income = income self.categories_to_budget_map = self.create_budget_template() def create_budget_template(self): return { DefaultCategories.HOUSING: self.income * .4, DefaultCategories.FOOD: self.income * .2, DefaultCategories.GAS: self.income * .1, DefaultCategories.SHOPPING: self.income * .2, ... } def override_category_budget(self, category, amount): self.categories_to_budget_map[category] = amount ``` -------------------------------- ### Home Timeline REST API Response Source: https://github.com/donnemartin/system-design-primer/blob/master/solutions/system_design/twitter/README.md Example response structure for a user's home timeline. Each object represents a tweet with user ID, tweet ID, and status. ```json { "user_id": "456", "tweet_id": "123", "status": "foo" }, { "user_id": "789", "tweet_id": "456", "status": "bar" }, { "user_id": "789", "tweet_id": "579", "status": "baz" }, ``` -------------------------------- ### Parking Lot Implementation in Python Source: https://github.com/donnemartin/system-design-primer/blob/master/solutions/object_oriented_design/parking_lot/parking_lot.ipynb Defines the core classes for a parking lot system, including vehicle types, parking spots, levels, and the parking lot manager. Supports different vehicle sizes and multi-level parking. ```python from abc import ABCMeta, abstractmethod class VehicleSize(Enum): MOTORCYCLE = 0 COMPACT = 1 LARGE = 2 class Vehicle(metaclass=ABCMeta): def __init__(self, vehicle_size, license_plate, spot_size): self.vehicle_size = vehicle_size self.license_plate = license_plate self.spot_size = spot_size self.spots_taken = [] def clear_spots(self): for spot in self.spots_taken: spot.remove_vehicle(self) self.spots_taken = [] def take_spot(self, spot): self.spots_taken.append(spot) @abstractmethod def can_fit_in_spot(self, spot): pass class Motorcycle(Vehicle): def __init__(self, license_plate): super(Motorcycle, self).__init__(VehicleSize.MOTORCYCLE, license_plate, spot_size=1) def can_fit_in_spot(self, spot): return True class Car(Vehicle): def __init__(self, license_plate): super(Car, self).__init__(VehicleSize.COMPACT, license_plate, spot_size=1) def can_fit_in_spot(self, spot): return True if (spot.size == LARGE or spot.size == COMPACT) else False class Bus(Vehicle): def __init__(self, license_plate): super(Bus, self).__init__(VehicleSize.LARGE, license_plate, spot_size=5) def can_fit_in_spot(self, spot): return True if spot.size == LARGE else False class ParkingLot(object): def __init__(self, num_levels): self.num_levels = num_levels self.levels = [] def park_vehicle(self, vehicle): for level in levels: if level.park_vehicle(vehicle): return True return False class Level(object): SPOTS_PER_ROW = 10 def __init__(self, floor, total_spots): self.floor = floor self.num_spots = total_spots self.available_spots = 0 self.parking_spots = [] def spot_freed(self): self.available_spots += 1 def park_vehicle(self, vehicle): spot = self._find_available_spot(vehicle) if spot is None: return None else: spot.park_vehicle(vehicle) return spot def _find_available_spot(self, vehicle): """Find an available spot where vehicle can fit, or return None""" # ... def _park_starting_at_spot(self, spot, vehicle): """Occupy starting at spot.spot_number to vehicle.spot_size.""" # ... class ParkingSpot(object): def __init__(self, level, row, spot_number, spot_size, vehicle_size): self.level = level self.row = row self.spot_number = spot_number self.spot_size = spot_size self.vehicle_size = vehicle_size self.vehicle = None def is_available(self): return True if self.vehicle is None else False def can_fit_vehicle(self, vehicle): if self.vehicle is not None: return False return vehicle.can_fit_in_spot(self) def park_vehicle(self, vehicle): # ... def remove_vehicle(self): # ... ``` -------------------------------- ### Push Changes to Origin Source: https://github.com/donnemartin/system-design-primer/blob/master/CONTRIBUTING.md Push your feature branch to your fork on GitHub. ```bash $ git push -u origin my-feature ``` -------------------------------- ### Sample REST PUT Request Source: https://github.com/donnemartin/system-design-primer/blob/master/README.md Demonstrates a PUT request with a JSON payload for updating a resource in REST. ```http PUT /someresources/anId {"anotherdata": "another value"} ``` -------------------------------- ### Twitter-like API - Post Tweet Source: https://context7.com/donnemartin/system-design-primer/llms.txt Example using curl to post a new tweet. The API endpoint expects a JSON payload with user ID, authentication token, status message, and optional media IDs. ```bash # Post a new tweet $ curl -X POST --data '{ "user_id": "123", "auth_token": "ABC123", \ "status": "hello world!", "media_ids": "ABC987" }' \ https://twitter.com/api/v1/tweet ``` -------------------------------- ### Implement Hash Map with Chaining in Python Source: https://github.com/donnemartin/system-design-primer/blob/master/solutions/object_oriented_design/hash_table/hash_map.ipynb This Python code implements a hash map using chaining for collision resolution. It includes methods for setting, getting, and removing key-value pairs. Assumes integer keys and does not consider load factors. ```python class Item(object): def __init__(self, key, value): self.key = key self.value = value class HashTable(object): def __init__(self, size): self.size = size self.table = [[] for _ in range(self.size)] def _hash_function(self, key): return key % self.size def set(self, key, value): hash_index = self._hash_function(key) for item in self.table[hash_index]: if item.key == key: item.value = value return self.table[hash_index].append(Item(key, value)) def get(self, key): hash_index = self._hash_function(key) for item in self.table[hash_index]: if item.key == key: return item.value raise KeyError('Key not found') def remove(self, key): hash_index = self._hash_function(key) for index, item in enumerate(self.table[hash_index]): if item.key == key: del self.table[hash_index][index] return raise KeyError('Key not found') ``` -------------------------------- ### MapReduce Job for Website Hit Counts Source: https://github.com/donnemartin/system-design-primer/blob/master/solutions/system_design/pastebin/README.md Implement a MapReduce job using Python to process web server logs and calculate hit counts per URL per month. Requires the MRJob library. ```python class HitCounts(MRJob): def extract_url(self, line): """Extract the generated url from the log line.""" ... def extract_year_month(self, line): """Return the year and month portions of the timestamp.""" ... def mapper(self, _, line): """Parse each log line, extract and transform relevant lines. Emit key value pairs of the form: (2016-01, url0), 1 (2016-01, url0), 1 (2016-01, url1), 1 """ url = self.extract_url(line) period = self.extract_year_month(line) yield (period, url), 1 def reducer(self, key, values): """Sum values for each key. (2016-01, url0), 2 (2016-01, url1), 1 """ yield key, sum(values) ``` -------------------------------- ### Cache-Aside Pattern: Get User Data Source: https://github.com/donnemartin/system-design-primer/blob/master/README.md Retrieves user data using the cache-aside pattern. It first checks the cache; if the data is not found (cache miss), it fetches from the database, stores it in the cache, and then returns it. This pattern is also known as lazy loading. ```python def get_user(self, user_id): user = cache.get("user.{0}", user_id) if user is None: user = db.query("SELECT * FROM users WHERE user_id = {0}", user_id) if user is not None: key = "user.{0}".format(user_id) cache.set(key, json.dumps(user)) return user ``` -------------------------------- ### Query API Server Implementation Source: https://github.com/donnemartin/system-design-primer/blob/master/solutions/system_design/query_cache/README.md Handles parsing and processing of user queries, utilizing a memory cache and a reverse index service. Results are fetched from the cache or generated and then stored. ```python class QueryApi(object): def __init__(self, memory_cache, reverse_index_service): self.memory_cache = memory_cache self.reverse_index_service = reverse_index_service def parse_query(self, query): """Remove markup, break text into terms, deal with typos, normalize capitalization, convert to use boolean operations. """ ... def process_query(self, query): query = self.parse_query(query) results = self.memory_cache.get(query) if results is None: results = self.reverse_index_service.process_search(query) self.memory_cache.set(query, results) return results ``` -------------------------------- ### Shortest Path Algorithm (BFS) Source: https://github.com/donnemartin/system-design-primer/blob/master/solutions/system_design/social_graph/README.md Implements BFS to find the shortest path between two nodes in a graph. Handles edge cases like null inputs and source being the same as destination. Assumes graph nodes have 'key', 'adj_nodes', and 'visit_state' attributes. ```python class Graph(Graph): def shortest_path(self, source, dest): if source is None or dest is None: return None if source is dest: return [source.key] prev_node_keys = self._shortest_path(source, dest) if prev_node_keys is None: return None else: path_ids = [dest.key] prev_node_key = prev_node_keys[dest.key] while prev_node_key is not None: path_ids.append(prev_node_key) prev_node_key = prev_node_keys[prev_node_key] return path_ids[::-1] def _shortest_path(self, source, dest): queue = deque() queue.append(source) prev_node_keys = {source.key: None} source.visit_state = State.visited while queue: node = queue.popleft() if node is dest: return prev_node_keys prev_node = node for adj_node in node.adj_nodes.values(): if adj_node.visit_state == State.unvisited: queue.append(adj_node) prev_node_keys[adj_node.key] = prev_node.key adj_node.visit_state = State.visited return None ``` -------------------------------- ### Implement Cache-Aside Pattern in Python Source: https://context7.com/donnemartin/system-design-primer/llms.txt Use this pattern to load data into cache only when requested. It checks the cache first and falls back to the database on a miss, populating the cache for subsequent reads. ```python def get_user(self, user_id): """Cache-aside pattern: check cache first, load from DB on miss.""" user = cache.get("user.{0}", user_id) if user is None: user = db.query("SELECT * FROM users WHERE user_id = {0}", user_id) if user is not None: key = "user.{0}".format(user_id) cache.set(key, json.dumps(user)) return user ```