System design: recommended articles for a newspaper
I remembered this challenge I was given, and I failed it miserably. My problem during the interview was that I failed to figure out the real challenge. I was given a full newspaper page to build from scratch, including premium, free and anonymous users.
I got bugged by the user management part and failed to see that the real challenge was the recommendation system which the interviewers wanted me to look after.
Unsurprisingly, I failed this interview: the second the call ended, I knew I had not handled it properly. I decided to redo it with fresh eyes, without the interview pressure, and knowing what the real challenge was.
Problem
Build a full website that recommends articles to users based on their preferences. The system needs to handle premium, non-premium and anonymous users.
The goal is to focus on the reader-facing part of the website; in a second part, we can talk more about article management.
Design
User flow
This one had such a broad scope that I failed to see which problem really needed solving. This time, I would go quickly over user management: it is a classic login service, nothing special to design. The only uncommon part is handling anonymous users.
flowchart LR
subgraph browser [Browser]
frontend[User]
end
subgraph userapi [User Management]
server[IAM server]
db[(User DB)]
end
server -->|set visitor_id cookie| frontend
frontend -->|login with email, password| server
server -->|returned jwt| frontend
frontend -->|logout| server
server --- db
The login is the classic one: email and password, returning a short-lived JWT (with refresh tokens) which is sent with every later request. Signup, password reset, SSO or logout are standard flows I would not detail here.
User table:
- id (uuid, easier to shard later)
- email (indexed for login)
- password (hashed)
- is_premium (boolean)
The uncommon part is anonymous users. I would create a visitor table, one entry per browser, linked to a user once they log in:
Visitor table:
- id (uuid) - a unique ID generated by the server and stored in a cookie
- user_id (FK to user, nullable, set during login)
- last_seen_at
- created_at
The visitor_id is what lets us track the interests of anonymous visitors, and keep them once they log in. On logout, the browser stays linked to its visitor entry.
Article flow
Now we have all the information we need related to the user. Let's build the recommendation part. After getting the visitor_id, we can request the list of articles to show.
The frontend would always send the JWT token with every request to the article list endpoint on the backend. So the simple system would look like this:
flowchart LR
subgraph browser [Browser]
frontend[User]
end
subgraph articleapi [Article Management]
server[Article server]
db[(Articles DB)]
end
server -->|return list of articles - with pagination| frontend
frontend -->|Get list of articles, Header with JWT and/or visitor_id| server
server --- db
At this stage, there is no recommendation related to the user. We now want to introduce an additional call to the IAM/User service to get the known user information (premium/non-premium/anonymous).
flowchart LR
subgraph browser [Browser]
frontend[User]
end
subgraph articleapi [Article Management]
server[Article server]
db[(Articles DB)]
end
subgraph userapi [User Management]
serveruser[User server]
dbuser[(User DB)]
end
server -->|return list of articles - with pagination| frontend
frontend -->|Get list of articles, Header with JWT and visitor_id| server
server -->|Get current visitor information from JWT and/or visitor_id| serveruser
server --- db
serveruser --- dbuser
The response of the User server should look something like this:
{
"visitor_id": "",
"user_id":"",
"is_premium": true
}
It would allow the article recommendations to filter content based on premium/anonymous content. For privacy reasons, the email is not necessary in this context, so it should be removed from the response.
As this call is made on every request, there might be a few issues: it is going to be called a lot, and the user service may get overloaded. In this case, I see two solutions, each with their ups and downs:
| solution | pro | con |
|---|---|---|
| carrying user id and is_premium in JWT | no call to User service at all | information can be stale until the token is refreshed |
| caching the user information | drastically reduces repeated calls to the User service and no stale information (only during the short time TTL) | might still overload the User service |
For this context, I believe the second solution is better. The first solution would require invalidating JWTs. For the caching, we need to find the right TTL based on the usage and the load.
It is now easy to filter articles based on premium, non-premium and anonymous users. We can have some basic article schema looking like:
Article DB:
- id
- title
- content
- is_premium
- created_at
- created_by
- updated_at
- updated_by
Recommendation of articles based on preferences
Now the hard part: how to recommend articles based on user preferences. This raises a sub-question: how are the user preferences determined? Probably by reading interests.
The easy one: a first-time visitor will get a fallback list ordered by date and most read articles for example.
Personalisation requires us to store the reading information. Every time a user clicks on an article to read it, it triggers an API call to log that information. I will keep that simple, time spent on an article, time spent on the overview page on a specific article overview, etc. All of this information could lead to some rules and much more accurate suggestions.
So I would create a new table for user reads:
Read table:
- visitor_id
- user_id (nullable)
- article_id (FK)
- read_at
flowchart LR
subgraph browser [Browser]
frontend[User]
end
subgraph articleapi [Article Management]
server[Article server]
db[(Articles DB)]
end
frontend -->|log visitor read article| server
server --- db
The second part is to get the interests of visitors based on their reads. There are mostly two ways:
- The writer of an article can specify the tags related to it. This fully relies on human judgement, and the quality might vary depending on who does it.
- We can put in place an automatic tag suggestion of articles. We can use a
tagging servicewhich could tag article interests. The advantage is that it can be as precise as we want, can be done asynchronously and does not require manual effort. But it requires an AI system, and should probably have some human validation.
I am not making a choice here; I leave that open for a follow-up as it is a bit out of scope. Both solutions would require an article_interests table:
Article interests table:
- article_id (FK article table)
- tag (string)
I would make sure to add a unique constraint on article_id, tag to avoid duplication.
Now we can directly get a user's interests by combining the article_interests and read tables. We can even rank the interests based on the number of articles read per tag with a simple query.
Now we need to discuss the actual recommendation. We now know:
- whether the user is premium or not
- list of interests, ranked
We can now add a score to each article having the same tags. We combine relevance and date into one score:
interest(tag) = Σ over reads of articles with that tag: 0.5 ^ (days since read / H)
H is a half-life: a read from H days ago counts half as much as a read from today, one from 2H days ago a quarter, and so on. This parameter can be adjusted based on feedback.
We can use something like:
score(article) = Σ interest(tag) for the article's tags × 0.5 ^ (hours since published / P)
which sums the visitor's interest in each of the article's tags.
P is also a half-life, but a shorter one. The second part of the calculation is for the freshness of the content (to not have old articles overpowering new content). This parameter can be adjusted based on feedback.
Then we can sort the articles by the score.
This solution does not actually use complex vector recommendations, but this is something we can add if we also want to look for "similarity" without an exact match.
Feedback loop: to be able to fine-tune these parameters, we can put in place some A/B testing and count clicks and engagement based on those parameters. Results would help Product and Tech leads to make a decision on those parameters and on the ideal scoring.
Now the question is the scalability
The current system works fine, but what about the scale? The read insert/read can be slow at scale, especially during spikes. The required changes could be:
- add a pubsub topic for read event ingestion
- a worker pulls the read events and inserts them
- maintaining a pre-calculated interests profile for a visitor/user with an async cronjob or another worker into a fast read DB (something like redis).
Both the cronjob and the worker are valid. The worker consumes the same read events and keeps the interests up to date within seconds of a read. The cronjob is simpler, but the interests are only as fresh as its last run. The diagram below shows the worker version.
The scalable version would look like:
flowchart LR
subgraph browser [Browser]
frontend[User]
end
subgraph articleapi [Article Management]
server[Article server]
pubsub[Pubsub topic]
worker[Read worker]
worker2[Interests worker]
db[(Articles DB)]
redis[(Precalculated interests)]
end
frontend -->|log visitor read article| server
server -->|publish read event| pubsub
pubsub -->|consume| worker
pubsub -->|consume| worker2
worker -->|insert read| db
worker2 -->|update visitor interests| redis
db -.->|rebuild if cache is lost| worker2
frontend -->|get list of articles| server
server -->|get visitor interests| redis
server -->|get articles| db
As redis is a cache, it can be rebuilt from the read table in the background in case of issues.
The read path now does not need to read from the read table. It can use the redis data and rank articles based on the ranks of interests. We can also pre-compute lists per visitor, but only for returning visitors: a one-time visitor would not need it, so for them the list is computed on request.
Also something to write down: as the interests are updated in the background on every read, the ranking for the same visitor might change between two page loads, especially when paginating (page 2 could repeat or skip articles from page 1). So I would also suggest adding - the same way typesense or elasticsearch works - a sort token: the ranked list of article IDs is stored in redis under that token with a short TTL, and each page is read from that list. It keeps the same order across pages and avoids random orders polluting the experience.
Follow-up questions
How to handle breaking news
Breaking news would probably have a specific algorithm weight which would overpower everything else. It would require the article editor to flag an article as breaking news. We could have a new column in articles breaking_until to make sure the breaking does not stay stale forever.
As the lists per visitor are pre-computed and stored behind a sort token, a breaking news article would bypass them and be added on top of the list on every request. Otherwise visitors would only see it once their list is computed again.
How to handle live events
A live event is another type of article. Usually how that works on Le Monde for example, it is a classic article, with a "chat like" widget at the center. Editors add content on a regular basis, users are notified of new content or it's loaded automatically.
Here I would approach that "live widget" the same way as a one-direction chat. The question is how fresh the data needs to be. We have 2 solutions here:
- a recurring frontend pull as long as we are on the page. It checks for new content on the live event.
- an SSE (Server-Sent Events) connection can send live events to the frontend.
The second solution would help get the content as fast as possible. A lightweight "seen" event log can be done. It requires some connection handling, including lost connections. Events with millions of connections at the same time might require a lot of work.
The first one would just regularly pull the new content that has not been loaded to the frontend. The data flow is uni-directional, the content can be cached on a CDN, and loaded for everyone at the same time.
On the backend side, we could have a pubsub topic notifying the server of new content in the live article, which would then:
- send the content via SSE to every connected user
- store a small cache of the content to be pulled later and avoid heavy reads from our regular pull. A redis linking article_id and content.
It depends on the requirements. The pull with a CDN cache makes sense when:
- a delay of 10-30 seconds is acceptable. Editors add content every few minutes, so nobody would notice it.
- the audience is huge and comes in spikes. The CDN takes the spike, not our backend.
- the content is the same for everyone, which is what makes the CDN cache useful.
The SSE makes more sense when:
- every second matters (sports scores, election results, etc.).
- the content is personalised (e.g. "a topic you follow just published"). A CDN cannot cache it, so the pull loses its main advantage.
- we already have a push infrastructure, or use a managed service, so millions of open connections are not our problem.
A websocket would only make sense if the users also send content back (comments, reactions, chat between readers).
As a pragmatic choice, I would start with the pull and a CDN cache. An endpoint returns the latest entries, cached for ~10 seconds, and the frontend ignores the entries it already has. The frontend can also pull less often when the tab is hidden. The redis cache sits behind the CDN, so even a cache miss is cheap.
The main reason is that a live event is also our biggest traffic spike. With the pull, the spike lands on the CDN. With SSE, it lands on our connection servers, at the moment they are the most likely to fail. A newspaper live blog does not need real-time either, so we would solve a problem we do not have.
If a real-time need appears later, we can mix both: a small SSE message ("new content for article X") tells the frontend to pull, and the content still comes from the CDN.
Conclusion
Looking back, the challenge was not that hard. The real problem was hidden in "based on their preferences", and I spent my time on the part I knew best, a classic login service. Next time, I would ask early where the interviewers want to go deeper, go quickly over the standard parts, and spend the time on the hard one.
Redoing it without the interview pressure, and brainstorming it, also brought solutions I was not looking at before: SSE instead of a websocket, a pull cached on a CDN, a sort token to keep the pagination stable. A system design interview is less about knowing the answer than thinking out loud with someone, which is closer to how we actually design systems at work.
There are still some open questions I would like to explore:
- what happens when a part fails (falling back to the "most read" list)
- privacy and consent around the reading history
- avoiding a filter bubble, keeping some editorial choices in the list
- manual or automatic tagging of the articles