Aggregating DynamoDB data in realtime to list restaurants at Deliveroo

1441 views Backend System Design

DynamoDB does not support aggregation queries, but we need it for a use case; let’s build a real-time DDB aggregation today…

Deliveroo, a food delivery startup had a similar problem. On their app, people can mark a restaurant as “favorite” and now they wanted to render restaurants ordered by most favorite first.

Data Model

We have a favorites table in which we store users and their favorite restaurants. The table has restaurant_id_user_id as their primary key and created_at, and user_id, as other attributes.

With the above data model, getting if a user marked a restaurant as a favorite is an O(1) lookup and so are the marking and unmarking activites.

Getting restaurants ordered by favorite count

With this data model, it becomes near impossible to get restaurants ordered by their favorite count, purely because DynamoDB does not support aggregations.

Core idea

Maintain a separate table having aggregated favorite count as one of the attributes and use it to get tables ordered by favorite count.

Data Model

Introducing a new table aggregated_favourites having the following schema

  • rastaurant_id as the primary key
  • time_window as the sort key
  • favourite_count, updated_at as other attributes.

Data Flow

We set up a DynamoDB stream that would contain all the events happening on the favorites table. This stream will be consumed by an AWS lambda function.

The lambda function will transactional do count++ upon every creation and count-- on deletion.

This way, we maintain the aggregated favorite count for each restaurant in near-realtime without doing any fancy code changes.


  • extremely cost coefficient for Deliveroo scale
  • count updation is asynchronous, hence API response time unaffected
  • better than running a daily cron job or doing a sync write to the aggregated table.

Arpit Bhayani

Arpit's Newsletter

CS newsletter for the curious engineers

❤️ by 21000+ readers

If you like what you read subscribe you can always subscribe to my newsletter and get the post delivered straight to your inbox. I write essays on various engineering topics and share it through my weekly newsletter.

Other essays that you might like

Overview of Discord's data platform that daily processes petabytes of data and trillion points

924 views 54 likes 2022-11-14

When a company scales, they adopt microservices and each service typically gets its own independent database. With data ...

How Airbnb designed and scaled its central authorization system - Himeji

2206 views 98 likes 2022-11-07

Authorization plays a critical role in ensuring that the platform is not abused. For example, Instagram ensures that if ...

How Gojek masks and keeps users' phone numbers secure at scale?

2572 views 152 likes 2022-10-31

Do hyperlocal companies like Uber, Ola, Swiggy, Gojek, Zomato, etc share our phone numbers with the delivery people or t...

The architecture of Yelp's in-house Search Engine - nrtSearch

2193 views 81 likes 2022-10-24

Elasticsearch is a great search engine, but Yelp was not happy with its performance, so they built their own HTTP layer ...

Be a better engineer

A set of courses designed to make you a better engineer and excel at your career; no-fluff, pure engineering.

Paid Courses

System Design Masterclass

A masterclass that helps you become great at designing scalable, fault-tolerant, and highly available systems.

1000+ learners

Details →

Redis Internals

Learn internals of Redis by re-implementing some of the core features in Golang.

28+ learners

Details →

Free Courses

Designing Microservices

A free playlist to help you understand Microservices and their high-level patterns in depth.

17+ learners

Details →

GitHub Outage Dissections

A free playlist to help you learn core engineering from outages that happened at GitHub.

67+ learners

Details →

Hash Table Internals

A free playlist to help you understand the internal workings and construction of Hash Tables.

25+ learners

Details →

BitTorrent Internals

A free playlist to help you understand the algorithms and strategies that power P2P networks and BitTorrent.

42+ learners

Details →

Topics I talk about

Being a passionate engineer, I love to talk about a wide range of topics, but these are my personal favourites.

  • v13.7.5
  • © Arpit Bhayani, 2022

Powered by this tech stack.