<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: ARUNABH PRIYADARSHI</title>
    <description>The latest articles on DEV Community by ARUNABH PRIYADARSHI (@arunabh_priyadarshi_bfb98).</description>
    <link>https://dev.to/arunabh_priyadarshi_bfb98</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2169796%2Faf2800f9-c367-474e-aa05-1196c5c461c1.jpg</url>
      <title>DEV Community: ARUNABH PRIYADARSHI</title>
      <link>https://dev.to/arunabh_priyadarshi_bfb98</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/arunabh_priyadarshi_bfb98"/>
    <language>en</language>
    <item>
      <title>Single-table DynamoDB design for a real-time game (rooms, rounds, leaderboards)</title>
      <dc:creator>ARUNABH PRIYADARSHI</dc:creator>
      <pubDate>Mon, 29 Jun 2026 20:20:33 +0000</pubDate>
      <link>https://dev.to/arunabh_priyadarshi_bfb98/single-table-dynamodb-design-for-a-real-time-game-rooms-rounds-leaderboards-1j4l</link>
      <guid>https://dev.to/arunabh_priyadarshi_bfb98/single-table-dynamodb-design-for-a-real-time-game-rooms-rounds-leaderboards-1j4l</guid>
      <description>&lt;p&gt;For the H0 Hackathon I built &lt;strong&gt;&lt;a href="https://kauwa-udd.vercel.app" rel="noopener noreferrer"&gt;Kauwa Udd&lt;/a&gt;&lt;/strong&gt;, a real-time multiplayer reaction game on Vercel + &lt;strong&gt;Amazon DynamoDB&lt;/strong&gt;. People hear "DynamoDB" and reach for a table per entity — users table, rooms table, scores table. I put &lt;em&gt;everything&lt;/em&gt; in &lt;strong&gt;one table&lt;/strong&gt;. Here's the design, why it works for a live game, and the exact key patterns.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why single-table?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Fewer round-trips:&lt;/strong&gt; related items share a partition, so one &lt;code&gt;Query&lt;/code&gt; fetches a room &lt;em&gt;and&lt;/em&gt; all its players.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;One thing to provision + secure:&lt;/strong&gt; my Vercel OIDC IAM role is scoped to a single table ARN (+ its indexes). Less surface area.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;It fits the access patterns&lt;/strong&gt; — and in DynamoDB you design for access patterns, not entities.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The trade-off is upfront key modeling. For a game with a handful of well-known access patterns, that's a feature, not a chore.&lt;/p&gt;

&lt;h2&gt;
  
  
  The keys
&lt;/h2&gt;

&lt;p&gt;Two attributes drive the base table: &lt;code&gt;PK&lt;/code&gt; (partition) and &lt;code&gt;SK&lt;/code&gt; (sort). One GSI — &lt;strong&gt;&lt;code&gt;GSI1&lt;/code&gt;&lt;/strong&gt; (&lt;code&gt;GSI1PK&lt;/code&gt; / &lt;code&gt;GSI1SK&lt;/code&gt;) — powers leaderboards.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Entity&lt;/th&gt;
&lt;th&gt;PK&lt;/th&gt;
&lt;th&gt;SK&lt;/th&gt;
&lt;th&gt;GSI1PK / GSI1SK&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Content object&lt;/td&gt;
&lt;td&gt;&lt;code&gt;OBJ#&amp;lt;id&amp;gt;&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;META&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;—&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Room&lt;/td&gt;
&lt;td&gt;&lt;code&gt;ROOM#&amp;lt;code&amp;gt;&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;META&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;—&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Player in room&lt;/td&gt;
&lt;td&gt;&lt;code&gt;ROOM#&amp;lt;code&amp;gt;&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;PLAYER#&amp;lt;playerId&amp;gt;&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;—&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Round&lt;/td&gt;
&lt;td&gt;&lt;code&gt;ROOM#&amp;lt;code&amp;gt;&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;ROUND#&amp;lt;n&amp;gt;&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;—&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Reaction (click)&lt;/td&gt;
&lt;td&gt;&lt;code&gt;ROOM#&amp;lt;code&amp;gt;&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;CLICK#&amp;lt;n&amp;gt;#&amp;lt;playerId&amp;gt;&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;—&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;User stats&lt;/td&gt;
&lt;td&gt;&lt;code&gt;USER#&amp;lt;id&amp;gt;&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;STATS&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;—&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Leaderboard entry&lt;/td&gt;
&lt;td&gt;&lt;code&gt;LB#&amp;lt;period&amp;gt;#&amp;lt;scope&amp;gt;&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;USER#&amp;lt;id&amp;gt;&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;LB#&amp;lt;period&amp;gt;#&amp;lt;scope&amp;gt;&lt;/code&gt; / &lt;code&gt;&amp;lt;score&amp;gt;&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The pattern: a &lt;strong&gt;room is a partition&lt;/strong&gt;. Its META, every player, every round, and every reaction live under &lt;code&gt;ROOM#&amp;lt;code&amp;gt;&lt;/code&gt; — so the whole live game state is one partition, queryable in a single call.&lt;/p&gt;

&lt;h2&gt;
  
  
  Access patterns → queries
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Get a room + its players&lt;/strong&gt; (lobby render):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;ddb&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;QueryCommand&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;TableName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;TABLE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;KeyConditionExpression&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;PK = :pk&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;ExpressionAttributeValues&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;:pk&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;`ROOM#&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;code&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;}))&lt;/span&gt;
&lt;span class="c1"&gt;// one round-trip → META + all PLAYER# + ROUND# items&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Collect a round's reactions&lt;/strong&gt; (the game loop reads these at the 3-second boundary):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;ddb&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;QueryCommand&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;TableName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;TABLE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;KeyConditionExpression&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;PK = :pk AND begins_with(SK, :sk)&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;ExpressionAttributeValues&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;:pk&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;`ROOM#&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;code&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;:sk&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;`CLICK#&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;round&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;#`&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;}))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;begins_with&lt;/code&gt; on the sort key is the workhorse here — "all players' clicks for round N" is just a prefix.&lt;/p&gt;

&lt;h2&gt;
  
  
  Leaderboards with one GSI
&lt;/h2&gt;

&lt;p&gt;DynamoDB has no &lt;code&gt;ORDER BY&lt;/code&gt;, so top-N comes from a GSI whose &lt;strong&gt;sort key is the score&lt;/strong&gt;. Each leaderboard bucket (&lt;code&gt;LB#daily#global&lt;/code&gt;, &lt;code&gt;LB#all#room#&amp;lt;code&amp;gt;&lt;/code&gt;, …) is a GSI partition; query it descending, limit N:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;ddb&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;QueryCommand&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;TableName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;TABLE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;IndexName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;GSI1&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;KeyConditionExpression&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;GSI1PK = :pk&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;ExpressionAttributeValues&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;:pk&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;`LB#&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;period&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;#&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;scope&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="na"&gt;ScanIndexForward&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;   &lt;span class="c1"&gt;// highest score first&lt;/span&gt;
  &lt;span class="na"&gt;Limit&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I store the score zero-padded (or as a numeric GSI sort key) so lexical order matches numeric order. The &lt;em&gt;same&lt;/em&gt; GSI serves global daily/weekly boards &lt;strong&gt;and&lt;/strong&gt; per-room boards just by changing the partition value — including the public per-room leaderboard you can share after a match.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Honest caveat: top-N is easy; &lt;strong&gt;"your exact rank among millions"&lt;/strong&gt; is the one thing single-table DynamoDB doesn't do cheaply (you'd add a Redis sorted set or, in my roadmap, Aurora DSQL). For top-N + your-own-best, the GSI is perfect.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  TTL for the ephemeral stuff
&lt;/h2&gt;

&lt;p&gt;Rooms, rounds, and clicks are throwaway. Each carries a &lt;code&gt;ttl&lt;/code&gt; (epoch seconds) and DynamoDB sweeps them automatically — no cleanup jobs, no cron. Durable stuff (user stats, leaderboards) just omits &lt;code&gt;ttl&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Auth in the same table
&lt;/h2&gt;

&lt;p&gt;Even Auth.js sessions live here via the DynamoDB adapter (configured to the same &lt;code&gt;PK&lt;/code&gt;/&lt;code&gt;SK&lt;/code&gt; + &lt;code&gt;GSI1&lt;/code&gt;). Auth item keys (&lt;code&gt;USER#…&lt;/code&gt;, &lt;code&gt;ACCOUNT#…&lt;/code&gt;) don't collide with game keys (&lt;code&gt;ROOM#…&lt;/code&gt;, &lt;code&gt;OBJ#…&lt;/code&gt;, &lt;code&gt;LB#…&lt;/code&gt;), so one table holds the whole app.&lt;/p&gt;

&lt;h2&gt;
  
  
  Takeaways
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Model &lt;strong&gt;partitions around your access unit&lt;/strong&gt; (here, the room) → live state in one query.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;begins_with(SK, …)&lt;/code&gt;&lt;/strong&gt; turns "all the X for Y" into a prefix scan within a partition.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;One GSI with score as the sort key&lt;/strong&gt; covers every leaderboard variant.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;TTL&lt;/strong&gt; makes ephemeral game data self-cleaning.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;▶️ &lt;strong&gt;Play it:&lt;/strong&gt; &lt;a href="https://kauwa-udd.vercel.app" rel="noopener noreferrer"&gt;https://kauwa-udd.vercel.app&lt;/a&gt;&lt;br&gt;
💻 &lt;strong&gt;Code:&lt;/strong&gt; &lt;a href="https://github.com/nextjedi/kauwa-udd" rel="noopener noreferrer"&gt;https://github.com/nextjedi/kauwa-udd&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;#H0Hackathon #DynamoDB #AWS #Vercel #serverless #webdev&lt;/em&gt;&lt;/p&gt;

</description>
      <category>architecture</category>
      <category>aws</category>
      <category>database</category>
      <category>gamedev</category>
    </item>
  </channel>
</rss>
