2022-07-28 10:00:00+00:00

Relational databases are excellent for transactional consistency, but slow down when handling complex search filters, wildcards, and autocomplete requests across millions of records. An ideal stack delegates searches to a dedicated engine like Elasticsearch, while keeping PostgreSQL as the source of truth.

To do this, we must build a synchronization pipeline that captures database writes and updates Elasticsearch indexes in real time.


1. Hooking into Database Events

We intercept data changes in our ORM models (using SQLAlchemy event listeners or FastAPI service logic) and write an event payload to our search indexing worker queue:

# Syncing PostgreSQL records to Elasticsearch in Python
from elasticsearch import AsyncElasticsearch

es_client = AsyncElasticsearch(hosts=["http://localhost:9200"])

async def index_account_record(account_id, account_data):
    doc_payload = {
        "name": account_data["name"],
        "email": account_data["email"],
        "industry": account_data["industry"],
        "status": account_data["status"]
    }
    
    # index the document asynchronously
    response = await es_client.index(
        index="the enterprise platform-accounts",
        id=account_id,
        document=doc_payload
    )
    return response["result"]

2. Handling Ingestion Failures

If Elasticsearch is down, the synchronization task must not fail the client request. The indexing task is written to an asynchronous queue (using Kafka or SQS), so workers can retry index insertion until successful.

Outbox Pattern for PostgreSQL-to-Elasticsearch Consistency

Writing to PostgreSQL and Elasticsearch in a dual-write HTTP handler risks partial failure if one datastore times out. Using a transactional outbox table or Change Data Capture (CDC) stream guarantees eventual consistency across your relational and search indexes.


About the Author — Hire Freelance Programmer Aldian Fazrihady

Aldian Fazrihady is a Top-Rated Senior Freelance Programmer, Freelance Full Stack Developer, and AI Application Engineer with 15+ years of production software engineering experience and Google Cloud Professional Cloud Architect & Data Engineer certifications. He specializes in building custom LLM & autonomous AI agents, RAG pipelines, Next.js/React PWAs, Python (Django/FastAPI), Go microservices, and serverless cloud infrastructure on Google Cloud Run and AWS.

Hire Aldian Fazrihady on Upwork | Book 1-Day Programming Service ($100 USD via Polar) | View Live AI Portfolio | Portfolio Directory | Send a Direct Message

Related Engineering Articles

Freelance Programmer Specializations & Category Hubs