Aggregating DynamoDB data in realtime to list restaurants at Deliveroo

Watch the video explanation ➔

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.

Here's the video ⤵

Courses I teach

Alongside my daily work, I also teach some highly practical courses, with a no-fluff no-nonsense approach, that are designed to spark engineering curiosity and help you ace your career.

System Design Masterclass

A no-fluff masterclass that helps experienced engineers form the right intuition to design and implement highly scalable, fault-tolerant, extensible, and available systems.

Details →

System Design for Beginners

An in-depth and self-paced course for absolute beginners to become great at designing and implementing scalable, available, and extensible systems.

Details →

Redis Internals

A self-paced and hands-on course covering Redis internals - data structures, algorithms, and some core features by re-implementing them in Go.

Details →

Writings and Learnings

Knowledge Base



Arpit's Newsletter read by 90000+ engineers

Weekly essays on real-world system design, distributed systems, or a deep dive into some super-clever algorithm.