BBackend CourseLeroy Labs
0/0
Search the course

NoSQL & search engines (MongoDB, Cassandra, DynamoDB, Elasticsearch)

Document, key-value, wide-column and graph stores, when each beats Postgres, modeling for access patterns, and full-text search with Postgres tsvector vs Elasticsearch/OpenSearch.

● advanced

Families

Type Examples Data model Shines at
Document MongoDB, Couchbase, Firestore JSON documents Flexible, nested aggregates read together (a product catalogue with varied attributes)
Key-value Redis, DynamoDB key → value Massive scale with simple access (sessions, carts, counters)
Wide-column Cassandra, ScyllaDB, Bigtable Partitioned rows, sorted columns Huge write throughput and time series (IoT, messages, activity feeds)
Graph Neo4j, Neptune Nodes and edges Relationship traversal (social graphs, fraud rings, recommendations)
Search Elasticsearch, OpenSearch, Meilisearch, Typesense Inverted index Full-text relevance, facets, typo tolerance
Time-series TimescaleDB, InfluxDB Time-indexed metrics Monitoring, analytics
Vector pgvector, Pinecone, Qdrant Embeddings Semantic search, RAG for AI features

Modeling differences

Relational modeling normalizes and joins at query time. NoSQL (especially DynamoDB and Cassandra) is modeled for your access patterns: you list the queries first, then design keys so each query reads one partition, and you denormalize freely.

Cassandra table for "messages in a chat, newest first":
  PRIMARY KEY ((chat_id), sent_at DESC, message_id)
  -> partition = one chat; clustering = time order; one query, one partition

Why Postgres first (and why you were right)

Postgres also does JSONB (documents), PostGIS (geo), full-text search, pgvector (embeddings), partitioning (time series) and LISTEN/NOTIFY. One well-run Postgres covers most needs until real scale proves otherwise. Add a new datastore only for a specific, measured need.

Postgres built-in

ALTER TABLE hostels ADD COLUMN search tsvector
  GENERATED ALWAYS AS (to_tsvector('english', coalesce(name,'') || ' ' || coalesce(description,''))) STORED;
CREATE INDEX idx_hostels_search ON hostels USING GIN (search);

SELECT name, ts_rank(search, q) AS rank
FROM hostels, websearch_to_tsquery('english', 'quiet near library') q
WHERE search @@ q ORDER BY rank DESC LIMIT 20;

This would replace Hostel’s LIKE '%x%' search with stemming, ranking and an index. Add pg_trgm for typo tolerance.

Elasticsearch / OpenSearch

These give BM25 relevance tuning, facets and aggregations, synonyms, autocomplete, many languages and scale-out. The cost is that it’s another system to keep in sync, through dual writes (risky), the outbox, or CDC (Debezium). Search is eventually consistent.

Need Use
Search one table, modest scale Postgres tsvector + pg_trgm
Rich product search, facets, typo tolerance, many fields Elasticsearch/OpenSearch/Meilisearch
Logs OpenSearch/ELK, Loki

Check yourself

1. Cassandra/DynamoDB schemas are designed primarily around…
2. Cheapest upgrade from LIKE '%x%' search in Hostel?