Improving the performance of background batch data processing in distributed services
The paper addresses the problem of optimizing a background process (worker) that performs batch processing of records in a distributed service deployed as multiple instances. In the original implementation, each record was processed individually, which resulted in an excessive number of DBMS queries (the N+1 problem), lock conflicts between concurrently running instances, and incomplete data processing. An approach was proposed and implemented that includes selecting records in fixed-size batches with row-level locking at the DBMS level (SELECT … FOR UPDATE SKIP LOCKED), processing each batch within a single transaction, batch status updates, and parallel processing of records within a batch. Load testing demonstrated a processing speedup of up to 6.3 times and a reduction in the number of database queries by a factor of approximately 7; the business logic, metrics, and tracing were preserved.
batch data processing, background tasks, distributed services, transactions, row-level locking, concurrent access, PostgreSQL DBMS, Go language, N+1 problem.
Introduction
High-load information systems are often built as distributed services consisting of several horizontally scalable instances. Part of the work in such systems is performed asynchronously by background tasks (workers), which periodically select data from the database, process them, and write the results to the database. The throughput of the system and the load on the DBMS depend on these workers.
However, a straightforward worker implementation may lead to performance and correctness problems: an excessive number of database queries when each record is processed individually (the N+1 problem); conflicts between instances that select the same records simultaneously; and missed records that appear in the database while the worker is running.
The aim of this work was to redesign the background worker so as to reduce the number of DBMS queries, eliminate conflicts between instances, and ensure the processing of all incoming records without changing the business logic. To achieve this aim, the following tasks were addressed: to analyze the original implementation and identify bottlenecks; to develop an optimized processing algorithm; to implement it while preserving metrics, tracing, and logging; and to conduct functional and load testing.
Literature Review
The N+1 problem is described in the literature on database access design: an application executes one query to select identifiers and then a separate query for each record, so the number of DBMS calls grows in proportion to the data volume [1, 2]. The standard way to eliminate this problem is batch processing, in which data are selected and updated in groups rather than one record at a time [1].
Row-level locks are used when a single table is processed in parallel by several instances. In PostgreSQL, the SELECT … FOR UPDATE clause locks the selected rows, and the SKIP LOCKED option allows a transaction to skip rows already locked by another transaction instead of waiting for them to be released [6, 7]. As a result, several worker instances can process a shared table of records without waiting for one another.
Atomicity and isolation of operations are ensured by transactions in accordance with the ACID properties; executing a group of operations within a single transaction reduces overhead and ensures data consistency [2, 10]. In Go, parallel processing is implemented using goroutines, while the correctness of concurrent data access is ensured by synchronization mechanisms and the memory model of the language [4, 5, 8, 9].
Methodology
The study was conducted on a background worker that finalizes records which have transitioned to an intermediate state, in a distributed service written in Go and using the PostgreSQL DBMS. The service is deployed as several instances, each of which runs its own copy of the worker.
The original implementation had three drawbacks. First, the N+1 problem occurred: a query was executed to retrieve the identifiers of all records to be processed, and then, for each record, a separate query was executed to retrieve the record, followed by a separate check and a separate query to update its status. For 1,000 records, this generated approximately 19,000 DBMS queries. Second, all instances simultaneously selected the same records and attempted to lock them, which led to lock conflicts and repeated attempts to process the same data. Third, in a single run, the worker processed only the records that existed at the moment it started and ignored the records that appeared during its operation.
The operation of the original implementation is shown in Fig. 1. The left part shows the worker algorithm within a single instance: after the identifiers of all records are selected, separate queries are executed sequentially for each record to retrieve it, check the conditions, and update its status, and each update is performed in a separate transaction. The right part shows the interaction of several instances with the records table: all instances access the same set of records, and records added after the workers have started remain unprocessed.

Fig. 1. Flowchart of the original worker implementation
To eliminate these drawbacks, the following algorithm was proposed (Fig. 2):
- records are selected from the database in fixed-size batches; the rows are locked at the time of selection using SELECT … FOR UPDATE SKIP LOCKED, which allows concurrently running instances to automatically skip records that have already been taken for processing by another instance;
- the entire batch is processed within a single transaction;
- records within a batch are processed in parallel;
- the statuses of all processed records in a batch are updated by one or several (no more than three) batch queries;
- the processing cycle is repeated as long as unprocessed records remain in the database, which guarantees the processing of records that appear while the worker is running.
The algorithm of the optimized worker and the distribution of records among instances are shown in Fig. 2. Batch selection, parallel record processing, batch status updates, and committing the changes (COMMIT) are performed within one transaction per batch. Owing to the SKIP LOCKED option, each instance receives its own batch that does not overlap with the batches of other instances, and records added after the workers have started are selected in one of the subsequent iterations (batch 4 in Fig. 2).

Fig. 2. Flowchart of the optimized worker implementation
The new implementation was enabled through a feature flag, which made it possible to enable and disable it in the production environment without code changes. The instrumentation was preserved and extended: performance metrics, OpenTelemetry tracing, and logging of the number of processed records and errors.
To verify correctness, repository-level tests were written for the new SQL queries, as well as functional tests verifying that each record is processed exactly once when several instances operate in parallel. To evaluate efficiency, load testing was performed: the original and optimized versions were compared for different numbers of instances and records, and the number of DBMS queries was measured using a query tracer.
Results
The results of load testing, in which the performance of the original and optimized versions was compared, are presented in Table 1. When 50 records were processed by two instances, the optimized version achieved a speedup of 1.67 times and reduced the number of queries by a factor of 2.8. When 1,000 records were processed by five instances, the execution time decreased from 1,774 ms to 283 ms (a speedup of 6.3 times), and the number of queries decreased from 19,005 to 2,720 (a reduction by a factor of approximately 7). The number of queries per record decreased from ~19 to ~2.7. For both 1,000 and 5,000 records, the number of queries was reduced by a factor of approximately 7; the greatest speedup (6.3 times) was obtained for 1,000 records, whereas for 5,000 records the speedup was 2.7 times.
Table 1.
| Number of instances | Number
of records |
Time, ms (orig./opt.) |
Queries (orig./opt.) |
Speedup | Query reduction |
| 2 | 50 | 75.77 / 45.46 | 502 / 178 | 1.67× | 2.8× |
| 5 | 1,000 | 1,774.18 / 282.75 | 19,005 / 2,720 | 6.27× | 6.99× |
| 5 | 5,000 | 9,126.22 / 3,362.43 | 95,005 / 13,520 | 2.71× | 7.03× |
Conclusion
This paper proposes and implements an approach to optimizing background batch data processing in a distributed service. Batch selection with row-level locking (SKIP LOCKED), a single transaction per batch, parallel processing within a batch, and batch status updates made it possible to reduce the number of DBMS queries by a factor of approximately 7, to accelerate processing by up to 6.3 times, to eliminate lock conflicts between instances, and to ensure the processing of records that arrive while the worker is running. The approach is applicable to background tasks in which several instances process a shared table of records.
References
1. Connolly T., Begg C. Bazy dannykh. Proektirovanie, realizatsiya i soprovozhdenie. Teoriya i praktika [Databases. Design, Implementation and Maintenance. Theory and Practice]. — 3rd ed. — Moscow: Vil'yams, 2017. — 1440 p. (In Russian)
2. Garcia-Molina H., Ullman J. D., Widom J. Sistemy baz dannykh. Polnyi kurs [Database Systems: The Complete Book]. — Moscow: Vil'yams, 2003. — 1088 p. (In Russian)
3. Obe R., Hsu L. PostgreSQL: Up and Running. — 3rd ed. — Sebastopol: O'Reilly Media, 2017. — 314 p.
4. Donovan A. A. A., Kernighan B. W. Yazyk programmirovaniya Go [The Go Programming Language]. — Moscow: Vil'yams, 2016. — 432 p. (In Russian)
5. Cox-Buday K. Concurrency in Go: Tools and Techniques for Developers. — Sebastopol: O'Reilly Media, 2017. — 238 p.
6. Concurrency Control // PostgreSQL Documentation / The PostgreSQL Global Development Group. — URL: https://www.postgresql.org/docs/current/mvcc.html (accessed: 18 October 2026).
7. SELECT. The Locking Clause // PostgreSQL Documentation / The PostgreSQL Global Development Group. — URL: https://www.postgresql.org/docs/current/sql-select.html (accessed: 18 October 2026).
8. Effective Go: Concurrency // The Go Programming Language. — URL: https://go.dev/doc/effective_go (accessed: 18 October 2026).
9. The Go Memory Model // The Go Programming Language. — URL: https://go.dev/ref/mem (accessed: 18 October 2026).
10. Gray J., Reuter A. Transaction Processing: Concepts and Techniques. — San Francisco: Morgan Kaufmann, 1993. — 1070 p.
11. Fowler M. Patterns of Enterprise Application Architecture. — Boston: Addison-Wesley, 2002. — 533 p.
12. Kleppmann M. Designing Data-Intensive Applications: The Big Ideas Behind Reliable, Scalable, and Maintainable Systems. — Sebastopol: O'Reilly Media, 2017. — 590 p.
13. Hohpe G., Woolf B. Enterprise Integration Patterns: Designing, Building, and Deploying Messaging Solutions. — Boston: Addison-Wesley, 2003. — 736 p.
14. Härder T., Reuter A. Principles of Transaction-Oriented Database Recovery // ACM Computing Surveys. — 1983. — Vol. 15, No. 4. — P. 287–317.
15. Berenson H., Bernstein P., Gray J., Melton J., O'Neil E., O'Neil P. A Critique of ANSI SQL Isolation Levels // Proceedings of the 1995 ACM SIGMOD International Conference on Management of Data. — San Jose, 1995. — P. 1–10.
16. Ports D. R. K., Grittner K. Serializable Snapshot Isolation in PostgreSQL // Proceedings of the VLDB Endowment. — 2012. — Vol. 5, No. 12. — P. 1850–1861.
