Scrivio Interview library
ScenarioIntermediate6 min read · updated 12 November 2026

With Hibernate Search on Elasticsearch, index updates are sometimes lost when the application crashes right after commit, and bulk updates with JPQL do not reach the index. How would you address both?

By default, Hibernate Search indexes in memory right after the commit, so a crash between the commit and the index write loses that update. The outbox-polling coordination strategy fixes this by storing indexing events in a database table in the same transaction as the data and processing them reliably afterwards (check your version). JPQL bulk updates, native SQL and changes made by other applications never raise entity events, so we reindex the affected entities explicitly or with the mass indexer, and we schedule reconciliation for critical indexes.

Why this question is asked

Search results that disagree with the database are one of the most frustrating bugs for users: they search for a product they just renamed and find the old name, or an item they deleted still appears. With Hibernate Search on Elasticsearch, these gaps come from two specific places, and both follow from how indexing is triggered. Interviewers ask this scenario to check that we understand those mechanics rather than just restarting the indexer. In this answer, we will recap how default indexing works, see why a crash loses updates, fix it with outbox polling, deal with bulk and external changes, and add reconciliation as a safety net.

1A Quick Recap of Listener-Triggered Indexing

Hibernate Search listens to Hibernate's entity events. When an indexed entity changes in a transaction, it records the affected document in an in-memory indexing plan. When the transaction commits, it sends the new documents to Elasticsearch. Hibernate Search calls this listener-triggered indexing, and in older versions automatic indexing.

The important word is in-memory. Between the database commit and the moment Elasticsearch acknowledges the write, the only record that the index needs updating lives in the application's heap.

2Why a Crash Loses Index Updates

Consider the timeline. The database commits the new title for book 7. Hibernate Search starts sending the document to Elasticsearch. The JVM is killed, by a deployment, an out-of-memory error or a container eviction, before the request completes. The database has the new title; the index has the old one, and nothing remembers the difference. The same happens if Elasticsearch is unreachable for long enough that the write gives up after the commit.

Step throughLosing an index update in a crash
ApplicationDatabaseElasticsearch
update book 7 title
commit
committed
index document for book 7 (in flight)
JVM crashes
never acknowledged: index keeps old title

6 messages in order. Play them, or step through them one at a time.

The only record of the pending index write was in the heap.

3Fix 1: The Outbox-Polling Coordination Strategy

The transactional outbox pattern solves this class of problem in general: instead of sending a message to another system after commit, we write the message into a table in the same database transaction as the data, and a separate process reads the table and delivers it. If the transaction commits, the message is guaranteed to exist; if it rolls back, the message disappears with it.

Hibernate Search ships this as a coordination strategy called outbox-polling, in a separate module. With it enabled, entity changes produce rows in an outbox event table written in the same transaction. Background agents in each application instance poll that table, build the documents and send them to Elasticsearch, and delete events once they are processed. A crash just means the events wait until an instance picks them up again. The agents also share the work between instances, and the events for one entity are processed in order.

XML
<!-- pom.xml: the outbox-polling module -->
<dependency>
<groupId>org.hibernate.search</groupId>
<artifactId>hibernate-search-mapper-orm-outbox-polling</artifactId>
</dependency>
# application.properties
spring.jpa.properties.hibernate.search.coordination.strategy=outbox-polling
# Hibernate Search creates its outbox and agent tables (names and setup: check your version)

The trade-off is latency and load. Index updates now arrive after a polling delay, typically well under a second to a few seconds, rather than right after commit, and the database carries the extra table writes. For most search features that is acceptable in exchange for not losing updates. Module names, table names and configuration keys have changed between versions, so check the documentation of the version you use.

4Why Bulk Updates Never Reach the Index

The second problem has a different cause. A JPQL bulk update such as update Book b set b.price = b.price * 1.1 is translated into one SQL statement. Hibernate never loads the books, so no entity events fire, and Hibernate Search, with or without outbox polling, has nothing to react to. Native SQL updates behave the same way, and so do changes written directly to the database by other applications, scripts or a DBA fixing data by hand. All of these are invisible to Hibernate Search.

So after a bulk statement we must tell Hibernate Search what changed. For a known set of ids we can add them to the indexing plan explicitly; for larger or unknown sets we reindex the type, or a filtered subset of it, with the mass indexer.

Java
@Transactional
public void raisePrices(String category) {
List<Long> ids = repo.findIdsByCategory(category); // remember which rows we touch
repo.bulkRaisePrices(category); // JPQL bulk update: no entity events
SearchIndexingPlan plan = Search.session(em).indexingPlan();
repo.findAllById(ids).forEach(plan::addOrUpdate); // reindex these documents after commit
}
// For a large change: rebuild only what changed (reindexOnly API: check your version)
Search.session(em).massIndexer(Book.class)
.type(Book.class).reindexOnly("e.category = :c").param("c", category)
.startAndWait();

Notice that the indexing plan is tied to the transaction, so with outbox polling these explicit entries also go through the outbox. For changes from other applications, the cleaner long-term answer is to route them through the owning service's API, so they pass through Hibernate, or to move to change data capture for indexing.

5Fix 2: Scheduled Reconciliation

Even with both fixes, some drift is possible: a manual SQL fix, a bug in a mapping, a restored backup. For indexes the business depends on, a scheduled reconciliation job compares the database with the index and repairs differences. A cheap version compares counts per type and checks recently changed rows; a thorough version compares an updated_at or version field for every id.

Step throughNightly reconciliation of the book index
  1. Select ids and updated_at changed in the last 24 hfrom the books table
  2. Fetch the same ids from the indexwith their indexed updated_at
  3. Missing or older in the index?
    YesAdd to a reindex listNoLeave it
  4. Reindex the listed idsindexing plan or mass indexer
  5. Report the drift countan alert if it is not near zero

5 steps in order. Play them, or step through them one at a time.

6Follow-up questions

Does outbox polling make the index update part of the transaction?

No, and it cannot, because Elasticsearch is a separate system. It makes the intent to update the index part of the transaction. The index is still updated a little later, so the system is eventually consistent, but no committed change is forgotten.

Could we refresh Elasticsearch synchronously instead?

Hibernate Search lets us choose how long a commit waits for the index, for example until the write is committed or even refreshed in Elasticsearch. Waiting longer makes changes searchable sooner after commit, but it does not survive a crash before the write happens, so it does not solve lost updates.

When should we move to change data capture instead?

When many writers change the data outside Hibernate, when several databases feed one index, or when indexing load should be separated from the application. Then a CDC tool such as Debezium reads the database log, which sees every change including bulk and external ones.

7Conclusion

In this answer, we saw that default listener-triggered indexing keeps pending index changes in memory after commit, so a crash can lose them, and that outbox-polling coordination stores indexing events in a table in the same transaction so they are processed reliably. JPQL bulk updates, native SQL and external changes raise no entity events, so we reindex affected entities through the indexing plan or the mass indexer, and a scheduled reconciliation job catches whatever drift remains.

What to say in the interviewAbout 80 seconds aloud

Open with

There are two separate causes. The lost updates after a crash come from how default indexing works: Hibernate Search collects index changes in memory during the transaction and sends them to Elasticsearch after the commit, so if the JVM dies between the commit and the index write, the database has the change and nothing remembers that the index needs it.

Then, if they want more

The fix is the outbox-polling coordination strategy, an extra module where the indexing events are written to a table in the same transaction as the data, and background agents process them afterwards. If the app crashes, the events are still there.

It adds a small delay and some database load, and the configuration details depend on the version, so I would check them. The bulk update problem is different: JPQL bulk updates and native SQL never load entities, so no events fire, and the same goes for changes made by other applications.

After a bulk statement I would add the affected ids to the indexing plan, or for large changes run the mass indexer for that type, filtered if possible. And for critical indexes I would schedule a reconciliation job that compares recently changed rows with the index and repairs any drift.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "With Hibernate Search on Elasticsearch, index updates are sometimes lost when the...": https://scrivio-interview-library.netlify.app/databases/jpa-queries/8afd7314fa/