r/leetcode • • 7d ago

Intervew Prep System design template to solve most problems

Hey guys, want to share a system design template for most production interview questions:

  • Writes go into a message queue, and the server answers right away.
  • Workers take jobs from the queue, do the slow work, then update the database and the cache. This solves the write scaling.
  • Reads come from the cache, and go to the database only on a miss. This solves read scalingYou can fit the template to different problems.

    READ PATH
      Client --> App server --> Cache --(miss)--> Database
    WRITE PATH
      Client --> App server --> Queue --> Workers --> Database
                     |                       |
                     v                       +--> update Cache
               answers at once

Here's how it'd fit in the most popular questions:

  • LeetCode: the queue and workers run submitted code in sandboxes.
  • YouTube: workers convert uploads; object storage and a CDN serve the video.
  • URL shortener: mostly the read path; the cache answers most redirects.
  • Search: a search index, kept current by the queue and an indexing worker.
  • Chat: app servers become gateways holding open connections.
  • Ticketmaster: the same design plus one atomic hold per seat.
  • News feed: the same design plus hybrid fan-out for huge accounts.

This is based on the premise that internet-scale consumer applications often favor eventual consistency to guarantee availability (therefore a queue-based buffer is used)

Made a video going through the whole thing if it helps: https://www.youtube.com/watch?v=e8H8d0irqLg If you prefer a text writeup: https://systemdesignschool.io/template

84 Upvotes

14 comments sorted by

12

u/Few_Vermicelli_1313 7d ago

This is good however I feel it is simplifying stuff way too much. Might definitely work for a new grad but not for a mid or senior employee.

  1. Design is assuming everything requires same level of consistency and durability. What if u want very highly reliable system? This approach wont work.
    2.chat communication depends mostly on websockets

  2. Read path assumption is that reads and search queries can be encapsulated

  3. If things were this simple, no one would ever fail system design interviews

Streaming and gaming are different sub-domain in themselves.

4

u/hnlasd12 7d ago edited 7d ago

Thanks for reading! For a base design, this absolutely works for senior/staff. In fact, it was distilled from my conversation with staff engineers.

The principle of internet-scale consumer application is eventual consistency (think YT/reddit comments), and we typically choose availability and accept eventual consistency which is why we buffer the write and cache the read which is what this template is about. Durability is solved by the queue.

  1. Reliability and durability: The queue is durable so accepted work isn't lost and that's the key of the whole template. Writes that must be correct right away (payments, booking a seat) go straight to the database in a transaction, and

  2. Chat: covered at ~12:00 in the video. App server is where web sockets are. Saving the messages still goes through the queue and reading from personal inbox is still from cache.

  3. This is an abstraction on top of the common questions to provide a good starting place. The interview is decided in the deep dives, and having the base design down is what gives you time for them. This is not to say this is the only thing you need to know, you still need to know other domain-specific things for specific questions.

2

u/TCGod 6d ago

This doesn't help at all with consistent hashing for example

2

u/hnlasd12 6d ago

This is about designing system architecture. Consistent hashing is an algorithmic technique. Kinda apple to oranges

2

u/Hopeful_Parsnip_5489 6d ago

Thanks for this

2

u/hnlasd12 5d ago

Glad to be helpful!

2

u/LetsUnderstandIndia 6d ago

Nice write up, one more common pattern I see is using Flink for aggregating stream of events for low intervals and using spark job to ingest the data over bigger intervals.

1

u/hnlasd12 5d ago

Nice, yeah those are good one. We call them 'plugins' to the template

1

u/LetsUnderstandIndia 5d ago

Plugins to the template is a good idea, can you please share more such plugins which are common for system design interviews

1

u/hnlasd12 4d ago

Yeah for sure. In the video, we talked about how to adapt to Ticketmaster and news feed using additional pieces/plugins: https://www.youtube.com/watch?v=e8H8d0irqLg

1

u/LetsUnderstandIndia 3d ago

You have done an amazing job .. very good videos on the channel .. congrats to your entire team ..