<?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: Akhileshpm</title>
    <description>The latest articles on DEV Community by Akhileshpm (@akhileshpm).</description>
    <link>https://dev.to/akhileshpm</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%2F692687%2Fa0181e2b-2e1b-4bb9-aa43-cb59b3b52a13.jpeg</url>
      <title>DEV Community: Akhileshpm</title>
      <link>https://dev.to/akhileshpm</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/akhileshpm"/>
    <language>en</language>
    <item>
      <title>Why Cassandra fits high-frequency writes better than Postgres</title>
      <dc:creator>Akhileshpm</dc:creator>
      <pubDate>Mon, 09 Nov 2026 17:04:09 +0000</pubDate>
      <link>https://dev.to/akhileshpm/why-cassandra-fits-high-frequency-writes-1j4j</link>
      <guid>https://dev.to/akhileshpm/why-cassandra-fits-high-frequency-writes-1j4j</guid>
      <description>&lt;p&gt;Imagine thousands of devices sending measurements every second. Each measurement is an independent write. Cassandra can handle this workload by spreading writes across servers and grouping disk writes on each server.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. What happens to one measurement?
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Cassandra · Commit log, memtable, SSTable
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Append to commit log + update memtable → flush a batch to an SSTable&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Each replica appends the measurement to its commit log and adds it to an in-memory structure called a memtable. If the replica crashes before a flush, it can replay durable commit log entries. The commit log sync settings control when those entries reach durable storage.&lt;/p&gt;

&lt;p&gt;The memtable holds many writes. A flush writes them in sorted order to a new file called an SSTable. This is the write path of a log-structured merge tree, or &lt;strong&gt;LSM tree&lt;/strong&gt;. For an ordinary write, Cassandra can record a new value without reading and modifying an older SSTable.&lt;/p&gt;

&lt;h3&gt;
  
  
  PostgreSQL · WAL, heap pages, indexes
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Record WAL + update buffered heap and index pages → flush pages later&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;PostgreSQL also has a recovery log, called the write-ahead log (WAL). It can acknowledge a durable commit before the changed table pages reach disk.&lt;/p&gt;

&lt;p&gt;Each insert adds a row to a heap page and updates the applicable indexes. With a B-tree index, it adds an entry to the appropriate index page. PostgreSQL buffers page changes, but the primary still does the work of maintaining the table and its indexes as measurements arrive.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Cassandra groups table writes into new sorted files. Later, compaction merges files and rewrites data. This shifts some work out of the write request and adds background disk I/O. Sustained throughput depends on compaction keeping pace.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://cassandra.apache.org/doc/stable/cassandra/architecture/storage-engine.html" rel="noopener noreferrer"&gt;Cassandra storage engine&lt;/a&gt; · &lt;a href="https://www.postgresql.org/docs/18/wal-intro.html" rel="noopener noreferrer"&gt;PostgreSQL WAL&lt;/a&gt; · &lt;a href="https://www.postgresql.org/docs/18/indexes-intro.html" rel="noopener noreferrer"&gt;PostgreSQL index maintenance&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  2. What happens when the write rate doubles?
&lt;/h2&gt;

&lt;p&gt;In Cassandra, the device ID and hour can form the partition key. Cassandra routes each partition to its replica set. Writes for different devices can use different replica sets. Adding nodes can increase write capacity if the partitions and write traffic are well distributed. One heavily used partition still depends on its own replica set.&lt;/p&gt;

&lt;p&gt;In a standard PostgreSQL primary and standby setup, all inserts go to one primary. Standbys replay its WAL. If incoming writes exceed the primary’s capacity to process rows, maintain indexes, or flush WAL and data pages, requests can queue. Write latency rises, and clients can eventually time out. To spread that load across multiple PostgreSQL primaries, the deployment needs sharding.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://cassandra.apache.org/doc/stable/cassandra/architecture/dynamo.html" rel="noopener noreferrer"&gt;Cassandra partitioning and replication&lt;/a&gt; · &lt;a href="https://www.postgresql.org/docs/18/warm-standby.html" rel="noopener noreferrer"&gt;PostgreSQL physical replication&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  When Cassandra has the advantage
&lt;/h2&gt;

&lt;p&gt;This workload gives Cassandra two ways to sustain writes. The LSM tree groups many changes into sorted disk writes. Partitioning lets multiple servers do that work in parallel. That combination can make Cassandra a better fit when independent writes exceed what one PostgreSQL primary can sustain.&lt;/p&gt;

&lt;p&gt;PostgreSQL can handle high insert rates within the capacity of its primary. The crossover depends on the hardware, schema, replication and durability settings, and how evenly the application distributes writes. A fair benchmark measures sustained throughput and write latency while compaction and other background work run.&lt;/p&gt;

&lt;h2&gt;
  
  
  Inside Cassandra’s write path
&lt;/h2&gt;

&lt;p&gt;On each replica, the same measurement passes through three structures. Each structure has a different job.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;New measurement → commit log and memtable → SSTable → compaction&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Commit log · Recover a write after a crash
&lt;/h3&gt;

&lt;p&gt;The replica appends the measurement to its local commit log. Tables on that replica share the log. Cassandra writes to the current segment until it reaches its size limit, then starts another segment.&lt;/p&gt;

&lt;p&gt;Suppose the replica crashes before it flushes the measurement to an SSTable. On restart, Cassandra can replay the durable log entry into the memtable. After all changes in a segment reach SSTables, Cassandra can recycle that segment.&lt;/p&gt;

&lt;p&gt;In &lt;strong&gt;batch&lt;/strong&gt; sync mode, the replica waits for the log to sync to disk before it acknowledges the write. In &lt;strong&gt;periodic&lt;/strong&gt; mode, it can acknowledge before the next sync. A crash can therefore lose recent unsynced writes on that replica.&lt;/p&gt;

&lt;h3&gt;
  
  
  Memtable · Collect writes in memory
&lt;/h3&gt;

&lt;p&gt;Cassandra keeps recent changes for a table in a memtable. Reads can see data in the memtable before Cassandra flushes it. This allows the replica to collect many measurements before it writes a new SSTable.&lt;/p&gt;

&lt;p&gt;A flush can occur when memtables reach their memory limit or when Cassandra needs to free commit log space. Cassandra switches to a new memtable so that incoming writes can continue while the previous one flushes.&lt;/p&gt;

&lt;h3&gt;
  
  
  SSTable · Store a sorted batch on disk
&lt;/h3&gt;

&lt;p&gt;A flush writes the data in sorted order to a new SSTable. An SSTable is immutable. If a device sends a correction for the same measurement later, Cassandra can write the correction to a new SSTable.&lt;/p&gt;

&lt;p&gt;A read may need data from the memtable and several SSTables. Bloom filters help Cassandra skip files that cannot contain the requested partition. Compaction merges selected SSTables and can remove obsolete values.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;For the device workload, each flush turns many small changes into a larger sequential write. Compaction rewrites some of that data later, so the disks need enough capacity for both incoming writes and background merges.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://cassandra.apache.org/doc/stable/cassandra/architecture/storage-engine.html" rel="noopener noreferrer"&gt;Apache Cassandra storage engine documentation&lt;/a&gt;&lt;/p&gt;

</description>
      <category>cassandra</category>
      <category>postgres</category>
      <category>database</category>
      <category>systemdesign</category>
    </item>
    <item>
      <title>Cassandra vs PostgreSQL: How Facebook's Inbox Search Led to a Distributed Database</title>
      <dc:creator>Akhileshpm</dc:creator>
      <pubDate>Sun, 08 Nov 2026 09:54:35 +0000</pubDate>
      <link>https://dev.to/akhileshpm/cassandra-vs-postgresql-how-facebooks-inbox-search-led-to-a-distributed-database-828</link>
      <guid>https://dev.to/akhileshpm/cassandra-vs-postgresql-how-facebooks-inbox-search-led-to-a-distributed-database-828</guid>
      <description>&lt;h2&gt;
  
  
  A database choice that starts with a search problem
&lt;/h2&gt;

&lt;p&gt;Imagine you're building a messaging application.&lt;/p&gt;

&lt;p&gt;Users send thousands of messages every second. Over time, the application stores billions of messages. And users expect something that sounds simple:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;"Show me all the messages containing the word 'football'."&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;At first, this looks like a straightforward database query.&lt;/p&gt;

&lt;p&gt;If you're familiar with PostgreSQL, you might start with a table like this:&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="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;messages&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="n"&gt;BIGSERIAL&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;user_id&lt;/span&gt; &lt;span class="nb"&gt;BIGINT&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="k"&gt;message_text&lt;/span&gt; &lt;span class="nb"&gt;TEXT&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;created_at&lt;/span&gt; &lt;span class="nb"&gt;TIMESTAMP&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To search for messages containing a particular word, PostgreSQL provides full-text search.&lt;/p&gt;

&lt;p&gt;For example:&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="k"&gt;SELECT&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;message_text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;created_at&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;messages&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;to_tsvector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'english'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;message_text&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="o"&gt;@@&lt;/span&gt; &lt;span class="n"&gt;plainto_tsquery&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'english'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'football'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can improve this with a GIN index:&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="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="n"&gt;messages_search_idx&lt;/span&gt;
&lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;messages&lt;/span&gt;
&lt;span class="k"&gt;USING&lt;/span&gt; &lt;span class="n"&gt;GIN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;to_tsvector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'english'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;message_text&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is a perfectly reasonable starting point.&lt;/p&gt;

&lt;p&gt;In fact, PostgreSQL has a mature full-text search implementation. It would be misleading to say that PostgreSQL cannot handle message search or that it automatically becomes slow when a table grows.&lt;/p&gt;

&lt;p&gt;So, where does the architectural problem begin?&lt;/p&gt;

&lt;h2&gt;
  
  
  When the workload starts changing
&lt;/h2&gt;

&lt;p&gt;Let's say the messaging application grows.&lt;/p&gt;

&lt;p&gt;Now we're dealing with:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Billions of messages.&lt;/li&gt;
&lt;li&gt;A continuous stream of new messages.&lt;/li&gt;
&lt;li&gt;Search indexes that grow alongside the message data.&lt;/li&gt;
&lt;li&gt;Users searching their message history frequently.&lt;/li&gt;
&lt;li&gt;A requirement to distribute storage and traffic across multiple machines.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The challenge is no longer just writing a good SQL query.&lt;/p&gt;

&lt;p&gt;It's about how we distribute the data, maintain the indexes, handle writes, and serve queries as the system grows.&lt;/p&gt;

&lt;p&gt;PostgreSQL can be scaled using partitioning, read replicas, and distributed extensions such as Citus. These are valid approaches.&lt;/p&gt;

&lt;p&gt;However, they introduce additional architectural decisions.&lt;/p&gt;

&lt;p&gt;For example, PostgreSQL table partitioning divides a table into smaller physical pieces, but it does not automatically distribute writes across independent database servers.&lt;/p&gt;

&lt;p&gt;Similarly, read replicas can help scale read traffic, but they don't distribute the primary database's write workload.&lt;/p&gt;

&lt;p&gt;There's also the cost of maintaining indexes.&lt;/p&gt;

&lt;p&gt;A GIN index is useful for full-text search, but inserting or updating a document can require updating multiple index entries. PostgreSQL's documentation explains that GIN updates can be expensive and that its pending-list mechanism can introduce additional work during searches.&lt;/p&gt;

&lt;p&gt;References:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.postgresql.org/docs/current/gin.html" rel="noopener noreferrer"&gt;PostgreSQL: GIN Indexes&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.postgresql.org/docs/current/ddl-partitioning.html" rel="noopener noreferrer"&gt;PostgreSQL: Table Partitioning&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;None of this means PostgreSQL is the wrong choice.&lt;/p&gt;

&lt;p&gt;It means that for a particular workload, we may want a storage architecture designed around distributing large amounts of data and handling high write throughput.&lt;/p&gt;

&lt;p&gt;And that's where Cassandra becomes interesting.&lt;/p&gt;

&lt;h2&gt;
  
  
  The real-world example: Facebook Inbox Search
&lt;/h2&gt;

&lt;p&gt;One of the most interesting examples comes from Facebook.&lt;/p&gt;

&lt;p&gt;In August 2008, Facebook introduced Cassandra as a distributed storage system.&lt;/p&gt;

&lt;p&gt;The original problem was not to replace Facebook's entire database infrastructure.&lt;/p&gt;

&lt;p&gt;It was specifically about &lt;strong&gt;Inbox Search&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Facebook needed to store reverse indexes of messages that users sent and received. The amount of data was growing, and the team needed a storage system that could scale incrementally across many commodity servers without a single point of failure.&lt;/p&gt;

&lt;p&gt;The original engineering article describes Cassandra as a distributed structured-data storage system built on a peer-to-peer architecture.&lt;/p&gt;

&lt;p&gt;It also describes the initial deployment: terabytes of indexes distributed across more than 600 CPU cores and 120 TB of disk.&lt;/p&gt;

&lt;p&gt;That is an important distinction.&lt;/p&gt;

&lt;p&gt;Facebook wasn't using Cassandra as a magical search engine. It was using Cassandra to store and distribute the data structures that made message searching possible.&lt;/p&gt;

&lt;p&gt;Source: &lt;a href="https://engineering.fb.com/2008/08/25/core-infra/cassandra-a-structured-storage-system-on-a-p2p-network/" rel="noopener noreferrer"&gt;Cassandra – A Structured Storage System on a P2P Network&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's understand how that works.&lt;/p&gt;

&lt;h2&gt;
  
  
  First, what is an inverted index?
&lt;/h2&gt;

&lt;p&gt;An inverted index is a data structure that maps a word to the documents or messages containing that word.&lt;/p&gt;

&lt;p&gt;Imagine we have these messages:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Message ID&lt;/th&gt;
&lt;th&gt;Message&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;101&lt;/td&gt;
&lt;td&gt;Hello, how are you?&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;102&lt;/td&gt;
&lt;td&gt;Are you watching football?&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;103&lt;/td&gt;
&lt;td&gt;Let's have dinner tonight&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;104&lt;/td&gt;
&lt;td&gt;Football is my favourite sport&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;105&lt;/td&gt;
&lt;td&gt;Hello, see you tomorrow&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;A normal message table stores messages and their contents.&lt;/p&gt;

&lt;p&gt;An inverted index stores the relationship in the opposite direction:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Keyword&lt;/th&gt;
&lt;th&gt;Message IDs&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;hello&lt;/td&gt;
&lt;td&gt;101, 105&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;football&lt;/td&gt;
&lt;td&gt;102, 104&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;dinner&lt;/td&gt;
&lt;td&gt;103&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;watching&lt;/td&gt;
&lt;td&gt;102&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Now, when a user searches for &lt;code&gt;football&lt;/code&gt;, we don't need to inspect every message's text.&lt;/p&gt;

&lt;p&gt;We look up the keyword in the index and get the matching message IDs.&lt;/p&gt;

&lt;p&gt;The application can then retrieve the actual message contents using those IDs.&lt;/p&gt;

&lt;p&gt;The important idea is that &lt;strong&gt;the search index is a separate data structure built from the original messages&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This is also the basic concept behind many full-text search systems.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where Cassandra fits into this architecture
&lt;/h2&gt;

&lt;p&gt;Cassandra is not a full-text search engine.&lt;/p&gt;

&lt;p&gt;It doesn't automatically read arbitrary message text, extract keywords, and create a searchable index for you.&lt;/p&gt;

&lt;p&gt;The application needs to build that index.&lt;/p&gt;

&lt;p&gt;Cassandra's role is to store and distribute the index across a cluster of machines.&lt;/p&gt;

&lt;p&gt;Let's understand the architecture through a simplified example.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. The application extracts keywords
&lt;/h3&gt;

&lt;p&gt;When a new message arrives:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Message ID: 201
User ID: 42
Message: "Let's watch football tonight"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The application extracts the searchable terms:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;football
tonight
watch
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It then creates index entries connecting those terms to the message.&lt;/p&gt;

&lt;p&gt;Conceptually:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;football -&amp;gt; Message 201
tonight  -&amp;gt; Message 201
watch    -&amp;gt; Message 201
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A real implementation would also need to consider tokenization, language handling, stop words, duplicate terms, and possibly word positions.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Cassandra stores the index using partitions
&lt;/h3&gt;

&lt;p&gt;Cassandra organizes data around a concept called a &lt;strong&gt;partition key&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The partition key determines how data is distributed across the cluster.&lt;/p&gt;

&lt;p&gt;For example, we could model a per-user keyword index like this:&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="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;messages_by_term&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;user_id&lt;/span&gt; &lt;span class="nb"&gt;BIGINT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;term&lt;/span&gt; &lt;span class="nb"&gt;TEXT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;time_bucket&lt;/span&gt; &lt;span class="nb"&gt;TEXT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;message_time&lt;/span&gt; &lt;span class="nb"&gt;TIMESTAMP&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;message_id&lt;/span&gt; &lt;span class="nb"&gt;BIGINT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;positions&lt;/span&gt; &lt;span class="n"&gt;LIST&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;INT&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;term&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;time_bucket&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="n"&gt;message_time&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;message_id&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;WITH&lt;/span&gt; &lt;span class="n"&gt;CLUSTERING&lt;/span&gt; &lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;message_time&lt;/span&gt; &lt;span class="k"&gt;DESC&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;message_id&lt;/span&gt; &lt;span class="k"&gt;ASC&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is an illustrative schema, not Facebook's actual Cassandra schema.&lt;/p&gt;

&lt;p&gt;Let's understand its structure.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;user_id&lt;/code&gt;: The user whose messages are being searched.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;term&lt;/code&gt;: The keyword extracted from a message.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;time_bucket&lt;/code&gt;: A time-based grouping, such as a month.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;message_time&lt;/code&gt;: Helps retrieve matching messages in chronological order.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;message_id&lt;/code&gt;: Identifies the original message.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;positions&lt;/code&gt;: Can store where the keyword appeared in the message.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The combination of &lt;code&gt;user_id&lt;/code&gt;, &lt;code&gt;term&lt;/code&gt;, and &lt;code&gt;time_bucket&lt;/code&gt; forms the partition key.&lt;/p&gt;

&lt;p&gt;That means all matching index entries for a particular user, keyword, and time bucket are grouped into the same logical partition.&lt;/p&gt;

&lt;p&gt;Cassandra uses consistent hashing to distribute partitions across nodes.&lt;/p&gt;

&lt;p&gt;It also replicates partitions across multiple nodes according to the configured replication strategy.&lt;/p&gt;

&lt;p&gt;So, instead of relying on one database server to store the entire search index, we can distribute that index across a cluster.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. What happens when a user searches?
&lt;/h3&gt;

&lt;p&gt;Suppose user &lt;code&gt;42&lt;/code&gt; searches for:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;football
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The application knows the user ID and the keyword.&lt;/p&gt;

&lt;p&gt;It can query Cassandra:&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="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;message_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;message_time&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;messages_by_term&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;user_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;42&lt;/span&gt;
  &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;term&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'football'&lt;/span&gt;
  &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;time_bucket&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'2026-10'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Cassandra locates the partition using its partition key and retrieves the matching entries.&lt;/p&gt;

&lt;p&gt;The application then uses the returned message IDs to fetch the actual messages.&lt;/p&gt;

&lt;p&gt;Conceptually:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;User searches for "football"
             |
             v
Application extracts search parameters
             |
             v
Cassandra looks up the matching partition
             |
             v
Returns matching message IDs
             |
             v
Application fetches message contents
             |
             v
Search results are returned to the user
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice something important here.&lt;/p&gt;

&lt;p&gt;Cassandra isn't scanning every message to find the word &lt;code&gt;football&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The application has already created an index that allows Cassandra to retrieve the matching message references directly.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Why do we need time buckets?
&lt;/h3&gt;

&lt;p&gt;You might have noticed the &lt;code&gt;time_bucket&lt;/code&gt; column in the partition key.&lt;/p&gt;

&lt;p&gt;Why not simply use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(user_id, term)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Imagine a user has millions of messages containing the same common word.&lt;/p&gt;

&lt;p&gt;If all those index entries belong to one partition, that partition can become extremely large.&lt;/p&gt;

&lt;p&gt;Large or heavily accessed partitions can create performance and operational problems.&lt;/p&gt;

&lt;p&gt;Time bucketing helps divide those entries into smaller partitions.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;User 42 + football + 2026-08
User 42 + football + 2026-09
User 42 + football + 2026-10
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the application can search only the relevant time buckets.&lt;/p&gt;

&lt;p&gt;There is a trade-off, though.&lt;/p&gt;

&lt;p&gt;If a user wants to search their entire message history, the application may need to query multiple buckets and combine the results.&lt;/p&gt;

&lt;p&gt;This is an important Cassandra design principle:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;You model your tables around the queries you need to support.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Unlike a traditional relational design, you don't normally depend on joins to combine normalized tables at query time.&lt;/p&gt;

&lt;p&gt;Instead, you often duplicate data and maintain separate tables for different access patterns.&lt;/p&gt;

&lt;p&gt;That duplication is intentional. It helps make reads predictable and efficient.&lt;/p&gt;

&lt;p&gt;Source: &lt;a href="https://cassandra.apache.org/doc/stable/cassandra/developing/data-modeling/intro.html" rel="noopener noreferrer"&gt;Cassandra Data Modeling Introduction&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What happens internally when Cassandra receives a write?
&lt;/h2&gt;

&lt;p&gt;So far, we've discussed how Cassandra distributes the index.&lt;/p&gt;

&lt;p&gt;But how does it store incoming data efficiently?&lt;/p&gt;

&lt;p&gt;Cassandra uses an architecture based on an LSM tree (Log-Structured Merge Tree).&lt;/p&gt;

&lt;p&gt;Let's simplify the write path.&lt;/p&gt;

&lt;p&gt;When the application sends a write:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;A coordinator node receives the request.&lt;/li&gt;
&lt;li&gt;Cassandra identifies the replica nodes responsible for the partition.&lt;/li&gt;
&lt;li&gt;The replicas persist the write to their commit logs.&lt;/li&gt;
&lt;li&gt;The data is added to an in-memory structure called a memtable.&lt;/li&gt;
&lt;li&gt;Eventually, the memtable is flushed to disk as an immutable SSTable.&lt;/li&gt;
&lt;li&gt;Background compaction merges SSTables over time.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The flow looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Application
    |
    v
Coordinator Node
    |
    +-----------&amp;gt; Replica Node 1
    |                  |
    |                  v
    |             Commit Log
    |                  |
    |                  v
    |              Memtable
    |                  |
    |                  v
    |               SSTable
    |
    +-----------&amp;gt; Replica Node 2
    |
    +-----------&amp;gt; Replica Node 3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Why is this useful?&lt;/p&gt;

&lt;p&gt;Traditional B-tree indexes often require updating existing structures in place.&lt;/p&gt;

&lt;p&gt;An LSM-based storage engine is designed around writing changes to memory and flushing them to disk in sequential files.&lt;/p&gt;

&lt;p&gt;This makes it well suited to write-heavy workloads.&lt;/p&gt;

&lt;p&gt;However, it isn't free.&lt;/p&gt;

&lt;p&gt;Because data can exist across multiple SSTables, reads may need to check more than one file. Cassandra uses mechanisms such as Bloom filters to avoid checking SSTables that definitely don't contain the requested partition.&lt;/p&gt;

&lt;p&gt;Compaction also consumes background I/O and creates write amplification.&lt;/p&gt;

&lt;p&gt;So Cassandra makes a different trade-off: efficient write handling and distributed storage, with additional work required to manage reads and compaction.&lt;/p&gt;

&lt;p&gt;Source: &lt;a href="https://cassandra.apache.org/doc/stable/cassandra/architecture/storage-engine.html" rel="noopener noreferrer"&gt;Cassandra Storage Engine&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What if one Cassandra node goes down?
&lt;/h2&gt;

&lt;p&gt;Another important part of Cassandra's architecture is replication.&lt;/p&gt;

&lt;p&gt;For example, imagine a replication factor of three.&lt;/p&gt;

&lt;p&gt;A partition is stored on three replica nodes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Partition A
   |
   +---- Node 1
   +---- Node 2
   +---- Node 3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If one node becomes unavailable, the other replicas may still serve requests, depending on the consistency level used.&lt;/p&gt;

&lt;p&gt;For example, with a replication factor of three:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;ONE&lt;/code&gt; requires a response from one replica.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;QUORUM&lt;/code&gt; requires a majority of replicas, which is two in this example.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Choosing the consistency level is an architectural decision.&lt;/p&gt;

&lt;p&gt;A quorum read and quorum write generally provide overlapping replica acknowledgements for the same replica set, but this does not mean Cassandra behaves exactly like a traditional relational database transaction.&lt;/p&gt;

&lt;p&gt;Cassandra provides tunable consistency, allowing the application to choose a trade-off between availability, latency, and consistency for its operations.&lt;/p&gt;

&lt;p&gt;This is useful for systems that need to remain available while distributing data across many machines.&lt;/p&gt;

&lt;p&gt;Source: &lt;a href="https://cassandra.apache.org/doc/latest/cassandra/architecture/dynamo.html" rel="noopener noreferrer"&gt;Cassandra Dynamo Architecture&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Does this mean Cassandra is always better than PostgreSQL?
&lt;/h2&gt;

&lt;p&gt;Definitely not.&lt;/p&gt;

&lt;p&gt;A database should be selected based on the workload, not simply because it can distribute data across more servers.&lt;/p&gt;

&lt;p&gt;Consider the differences:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Requirement&lt;/th&gt;
&lt;th&gt;PostgreSQL&lt;/th&gt;
&lt;th&gt;Cassandra&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Relational queries and joins&lt;/td&gt;
&lt;td&gt;Excellent support&lt;/td&gt;
&lt;td&gt;Not supported&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Full-text search&lt;/td&gt;
&lt;td&gt;Built-in full-text search&lt;/td&gt;
&lt;td&gt;Requires an application-built index or another search system&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Transactions&lt;/td&gt;
&lt;td&gt;Rich ACID transactions&lt;/td&gt;
&lt;td&gt;Different transaction and consistency model&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Distributed storage&lt;/td&gt;
&lt;td&gt;Requires additional architecture or extensions for sharding&lt;/td&gt;
&lt;td&gt;Built into its distributed architecture&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Data modeling&lt;/td&gt;
&lt;td&gt;Often starts with normalized relational tables&lt;/td&gt;
&lt;td&gt;Query-driven and often denormalized&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Write-heavy distributed workloads&lt;/td&gt;
&lt;td&gt;Can handle substantial workloads; scaling depends on architecture&lt;/td&gt;
&lt;td&gt;Designed for distributed, high-volume workloads&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;For a typical business application with relational data, complex queries, transactions, and moderate traffic, PostgreSQL may be the simpler and better choice.&lt;/p&gt;

&lt;p&gt;For a workload involving huge amounts of data, predictable access patterns, high write throughput, and a need to distribute data across many nodes, Cassandra may be worth considering.&lt;/p&gt;

&lt;p&gt;And if the requirement is sophisticated full-text search with relevance ranking, fuzzy matching, and language processing, a dedicated search engine may be a better fit than either database alone.&lt;/p&gt;

&lt;h2&gt;
  
  
  One important historical detail: Facebook later chose HBase for Messages
&lt;/h2&gt;

&lt;p&gt;There's another interesting part of this story.&lt;/p&gt;

&lt;p&gt;In 2010, Facebook published an article explaining the technology behind its newer Messages infrastructure.&lt;/p&gt;

&lt;p&gt;The team evaluated multiple technologies, including MySQL, Cassandra, and HBase.&lt;/p&gt;

&lt;p&gt;The article explains that MySQL struggled with long-tail data as datasets and indexes grew.&lt;/p&gt;

&lt;p&gt;It also explains that Cassandra's eventual consistency model was difficult to reconcile with the requirements of the newer Messages infrastructure.&lt;/p&gt;

&lt;p&gt;Facebook ultimately selected HBase for that system.&lt;/p&gt;

&lt;p&gt;This is worth mentioning because it prevents us from drawing the wrong conclusion from the original Cassandra story.&lt;/p&gt;

&lt;p&gt;Cassandra was a successful fit for Facebook's original Inbox Search problem.&lt;/p&gt;

&lt;p&gt;That did not make it the best choice for every subsequent messaging workload.&lt;/p&gt;

&lt;p&gt;The same company chose different databases for different engineering requirements.&lt;/p&gt;

&lt;p&gt;Source: &lt;a href="https://engineering.fb.com/2010/11/15/core-infra/the-underlying-technology-of-messages/" rel="noopener noreferrer"&gt;The Underlying Technology of Messages – Facebook Engineering&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What should we take away from this?
&lt;/h2&gt;

&lt;p&gt;The interesting lesson isn't that Cassandra is faster than PostgreSQL.&lt;/p&gt;

&lt;p&gt;It's that the way we model and distribute data changes depending on the problem we're trying to solve.&lt;/p&gt;

&lt;p&gt;With PostgreSQL, we might begin with normalized message records and a GIN index for full-text search.&lt;/p&gt;

&lt;p&gt;As the workload grows, we need to evaluate index maintenance, write throughput, partitioning, replication, and the complexity of distributing the workload.&lt;/p&gt;

&lt;p&gt;With Cassandra, we can build an application-managed inverted index, partition it around the queries we need, and distribute and replicate those partitions across a cluster.&lt;/p&gt;

&lt;p&gt;But that comes with trade-offs:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We need to design tables around access patterns.&lt;/li&gt;
&lt;li&gt;We may duplicate data.&lt;/li&gt;
&lt;li&gt;We need to maintain the search index when messages are created or deleted.&lt;/li&gt;
&lt;li&gt;Queries across multiple partitions require additional coordination.&lt;/li&gt;
&lt;li&gt;We need to understand consistency levels and operational trade-offs.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The database isn't the architecture by itself.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The workload determines the architecture, and the architecture helps determine the database.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;That's the real lesson behind Cassandra's original use at Facebook.&lt;/p&gt;




&lt;h2&gt;
  
  
  References and further reading
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://engineering.fb.com/2008/08/25/core-infra/cassandra-a-structured-storage-system-on-a-p2p-network/" rel="noopener noreferrer"&gt;Cassandra – A Structured Storage System on a P2P Network (Facebook Engineering, 2008)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://engineering.fb.com/2010/11/15/core-infra/the-underlying-technology-of-messages/" rel="noopener noreferrer"&gt;The Underlying Technology of Messages (Facebook Engineering, 2010)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://cassandra.apache.org/doc/stable/cassandra/developing/data-modeling/intro.html" rel="noopener noreferrer"&gt;Cassandra Data Modeling&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://cassandra.apache.org/doc/stable/cassandra/architecture/storage-engine.html" rel="noopener noreferrer"&gt;Cassandra Storage Engine&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://cassandra.apache.org/doc/latest/cassandra/architecture/dynamo.html" rel="noopener noreferrer"&gt;Cassandra Dynamo Architecture&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.postgresql.org/docs/current/gin.html" rel="noopener noreferrer"&gt;PostgreSQL GIN Indexes&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.postgresql.org/docs/current/ddl-partitioning.html" rel="noopener noreferrer"&gt;PostgreSQL Table Partitioning&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>database</category>
      <category>postgres</category>
      <category>cassandra</category>
      <category>systemdesign</category>
    </item>
    <item>
      <title>Understanding WordPress Architecture on AWS: It’s Simpler Than You Think</title>
      <dc:creator>Akhileshpm</dc:creator>
      <pubDate>Sun, 22 Sep 2024 11:39:43 +0000</pubDate>
      <link>https://dev.to/akhileshpm/understanding-wordpress-architecture-on-aws-its-simpler-than-you-think-57g5</link>
      <guid>https://dev.to/akhileshpm/understanding-wordpress-architecture-on-aws-its-simpler-than-you-think-57g5</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;"To understand complexity, first break it into its simplest components."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Take a look at this architecture for hosting WordPress on Amazon: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fykvfwalaso3b023we1m2.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fykvfwalaso3b023we1m2.jpg" alt="Image description" width="" height=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Do you understand this diagram? If yes, excellent—this blog will be a good refresher for you. If not, no worries. By the end of this blog, you’ll be able to understand it clearly.&lt;/p&gt;

&lt;h2&gt;
  
  
  Let's break down the key building blocks.
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;a href="https://docs.aws.amazon.com/route53/" rel="noopener noreferrer"&gt;Amazon Route 53 &amp;amp; CloudFront (DNS and CDN)&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;
Route 53 is the domain name service offered by AWS. It acts as the website's "phone directory," ensuring that when a user types in your website’s name, they are directed to the correct resources in the cloud. &lt;br&gt;
&lt;a href="https://docs.aws.amazon.com/cloudfront/" rel="noopener noreferrer"&gt;Amazon CloudFront&lt;/a&gt;, the content delivery network (CDN), ensures that static content like images and CSS are delivered quickly by caching them closer to the user’s location, improving speed and performance.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;a href="https://docs.aws.amazon.com/AmazonS3/latest/userguide/Welcome.html" rel="noopener noreferrer"&gt;S3 for Static Assets&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;
Think of Amazon S3 as a storage locker where your WordPress static files, such as media and theme assets, are kept. S3 is highly reliable and accessible, ensuring that files are stored redundantly across multiple locations and are always available to serve users.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;a href="https://docs.aws.amazon.com/vpc/" rel="noopener noreferrer"&gt;VPC and Subnets&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;
The Virtual Private Cloud (VPC) is like a gated community for your resources. Public subnets allow web traffic from users to reach your application, while private subnets isolate sensitive resources (such as your database) from the outside world, keeping them secure. This segmentation ensures that your WordPress site is both accessible and protected.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;a href="https://docs.aws.amazon.com/vpc/latest/userguide/vpc-nat-gateway.html" rel="noopener noreferrer"&gt;NAT Gateway&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;
The NAT (Network Address Translation) Gateway is like a secure tunnel that allows resources in private subnets (like your database or backend servers) to access the internet without exposing themselves directly. For instance, WordPress instances may need to download updates or plugins from the internet, and the NAT Gateway allows this outbound communication while keeping the servers themselves private and hidden from outside threats.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;a href="https://docs.aws.amazon.com/elasticloadbalancing/latest/application/introduction.html" rel="noopener noreferrer"&gt;Application Load Balancer&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;
ALB acts as a smart traffic controller. When multiple users visit your WordPress site, the load balancer distributes incoming requests across multiple servers (WordPress instances) to prevent any single server from being overwhelmed. This ensures that your site can handle increased traffic without slowing down.&lt;br&gt;
The WordPress application itself runs on EC2 instances. The beauty of AWS Auto Scaling is that it automatically adjusts the number of EC2 instances based on traffic patterns. So, during high-traffic periods, additional instances are launched to handle the load, and when traffic decreases, instances are terminated to save costs.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;a href="https://docs.aws.amazon.com/efs/" rel="noopener noreferrer"&gt;Elastic File System (EFS)&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;
WordPress instances need to share files (such as media uploads) across all servers. Amazon EFS provides a scalable and shared file system that can be accessed by multiple WordPress instances at the same time. This ensures that every server has access to the same files, keeping your website consistent across instances.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;a href="https://docs.aws.amazon.com/elasticache/" rel="noopener noreferrer"&gt;ElastiCache (Memcached)&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;
To improve performance, frequently accessed data (such as user sessions or query results) can be cached in memory rather than being fetched from the database each time. ElastiCache for Memcached is used for this purpose, acting like a temporary storage that makes your site load faster by reducing the need for repetitive database queries.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;a href="https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/Welcome.html" rel="noopener noreferrer"&gt;RDS for MySQL with Read Replicas&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;
At the heart of WordPress is the MySQL database. Amazon RDS (Relational Database Service) manages the database, taking care of backups, scaling, and updates. With read replicas, you can scale the database’s read operations to different servers, allowing more users to access your content simultaneously without performance bottlenecks.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;a href="https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-regions-availability-zones.html" rel="noopener noreferrer"&gt;Region&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;
AWS is divided into multiple geographic regions, each consisting of multiple isolated locations. Each region is independent, allowing for low latency and compliance with local laws. Regions are named after their geographical locations (e.g., US East (N. Virginia), EU (Frankfurt)). Choosing the right region can optimize performance and costs based on user location.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;a href="https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-regions-availability-zones.html" rel="noopener noreferrer"&gt;Availability Zones&lt;/a&gt;&lt;/strong&gt; (AZ)&lt;br&gt;
Each region contains multiple Availability Zones (typically 2 to 6). AZs are distinct data centers with their own power, cooling, and physical security. They are designed to be isolated from failures in other AZs, providing high availability and fault tolerance.Services deployed across multiple AZs can improve reliability and performance by distributing workloads.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Architecture Walkthrough
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;DNS Request (Amazon Route 53)&lt;/strong&gt;&lt;br&gt;
When a user types in the website URL, Amazon Route 53 routes the request to the Application Load Balancer (ALB). Route 53 works as the domain name service, directing the user to the correct web resources within AWS.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Content Delivery&lt;/strong&gt; (Amazon CloudFront)&lt;br&gt;
Amazon CloudFront, the Content Delivery Network (CDN), checks if the requested static content is cached at an edge location. If it’s available, it delivers static files like images, CSS, or JS directly to the user. If not, the request moves to the origin (WordPress servers). These static files are stored in AWS S3.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;An &lt;strong&gt;internet gateway&lt;/strong&gt; enables resources in your public subnets (such as EC2 instances) to connect to the internet if the resource has a public IPv4 address or an IPv6 address.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Application Load Balancer&lt;/strong&gt; (ALB)&lt;br&gt;
The Application Load Balancer (ALB) receives the request and forwards it to one of the WordPress EC2 instances running in either Availability Zone 1 (AZ1) or AZ2. The ALB ensures traffic is balanced between instances, preventing overloading of a single server.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;NAT Gateway&lt;/strong&gt; (AZ1 &amp;amp; AZ2)&lt;br&gt;
When the EC2 instances in private subnets need to access the internet (e.g., downloading WordPress updates), they use the NAT Gateway. This ensures secure outbound internet access for EC2 instances in both AZ1 and AZ2, while still keeping them private and unreachable from external sources.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Auto Scaling &amp;amp; EC2 Instances&lt;/strong&gt; (AZ1 &amp;amp; AZ2)&lt;br&gt;
The EC2 instances in AZ1 and AZ2 are where WordPress runs. Auto Scaling dynamically adjusts the number of these EC2 instances based on traffic levels. During high traffic, more instances are launched; during low traffic, they are terminated to save costs. This setup ensures redundancy across two Availability Zones for high availability.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Caching (ElastiCache)&lt;/strong&gt;&lt;br&gt;
To improve response times, frequently accessed data (such as session data or popular blog posts) is cached in ElastiCache (using Memcached). This reduces the load on the database by serving cached data directly from memory, avoiding repeated database queries for the same content.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Database Queries (RDS MySQL)&lt;/strong&gt;&lt;br&gt;
For dynamic content (such as posts and comments), requests are sent to the Amazon Aurora database. The RDS instance manages the database operations, while read replicas are used to improve performance by offloading read operations. Write operations happen on the primary RDS instance, but read-heavy workloads are distributed across read replicas in both AZ1 and AZ2.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;EFS for Shared Files&lt;/strong&gt;&lt;br&gt;
All WordPress instances in both AZ1 and AZ2 access a shared Elastic File System (EFS), ensuring that all media uploads and shared WordPress files are accessible to each server. This ensures that when one instance uploads an image or stores a file, all other instances can immediately access it, maintaining a consistent user experience.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now that you’ve gotten a grasp of the core components, revisit the diagram—you’ll likely have a clearer understanding of the architecture and how it all fits together.&lt;/p&gt;

</description>
      <category>aws</category>
      <category>wordpress</category>
      <category>architecture</category>
    </item>
    <item>
      <title>A pipe in Nestjs to validate DTOs conditionally.</title>
      <dc:creator>Akhileshpm</dc:creator>
      <pubDate>Fri, 08 Mar 2024 12:39:23 +0000</pubDate>
      <link>https://dev.to/akhileshpm/a-pipe-in-nestjs-to-validate-dtos-conditionally-hh1</link>
      <guid>https://dev.to/akhileshpm/a-pipe-in-nestjs-to-validate-dtos-conditionally-hh1</guid>
      <description>&lt;p&gt;A week ago, I was working on an API, and it was a dynamic one or in other words a one for four. The endpoint: &lt;a href="http://localhost:5000/api/v2/item/%7BitemId%7D/readings" rel="noopener noreferrer"&gt;http://localhost:5000/api/v2/item/{itemId}/readings&lt;/a&gt;. Depending on the item ID, the item changes and query params passed also change. And in order to validate these changing query params I required a dynamically validating pipe.&lt;/p&gt;

&lt;p&gt;The Internet didn't help (maybe I didn't search with the right keywords). Tried ChatGPT, Gemini, and Bing, which had Copilot. TBH, Gemini was the dumbest, Bing the smartest but with a hiccup, and surprisingly Chatgpt provided the nearest solution. After tweaking some lines of code, I got what I wanted and thought to share it here. Please don't hesitate to point out the mistakes in the comments.&lt;/p&gt;

&lt;p&gt;Starting with the Pipe I created&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="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Injectable&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;CustomDTOValidationPipe&lt;/span&gt; &lt;span class="k"&gt;implements&lt;/span&gt; &lt;span class="nx"&gt;PipeTransform&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;transform&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;metatype&lt;/span&gt; &lt;span class="p"&gt;}:&lt;/span&gt; &lt;span class="nx"&gt;ArgumentMetadata&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;metatype&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isDtoType&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;metatype&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="nx"&gt;value&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="nx"&gt;dtoClass&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;moduleDTORegistry&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;moduleType&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;dtoClass&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;BadRequestException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;AppConstants&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;INVALID_MODULE_TYPE&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;//to make the query object an instance of the DTO passed.&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;dtoInstance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;plainToClass&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;DynamicModulesParamsDto&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;errors&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;validateSync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;dtoInstance&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;skipMissingProperties&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="na"&gt;validationError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;target&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="k"&gt;private&lt;/span&gt; &lt;span class="nf"&gt;isDto&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="na"&gt;metatype&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;boolean&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;metatype&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;metatype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt; &lt;span class="k"&gt;instanceof&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;errors&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;BadRequestException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;extractErrorMessages&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;errors&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="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nf"&gt;isDtoType&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="na"&gt;metatype&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;boolean&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;metatype&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;metatype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt; &lt;span class="k"&gt;instanceof&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="cm"&gt;/* {
     "target": {
       "dateProcessed": "12-03-202",
       "timezone": "12:34:34323",
       "timestamp": "213",
       "state": "213",
       "plate": "123",
       "type": "213",
       "minConfidence": "213",
       "maxConfidence": "213",
       "moduleType": "hazmat"
     },
     "value": "12-03-202",
     "property": "dayProcessed",
     "children": [],
     "constraints": { "isDate": "dateProcessed must be in the dd:mm:yyyy format" }
   },
 given above is the structure of error we get from 
 validateSync method. Therefore, we need to take only the constraints to return it in the response.*/&lt;/span&gt;

   &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nf"&gt;extractErrorMessages&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="na"&gt;errors&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="p"&gt;[]):&lt;/span&gt; &lt;span class="kr"&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;return&lt;/span&gt; &lt;span class="nx"&gt;errors&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;error&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;errorConstraints&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;constraints&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;errorConstraints&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;keys&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;errorConstraints&lt;/span&gt;&lt;span class="p"&gt;)[&lt;/span&gt;&lt;span class="mi"&gt;0&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;For the sake of reusability, I have created a mapping for the 'moduleType' and its respective DTO.&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;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;moduleDTOMapping&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;person&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;PersonQueryParamsDto&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;Since this pipe is in place, whenever a new kind of item comes in this endpoint you just have to add its name as the property and DTO in the above mapping.&lt;/p&gt;

&lt;p&gt;When a client  hits the endpoint&lt;br&gt;
pipe checks the 'moduleType' and validates the query params with its corresponding DTO.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;dynamic-items.controller.ts&lt;/strong&gt;&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="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;ApiQuery&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;moduleType&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;string&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;enum&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ModuleType&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;required&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="nd"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;:itemId/readings&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="nf"&gt;getReadings&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Req&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Param&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;item&lt;/span&gt;&lt;span class="dl"&gt;'&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;IdValidationPipe&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Query&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;DynamicDTOValidationPipe&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="nx"&gt;queryParams&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;DynamicModulesParamsDto&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;//controller logic goes here&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To recapitulate, This article creates a pipe for validating multiple DTOs based on a query param. For example, for type = person, a person's DTO will be validated, and if the type is a dog, then the dog's DTO is validated. Given that the DTO and type mapping are declared somewhere.&lt;/p&gt;

</description>
      <category>nestjs</category>
      <category>backend</category>
      <category>pipes</category>
      <category>typescript</category>
    </item>
  </channel>
</rss>
