<?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: Eduard Grigoryan</title>
    <description>The latest articles on DEV Community by Eduard Grigoryan (@ed960).</description>
    <link>https://dev.to/ed960</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%2F4246216%2F35560c4d-65bd-4700-ae0f-01ac5fc81d38.jpg</url>
      <title>DEV Community: Eduard Grigoryan</title>
      <link>https://dev.to/ed960</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ed960"/>
    <language>en</language>
    <item>
      <title>A Telegram dating bot for Armenian students: a feed over 105,000 profiles in 18 ms on a single VPS</title>
      <dc:creator>Eduard Grigoryan</dc:creator>
      <pubDate>Sun, 15 Nov 2026 16:09:29 +0000</pubDate>
      <link>https://dev.to/ed960/a-telegram-dating-bot-for-armenian-students-a-feed-over-105000-profiles-in-18-ms-on-a-single-vps-1b3h</link>
      <guid>https://dev.to/ed960/a-telegram-dating-bot-for-armenian-students-a-feed-over-105000-profiles-in-18-ms-on-a-single-vps-1b3h</guid>
      <description>&lt;p&gt;How do you make the feed of a Telegram dating bot respond in milliseconds with 105,000 profiles on one inexpensive VPS? We walk through it on our own project — a bot for university students in Armenia (Մտերմիկ, Mtermik) built with NestJS + Telegraf, PostgreSQL, Redis and Next.js: denormalization via a trigger instead of a JOIN, a lock order that prevents deadlocks, a Redis the bot can live without, and why our scaling limit turned out to be Postgres memory rather than CPU. We found all of this with load tests before launch, not on real users.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why another dating bot
&lt;/h2&gt;

&lt;p&gt;Almost every student in Armenia is on Telegram, while the big dating apps have no idea which profiles actually belong to people studying nearby. The idea is simple: only students in the feed, and every one of them verified — with a university email or a photo of a student ID. A moderator reviews each profile before it is shown.&lt;/p&gt;

&lt;p&gt;That led to requirements that ended up shaping almost the whole architecture:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;trust&lt;/strong&gt; — one student email verifies exactly one account, contact details are not allowed in profile text, and there are reports, blocking and "Hide from acquaintances";&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;speed&lt;/strong&gt; — the feed must answer instantly even with tens of thousands of profiles, on a single inexpensive VPS;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;three languages&lt;/strong&gt; — Armenian, Russian and English: the language is chosen on the first &lt;code&gt;/start&lt;/code&gt;, and notifications arrive in it too.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Stack
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Part&lt;/th&gt;
&lt;th&gt;Technologies&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Bot and API&lt;/td&gt;
&lt;td&gt;NestJS, Telegraf&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Database&lt;/td&gt;
&lt;td&gt;PostgreSQL via Prisma&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Cache and state&lt;/td&gt;
&lt;td&gt;Redis (notification queues, card cache, Lua scripts)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Website, admin panel, Mini App&lt;/td&gt;
&lt;td&gt;Next.js, Feature-Sliced Design&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Monitoring&lt;/td&gt;
&lt;td&gt;Netdata on the VPS, &lt;code&gt;/api/health&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The bot and the Mini App share one domain layer: a swipe from the chat and a swipe from the app go through the same service, so the rules for the feed, likes and matches are written once.&lt;/p&gt;

&lt;h2&gt;
  
  
  Layers enforced by the linter
&lt;/h2&gt;

&lt;p&gt;The backend is split into layers, and dependencies only point downwards:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;core/        infrastructure: prisma, redis, mail, metrics
i18n/, data/ texts and reference data
domain/      product rules
telegram/, api/, inactivity/ ...  entry points and delivery channels
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The domain knows nothing about Telegram: it sends messages through a &lt;code&gt;BotSender&lt;/code&gt; abstraction, and importing &lt;code&gt;telegraf&lt;/code&gt; inside &lt;code&gt;domain/&lt;/code&gt; is forbidden by the ESLint rule &lt;code&gt;no-restricted-syntax&lt;/code&gt;. Bot handlers and HTTP controllers cannot import &lt;code&gt;PrismaService&lt;/code&gt; — they reach the database only through domain services. One more rule is &lt;code&gt;max-lines&lt;/code&gt;: a file may not exceed 500 lines, and older large files are listed with a ceiling that can only go down.&lt;/p&gt;

&lt;p&gt;It sounds bureaucratic, but it paid off: the bot and the Mini App got shared swipe logic for free, because from day one it never lived inside a Telegram handler.&lt;/p&gt;

&lt;h2&gt;
  
  
  The feed: from 0.4 s to 18 ms
&lt;/h2&gt;

&lt;p&gt;The feed is the hottest path: every ❤️ or 👎 tap needs the next profile. Selection takes into account gender, age, universities from the filter, last activity, boosts, "Who can see my profile" and everyone the person has already swiped.&lt;/p&gt;

&lt;p&gt;The first version filtered on columns from two tables — &lt;code&gt;Profile&lt;/code&gt; and &lt;code&gt;User&lt;/code&gt; (last activity &lt;code&gt;lastActiveAt&lt;/code&gt; and boost expiry were stored on the user). We inflated the test database to ~105,000 profiles and saw that, because of the join, Postgres was reading every user in a row: &lt;strong&gt;filling the feed took 0.4 s&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The fix is denormalization, but without manual syncing in application code. The needed columns are copied from &lt;code&gt;User&lt;/code&gt; to &lt;code&gt;Profile&lt;/code&gt; &lt;strong&gt;by a database trigger in the same transaction&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="c1"&gt;-- simplified&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;FUNCTION&lt;/span&gt; &lt;span class="nf"&gt;user_copy_to_profile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;RETURNS&lt;/span&gt; &lt;span class="k"&gt;trigger&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; $$
&lt;span class="k"&gt;BEGIN&lt;/span&gt;
  &lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="s2"&gt;"Profile"&lt;/span&gt;
     &lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="s2"&gt;"lastActiveAt"&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;NEW&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="s2"&gt;"lastActiveAt"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
         &lt;span class="s2"&gt;"boostExpiresAt"&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;NEW&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="s2"&gt;"boostExpiresAt"&lt;/span&gt;
   &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="s2"&gt;"userId"&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;NEW&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;RETURN&lt;/span&gt; &lt;span class="no"&gt;NEW&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;END&lt;/span&gt; $$ &lt;span class="no"&gt;LANGUAGE&lt;/span&gt; &lt;span class="n"&gt;plpgsql&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Code only ever writes &lt;code&gt;User&lt;/code&gt;; nobody touches the copy by hand. The feed filters on a single table — &lt;strong&gt;18 ms on the same 105,000 profiles&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The second part is the swipes table, the largest in the database. We constrained queries against it so that Postgres answers them only with an &lt;strong&gt;index-only scan over partial indexes&lt;/strong&gt; (&lt;code&gt;swipe_*&lt;/code&gt;), and "already swiped" arrives in selection as a ready-made list of ids. Every new query on the hot path is checked at scale, not on 7,000 test profiles: on a small database a bad plan looks like a good one.&lt;/p&gt;

&lt;h2&gt;
  
  
  The card cache and the "hidden profile" trap
&lt;/h2&gt;

&lt;p&gt;A ready-to-render profile card is cached in Redis for an hour. When served from the cache, the feed returns it without checking "approved and not hidden" — that check only happens when reading from the database.&lt;/p&gt;

&lt;p&gt;So any action that removes a profile from the feed (hide, delete, revoke verification, a report) must reset the card cache after writing to the database. Otherwise a profile someone has just hidden would stay visible for another hour to everyone who already had it in their pool.&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="c1"&gt;// simplified: database first, then cache — never the other way round&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="nf"&gt;hideProfile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;prisma&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;profile&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;where&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;userId&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;hidden&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;feedPool&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;invalidateFeedProfile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// DEL feed_profile:&amp;lt;id&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The order matters: if you reset the cache before the write, a concurrent request can put the old version from the database back into it. We have dedicated e2e tests for this: an action's test waits for the cache reset, not just for the row to change.&lt;/p&gt;

&lt;h2&gt;
  
  
  Redis is a cache, not the source of truth
&lt;/h2&gt;

&lt;p&gt;Once Redis went down on our staging server, and &lt;code&gt;/feed&lt;/code&gt; stopped opening entirely: the client queued commands offline and waited. Now:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;enableOfflineQueue: false&lt;/code&gt; — when Redis is unavailable, a command fails immediately;&lt;/li&gt;
&lt;li&gt;every Redis command on the update path is wrapped in &lt;code&gt;try/catch&lt;/code&gt; with a fallback: the feed is built from Postgres, a tap marker can simply be skipped.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// simplified: the feed pool lives in Redis, but Redis is not the only path&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;ids&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;ids&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;readPool&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;viewerId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;signature&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;offset&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;logger&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;warn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sb"&gt;`Pool read error, reading feed from DB: ${err}`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;first&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;firstVisible&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;buildPool&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;viewerId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;viewer&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;first&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;first&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&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;Without Redis the answer is slower, but the person gets the next profile instead of a silent bot.&lt;/p&gt;

&lt;p&gt;This check is part of our audit: we stop Redis on staging and replay bot updates.&lt;/p&gt;

&lt;h2&gt;
  
  
  A deadlock caused by two buttons
&lt;/h2&gt;

&lt;p&gt;Super Like deducts Sparks (the in-app currency) in the same transaction that records the swipe. "Undo" (reverting the last swipe) does too. If one transaction locks the user first (balance) and then the pair in &lt;code&gt;Swipe&lt;/code&gt;, while the other does the reverse, pressing both at the same time gives you a deadlock.&lt;/p&gt;

&lt;p&gt;There is now one rule for the whole codebase: &lt;strong&gt;first the &lt;code&gt;Swipe&lt;/code&gt; pair row, then &lt;code&gt;User&lt;/code&gt;&lt;/strong&gt;. We create the pair row up front and lock it immediately — which also solves the "both liked at the same moment" race: the second transaction waits, sees the first one's decision, and the match notification goes out exactly once.&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="c1"&gt;// simplified&lt;/span&gt;
&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;prisma&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nv"&gt;$transaction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tx&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// 1. the pair row: create it if missing, then lock it&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;tx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;swipe&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createMany&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;user1Id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;u1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;user2Id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;u2&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;skipDuplicates&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;before&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;tx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nv"&gt;$queryRaw&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;LockedSwipe&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="sb"&gt;`
    SELECT "decision1", "decision2", "isSuper1", "isSuper2"
      FROM "Swipe"
     WHERE "user1Id" = ${u1} AND "user2Id" = ${u2}
       FOR UPDATE`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="c1"&gt;// 2. only then the user's balance&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;tx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;where&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;me&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sparks&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;gte&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;SUPER_LIKE_PRICE&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;sparks&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;decrement&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;SUPER_LIKE_PRICE&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&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;// ... record the decision and check whether this became a match&lt;/span&gt;
&lt;span class="p"&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;Any two actions on the same rows get a concurrency test against a real Postgres, not a mock.&lt;/p&gt;

&lt;h2&gt;
  
  
  The scaling limit is memory, not CPU
&lt;/h2&gt;

&lt;p&gt;A load test with 105,000 profiles and half a CPU core for Postgres showed something unexpected. If the database (~340 MB) fits into &lt;code&gt;shared_buffers&lt;/code&gt;, the bot's p95 response time is &lt;strong&gt;1.8 s with no errors&lt;/strong&gt;. With a 64 MB buffer and a 63–74% cache hit rate, p95 climbs to &lt;strong&gt;8–30 s&lt;/strong&gt; and errors appear.&lt;/p&gt;

&lt;p&gt;That is why Postgres memory limits live in a single production config, next to the formula for the number of connections (~30 MB per process + ~3.6 MB per connection + &lt;code&gt;shared_buffers&lt;/code&gt;), and they need to be raised once the database grows past ~200 MB.&lt;/p&gt;

&lt;h2&gt;
  
  
  Verifying students without leaking data
&lt;/h2&gt;

&lt;p&gt;Only emails on the selected university's domain are accepted. A few details turned out to matter:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;addresses with a "+tag"&lt;/strong&gt; (&lt;code&gt;ivan+1@ysu.am&lt;/code&gt;) are rejected: they land in the same inbox, so one email could verify several accounts;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;one email — one account&lt;/strong&gt;, checked before the email is sent and again when the link is opened;&lt;/li&gt;
&lt;li&gt;the answers "this email is already taken" and "this email is blocked" reveal something about someone else's address, so their number per account is limited — otherwise you could enumerate addresses and build a list of students who have profiles;&lt;/li&gt;
&lt;li&gt;the link in the email is valid for 24 hours, works once, and only for the Telegram account that requested it;&lt;/li&gt;
&lt;li&gt;email sending is rate-limited in tiers: per account and separately per address, so nobody can flood someone else's inbox from several accounts.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Without a university email, you can send a photo of yourself holding a student ID — a moderator compares the face with the document and the profile.&lt;/p&gt;

&lt;h2&gt;
  
  
  Product rules live in specifications
&lt;/h2&gt;

&lt;p&gt;How the bot should behave is described not in code or in people's heads but in &lt;a href="https://github.com/Fission-AI/OpenSpec" rel="noopener noreferrer"&gt;OpenSpec&lt;/a&gt; specifications: &lt;code&gt;feed&lt;/code&gt;, &lt;code&gt;verification&lt;/code&gt;, &lt;code&gt;safety&lt;/code&gt;, &lt;code&gt;sparks&lt;/code&gt;, &lt;code&gt;i18n&lt;/code&gt;, &lt;code&gt;mini-app&lt;/code&gt; and about twenty more areas. Every requirement comes with concrete "WHEN … THEN …" scenarios. When behavior changes, the spec changes in the same commit, and &lt;code&gt;openspec validate&lt;/code&gt; checks them before every deploy.&lt;/p&gt;

&lt;p&gt;The specs also gave rise to "Bot tests" in the admin panel: a set of ready-made scenarios that runs on fresh test profiles and shows a step-by-step report — what the bot sent, which buttons, and where it diverged.&lt;/p&gt;

&lt;h2&gt;
  
  
  What we ended up with
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;A Telegram bot and a Mini App sharing the logic for the feed, likes, matches and Super Likes.&lt;/li&gt;
&lt;li&gt;Student verification by university email or document, with moderation of every profile.&lt;/li&gt;
&lt;li&gt;A feed over ~105,000 profiles — 18 ms per selection; everything on a single VPS.&lt;/li&gt;
&lt;li&gt;Three interface languages, reports, blocking, "Hide from acquaintances", "Who can see my profile".&lt;/li&gt;
&lt;li&gt;An admin panel: moderation, support requests, load tests, scenario tests for the bot, server health.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The main takeaway for our future projects: &lt;strong&gt;measure at scale before launch&lt;/strong&gt; and &lt;strong&gt;write the product rules down before the code&lt;/strong&gt;. Almost every problem in this article would have been invisible on a database of a few thousand profiles and would have surfaced only with real users.&lt;/p&gt;




&lt;p&gt;If you are building your own Telegram bot, ask away in the comments: we are happy to go deeper into load testing, Prisma on large tables or Telegram Mini Apps.&lt;/p&gt;

&lt;p&gt;See the feed live: the bot &lt;a href="https://t.me/ImYanBot" rel="noopener noreferrer"&gt;@ImYanBot&lt;/a&gt;, the website &lt;a href="https://mtermik.am/en" rel="noopener noreferrer"&gt;mtermik.am&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>postgres</category>
      <category>node</category>
      <category>performance</category>
      <category>telegram</category>
    </item>
  </channel>
</rss>
