Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.83 KB | None | 0 0
  1. PD
  2.  
  3. EXPLORE
  4.  
  5. TRACKS
  6.  
  7. MY COURSES
  8.  
  9. EDPRESSO
  10.  
  11. TEACH
  12. Search Course
  13. Grokking the System Design Interview
  14. 51% COMPLETEDReset
  15. System Design Problems
  16. System Design Interviews: A step by step guide
  17. Designing a URL Shortening service like TinyURL
  18. Designing Pastebin
  19. Designing Instagram
  20. Designing Dropbox
  21. Designing Facebook Messenger
  22. Designing Twitter
  23. Designing Youtube or Netflix
  24. Designing Typeahead Suggestion
  25. Designing an API Rate Limiter
  26. Designing Twitter Search
  27. Designing a Web Crawler
  28. Designing Facebook’s Newsfeed
  29. Designing Yelp or Nearby Friends
  30. Designing Uber backend
  31. Design Ticketmaster (*New*)
  32. Additional Resources
  33. Glossary of System Design Basics
  34. System Design Basics
  35. Key Characteristics of Distributed Systems
  36. Load Balancing
  37. Caching
  38. Data Partitioning
  39. Indexes
  40. Proxies
  41. Redundancy and Replication
  42. SQL vs. NoSQL
  43. CAP Theorem
  44. Consistent Hashing
  45. Long-Polling vs WebSockets vs Server-Sent Events
  46. Appendix
  47. Contact Us
  48. Other courses
  49. Mark Course as Completed
  50. OPTIONS
  51. Designing Pastebin
  52. Let's design a Pastebin like web service, where users can store plain text. Users of the service will enter a piece of text and get a randomly generated URL to access it.
  53.  
  54. Similar Services: pastebin.com, pasted.co, chopapp.com
  55. Difficulty Level: Easy
  56.  
  57. 1. What is Pastebin?
  58. Pastebin like services enable users to store plain text or images over the network (typically the Internet) and generate unique URLs to access the uploaded data. Such services are also used to share data over the network quickly, as users would just need to pass the URL to let other users see it.
  59.  
  60. If you haven’t used pastebin.com before, please try creating a new ‘Paste’ there and spend some time going through the different options their service offers. This will help you a lot in understanding this chapter.
  61.  
  62. 2. Requirements and Goals of the System
  63. Our Pastebin service should meet the following requirements:
  64.  
  65. Functional Requirements:
  66.  
  67. Users should be able to upload or “paste” their data and get a unique URL to access it.
  68. Users will only be able to upload text.
  69. Data and links will expire after a specific timespan automatically; users should also be able to specify expiration time.
  70. Users should optionally be able to pick a custom alias for their paste.
  71. Non-Functional Requirements:
  72.  
  73. The system should be highly reliable, any data uploaded should not be lost.
  74. The system should be highly available. This is required because if our service is down, users will not be able to access their Pastes.
  75. Users should be able to access their Pastes in real-time with minimum latency.
  76. Paste links should not be guessable (not predictable).
  77. Extended Requirements:
  78.  
  79. Analytics, e.g., how many times a paste was accessed?
  80. Our service should also be accessible through REST APIs by other services.
  81. 3. Some Design Considerations
  82. Pastebin shares some requirements with URL Shortening service, but there are some additional design considerations we should keep in mind.
  83.  
  84. What should be the limit on the amount of text user can paste at a time? We can limit users not to have Pastes bigger than 10MB to stop the abuse of the service.
  85.  
  86. Should we impose size limits on custom URLs? Since our service supports custom URLs, users can pick any URL that they like, but providing a custom URL is not mandatory. However, it is reasonable (and often desirable) to impose a size limit on custom URLs, so that we have a consistent URL database.
  87.  
  88. 4. Capacity Estimation and Constraints
  89. Our services will be read-heavy; there will be more read requests compared to new Pastes creation. We can assume a 5:1 ratio between read and write.
  90.  
  91. Traffic estimates: Pastebin services are not expected to have traffic similar to Twitter or Facebook, let’s assume here that we get one million new pastes added to our system every day. This leaves us with five million reads per day.
  92.  
  93. New Pastes per second:
  94.  
  95. 1M / (24 hours * 3600 seconds) ~= 12 pastes/sec
  96. Paste reads per second:
  97.  
  98. 5M / (24 hours * 3600 seconds) ~= 58 reads/sec
  99. Storage estimates: Users can upload maximum 10MB of data; commonly Pastebin like services are used to share source code, configs or logs. Such texts are not huge, so let’s assume that each paste on average contains 10KB.
  100.  
  101. At this rate, we will be storing 10GB of data per day.
  102.  
  103. 1M * 10KB => 10 GB/day
  104. If we want to store this data for ten years we would need the total storage capacity of 36TB.
  105.  
  106. With 1M pastes every day we will have 3.6 billion Pastes in 10 years. We need to generate and store keys to uniquely identify these pastes. If we use base64 encoding ([A-Z, a-z, 0-9, ., -]) we would need six letters strings:
  107.  
  108. 64^6 ~= 68.7 billion unique strings
  109. If it takes one byte to store one character, total size required to store 3.6B keys would be:
  110.  
  111. 3.6B * 6 => 22 GB
  112. 22GB is negligible compared to 36TB. To keep some margin, we will assume a 70% capacity model (meaning we don’t want to use more than 70% of our total storage capacity at any point), which raises our storage needs to 51.4TB.
  113.  
  114. Bandwidth estimates: For write requests, we expect 12 new pastes per second, resulting in 120KB of ingress per second.
  115.  
  116. 12 * 10KB => 120 KB/s
  117. As for the read request, we expect 58 requests per second. Therefore, total data egress (sent to users) will be 0.6 MB/s.
  118.  
  119. 58 * 10KB => 0.6 MB/s
  120. Although total ingress and egress are not big, we should keep these numbers in mind while designing our service.
  121.  
  122. Memory estimates: We can cache some of the hot pastes that are frequently accessed. Following the 80-20 rule, meaning 20% of hot pastes generate 80% of traffic, we would like to cache these 20% pastes
  123.  
  124. Since we have 5M read requests per day, to cache 20% of these requests, we would need:
  125.  
  126. 0.2 * 5M * 10KB ~= 10 GB
  127. 5. System APIs
  128. We can have SOAP or REST APIs to expose the functionality of our service. Following could be the definitions of the APIs to create/retrieve/delete Pastes:
  129.  
  130. addPaste(api_dev_key, paste_data, custom_url=None user_name=None, paste_name=None, expire_date=None)
  131. Parameters:
  132. api_dev_key (string): The API developer key of a registered account. This will be used to, among other things, throttle users based on their allocated quota.
  133. paste_data (string): Textual data of the paste.
  134. custom_url (string): Optional custom URL.
  135. user_name (string): Optional user name to be used to generate URL.
  136. paste_name (string): Optional name of the paste
  137. expire_date (string): Optional expiration date for the paste.
  138.  
  139. Returns: (string)
  140. A successful insertion returns the URL through which the paste can be accessed, otherwise, it will return an error code.
  141.  
  142. Similarly, we can have retrieve and delete Paste APIs:
  143.  
  144. getPaste(api_dev_key, api_paste_key)
  145. Where “api_paste_key” is a string representing the Paste Key of the paste to be retrieved. This API will return the textual data of the paste.
  146.  
  147. deletePaste(api_dev_key, api_paste_key)
  148. A successful deletion returns ‘true’, otherwise returns ‘false’.
  149.  
  150. 6. Database Design
  151. A few observations about the nature of the data we are storing:
  152.  
  153. We need to store billions of records.
  154. Each metadata object we are storing would be small (less than 1KB).
  155. Each paste object we are storing can be of medium size (it can be a few MB).
  156. There are no relationships between records, except if we want to store which user created what Paste.
  157. Our service is read-heavy.
  158. Database Schema:
  159. We would need two tables, one for storing information about the Pastes and the other for users’ data.
  160.  
  161.  
  162. Here, ‘URlHash’ is the URL equivalent of the TinyURL and ‘ContentKey’ is a reference to an external object storing the contents of the paste; we’ll discuss the external storage of the paste contents later in the chapter.
  163.  
  164. 7. High Level Design
  165. At a high level, we need an application layer that will serve all the read and write requests. Application layer will talk to a storage layer to store and retrieve data. We can segregate our storage layer with one database storing metadata related to each paste, users, etc., while the other storing the paste contents in some object storage (like Amazon S3). This division of data will also allow us to scale them individually.
  166.  
  167.  
  168. 8. Component Design
  169. a. Application layer
  170. Our application layer will process all incoming and outgoing requests. The application servers will be talking to the backend data store components to serve the requests.
  171.  
  172. How to handle a write request? Upon receiving a write request, our application server will generate a six-letter random string, which would serve as the key of the paste (if the user has not provided a custom key). The application server will then store the contents of the paste and the generated key in the database. After the successful insertion, the server can return the key to the user. One possible problem here could be that the insertion fails because of a duplicate key. Since we are generating a random key, there is a possibility that the newly generated key could match an existing one. In that case, we should regenerate a new key and try again. We should keep retrying until we don’t see failure due to the duplicate key. We should return an error to the user if the custom key they have provided is already present in our database.
  173.  
  174. Another solution of the above problem could be to run a standalone Key Generation Service (KGS) that generates random six letters strings beforehand and stores them in a database (let’s call it key-DB). Whenever we want to store a new paste, we will just take one of the already generated keys and use it. This approach will make things quite simple and fast since we will not be worrying about duplications or collisions. KGS will make sure all the keys inserted in key-DB are unique. KGS can use two tables to store keys, one for keys that are not used yet and one for all the used keys. As soon as KGS gives some keys to an application server, it can move these to the used keys table. KGS can always keep some keys in memory so that whenever a server needs them, it can quickly provide them. As soon as KGS loads some keys in memory, it can move them to the used keys table, this way we can make sure each server gets unique keys. If KGS dies before using all the keys loaded in memory, we will be wasting those keys. We can ignore these keys given that we have a huge number of them.
  175.  
  176. Isn’t KGS a single point of failure? Yes, it is. To solve this, we can have a standby replica of KGS and whenever the primary server dies it can take over to generate and provide keys.
  177.  
  178. Can each app server cache some keys from key-DB? Yes, this can surely speed things up. Although in this case, if the application server dies before consuming all the keys, we will end up losing those keys. This could be acceptable since we have 68B unique six letters keys, which are a lot more than we require.
  179.  
  180. How does it handle a paste read request? Upon receiving a read paste request, the application service layer contacts the datastore. The datastore searches for the key, and if it is found, returns the paste’s contents. Otherwise, an error code is returned.
  181.  
  182. b. Datastore layer
  183. We can divide our datastore layer into two:
  184.  
  185. Metadata database: We can use a relational database like MySQL or a Distributed Key-Value store like Dynamo or Cassandra.
  186. Object storage: We can store our contents in an Object Storage like Amazon’s S3. Whenever we feel like hitting our full capacity on content storage, we can easily increase it by adding more servers.
  187.  
  188. Detailed component design for Pastebin
  189. 9. Purging or DB Cleanup
  190. Please see Designing a URL Shortening service.
  191.  
  192. 10. Data Partitioning and Replication
  193. Please see Designing a URL Shortening service.
  194.  
  195. 11. Cache and Load Balancer
  196. Please see Designing a URL Shortening service.
  197.  
  198. 12. Security and Permissions
  199. Please see Designing a URL Shortening service.
  200.  
  201. Mark as Completed
  202. ← Back
  203. Designing a URL Shortening service like TinyURL
  204. Next →
  205. Designing Instagram
  206. Stuck? Get help on
  207. DISCUSS
  208. Send feedback
  209. 31 Recommendations
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement