reddit s architecture
play

Reddits Architecture And how its broken over the years Neil - PowerPoint PPT Presentation

Reddits Architecture And how its broken over the years Neil Williams QCon SF, 13 November 2017 What is Reddit? Reddit is the frontpage of the internet A social network where there are tens of thousands of communities around whatever


  1. Reddit’s Architecture And how it’s broken over the years Neil Williams QCon SF, 13 November 2017

  2. What is Reddit? Reddit is the frontpage of the internet A social network where there are tens of thousands of communities around whatever passions or interests you might have It’s where people converse about the things that are most important to them

  3. Reddit by the numbers 4th/7th Alexa Rank (US/World) 320M MAU 1.1M Communities 1M Posts per day 5M Comments day 75M Votes per day 70M Searches per Day

  4. Major components The stack that serves reddit.com. CDN Focusing on just the core experience. Frontend API r2 Search Listing Thing Rec.

  5. Major components A work in progress. CDN This tells you as much about the Frontend API organization as it does our tech. r2 Search Listing Thing Rec.

  6. r2: The monolith The oldest single component of CDN Reddit, started in 2008, and written in Python. Frontend API r2 Search Listing Thing Rec.

  7. Node.js frontend applications Modern frontends using shared CDN server/client code. Frontend API r2 Search Listing Thing Rec.

  8. New backend services Written in Python. CDN Splitting off from r2. Frontend API Common library/framework to standardize. r2 Thrift or HTTP depending on clients. Search Listing Thing Rec.

  9. CDN Send requests to distinct stacks CDN depending on domain, path, cookies, etc. Frontend API r2 Search Listing Thing Rec.

  10. CDN r2 Deep Dive Frontend API The original Reddit. r2 Load Balancers Much more complex than any of the other components. App App App Job Cassandra PostgreSQL Listing Search Thing Rec.

  11. CDN r2: Monolith Frontend API Monolithic Python application. r2 Load Balancers Same code deployed to all servers, but servers used for different tasks. App App App Job Cassandra PostgreSQL Listing Search Thing Rec.

  12. CDN r2: Load Balancers Frontend API Load balancers route requests to r2 Load Balancers distinct “pools” of otherwise identical servers. App App App Job Cassandra PostgreSQL Listing Search Thing Rec.

  13. CDN r2: Job Queues Frontend API Many requests trigger asynchronous r2 Load Balancers jobs that are handled in dedicated processes. App App App Job Cassandra PostgreSQL Listing Search Thing Rec.

  14. CDN r2: Things Frontend API Many core data types are stored in the r2 Load Balancers Thing data model. This uses PostgreSQL for persistence App App App and memcached for read performance. Job Cassandra PostgreSQL Listing Search Thing Rec.

  15. CDN r2: Cassandra Frontend API Apache Cassandra is used for most r2 Load Balancers newer features and data with heavy write rates. App App App Job Cassandra PostgreSQL Listing Search Thing Rec.

  16. Listings

  17. Listings The foundation of Reddit: an ordered list of links. Naively computed with a SQL query: SELECT * FROM links ORDER BY hot(ups, downs);

  18. Cached Results Rather than querying every time, we r/rarepuppers, sort by hot cache the list of Link IDs. Just run the query and cache the [123, 124, 125, … ] results. Invalidate cache on new submissions and votes.

  19. Cached Results Easy to look up the links by ID once r/rarepuppers, sort by hot listing is fetched. [123, 124, 125, … ] Link #123: title=doggo Link #124: title=pupper does a nap

  20. CDN Vote Queues Frontend API Votes invalidate a lot of cached r2 Load Balancers queries. Also have to do expensive anti-cheat App App App processing. Job Deferred to offline job queues with many processors. Cassandra PostgreSQL Listing Search Thing Rec.

  21. Mutate in place Eventually, even running the query is [(123, 10), (124, 8), (125, 8), … ] too slow for how quickly things change. Link #125 Add sort info to cache and modify the cached results in place. [(123, 10), (125, 9), (124, 8), … ] Locking required.

  22. CDN “Cache” Frontend API This isn’t really a cache anymore: r2 Load Balancers really a denormalized index of links. Data is persisted to Cassandra, reads App App App are still served from memcached. Job Cassandra PostgreSQL Listing Search Thing Rec.

  23. Vote queue pileups Mid 2012 We started seeing vote queues fill up at peak traffic hours. A given vote would wait in queue for hours before being processed. Delayed effects on site noticeable by users. https://commons.wikimedia.org/wiki/File:Miami_traffic_jam,_I-95_North_rush_hour.jpg

  24. Scale out? Adding more consumer processes made the problem worse .

  25. Observability Basic metrics showed average processing time of votes way higher. No way to dig into anything more granular.

  26. Lock contention Added timers to various portions of r/news, sort by hot vote processing. Time spent waiting for the cached query mutation locks was much higher during these pileups. Vote Vote Vote Vote Processor Processor Processor Processor

  27. Partitioning Put votes into different queues based on the subreddit of the link being r/news r/funny r/science r/programming voted on. Fewer processors vying for same locks concurrently. r/news r/funny r/science r/programming

  28. Smooth sailing!

  29. Slow again Late 2012 This time, the average lock contention and processing times look fine.

  30. p99 The answer was in the 99th percentile timers. A subset of votes were performing very poorly. Added print statements to get to the bottom of it.

  31. An outlier Vote processing updated all affected listings. domain:imgur.com, sort by hot This includes ones not related to subreddit, like the domain of the submitted link. A very popular domain was being Vote Vote Vote Vote submitted in many subreddits! Processor Processor Processor Processor (partition 1) (partition 2) (partition 3) (partition 4)

  32. Split up processing Link #125 Handle different types of queries in different queues so they never work cross-partition. Subreddit queries Anti-cheating Domain queries Profile queries

  33. Learnings Timers give you a cross section. p99 shows you problem cases. Have a way to dig into those exceptional cases.

  34. Learnings Locks are bad news for throughput. But if you must, use the right partitioning to reduce contention.

  35. Lockless cached queries New data model we’re trying out which allows mutations without locking. More testing needed.

  36. The future of listings Listing service: extract the basics CDN and rethink how we make listings. Frontend API Use machine learning and offline analysis to build up more personalized listings. r2 Search Listing Thing Rec.

  37. Things

  38. CDN Thing Frontend API r2’s oldest data model. r2 Load Balancers Stores data in PostgreSQL with heavy caching in memcached. App App App Designed to allow extension within a Job safety net. Cassandra PostgreSQL Listing Search Thing Rec.

  39. Tables One Thing type per “noun” on the site. Each Thing type is represented by a pair of tables in PostgreSQL.

  40. Thing Each row in the thing table represents reddit_thing_link one Thing instance. The columns in the thing table are id | ups | downs | deleted everything needed for sorting and ---+-----+-------+-------- filtering in early Reddit. 1 | 1 | 0 | f 2 | 99 | 10 | t 3 | 345 | 3 | f

  41. Thing Many rows in the data table will reddit_data_link correspond to a single instance of a Thing. thing_id | key | value These make up a key/value bag of ---------+-------+-------- properties of the thing. 1 | title | DAE think 1 | url | http://... 2 | title | Cat 2 | url | http://... 3 | title | Dog! 3 | url | http://...

  42. Thing in PostgreSQL Each Thing lives in a database cluster. Primary Primary that handles writes. A number of read-only replicas. Asynchronous replication. Read Replicas

  43. Thing in PostgreSQL r2 connects directly to databases. Primary Use replicas to handle reads. If a database seemed down, remove it r2 from connection pool. Read Replicas

  44. Thing in memcached Whole Thing objects serialized and Primary added to memcached. r2 reads from memcached first and r2 only hits PostgreSQL on cache miss. Read Replicas r2 writes changes directly to memcached at same time it does to PostgreSQL.

  45. Incident 2011 Primary Alerts indicating replication has crashed on a replica. r2 It is getting more out of date as time Read Replicas goes on.

  46. Incident Immediate response is to remove Primary broken replica and rebuild. Diminished capacity, but no direct r2 impact on users. Read Replicas

  47. Incident r/example hot links: #1, #2, #3, #4 Afterwards, we see references left around to things that don’t exist in the database. reddit_thing_link This causes the page to crash since it can’t find all the necessary data. id | ups | downs | deleted ---+-----+-------+-------- 1 | 1 | 0 | f 2 | 99 | 10 | t 4 | 345 | 3 | f

  48. Incident The issue always starts with a primary Primary saturating its disks. r2 Read Replicas

  49. Incident The issue always starts with a primary Primary saturating its disks. Upgrade the hardware! r2 Read Replicas

  50. How unsatisfying...

Recommend


More recommend