A simple Python-based multi-threaded transaction processing layer built on top of an embedded key/value store. The project supports two concurrency control schemes:
- Optimistic Concurrency Control (OCC)
- Conservative Two-Phase Locking (2PL)
Transactions are defined via workload templates and executed in parallel worker threads. The system collects basic metrics (throughput, response time, aborts) and is intended as an educational prototype.
Before getting started, ensure you have:
- Python 3.8+ (tested on 3.9)
- RocksDB installed on your system
- C compiler (for macOS: run
xcode-select --install) - pip package manager
cd /path/to/Multi-threaded_Transaction_ProcessorInstall RocksDB using official Installation Guide: https://github.com/facebook/rocksdb/blob/main/INSTALL.md
python3 -m venv venv
source venv/bin/activatepip install --upgrade pip
pip install -r requirements.txtThis installs:
rockstore==0.2.0– embedded key/value storecffi,pycparser– transitive dependencies
Edit config.py to customize your experiment:
NUM_THREADS = 4 # Number of worker threads
CONTENTION_PROBABILITY = 0.3 # Probability of accessing hotset (0.0-1.0)
MODE = "CCC" # Concurrency control: "OCC" or "2PL" (or "CCC")
WORKLOAD_FILE = "workload1/workload1.txt" # Path to workload template
INPUT_FILE = "workload1/input1.txt" # Path to initial data| Parameter | Description | Example Values |
|---|---|---|
NUM_THREADS |
Number of parallel worker threads | 2, 4, 8, 16 |
CONTENTION_PROBABILITY |
Fraction of operations on hotset (high contention data) | 0.1 (low), 0.5 (medium), 0.9 (high) |
MODE |
Concurrency control scheme | "OCC" (optimistic), "2PL" or "CCC" (pessimistic locking) |
WORKLOAD_FILE |
Template defining transaction operations | "workload1/workload1.txt", "workload2/workload2.txt" |
INPUT_FILE |
Initial keyspace population data | "workload1/input1.txt", "workload2/input2.txt" |
Low Contention Experiment:
NUM_THREADS = 8
CONTENTION_PROBABILITY = 0.1
MODE = "OCC"High Contention 2PL Test:
NUM_THREADS = 4
CONTENTION_PROBABILITY = 0.8
MODE = "2PL"Start the processor with:
python3 main.pyThe program will:
- Load initial data from
INPUT_FILE - Parse the workload template from
WORKLOAD_FILE - Execute transactions across all worker threads
- Display per-transaction logs during execution
- Print a summary at the end:
Throughput: 123.4
Average Response Time: 0.056
Aborts: 10
main.py– bootstrapper: initializes DB, loads data, parses workload, picks concurrency manager, and starts the executor.storage/database.py– thread-safe wrapper aroundrockstorewith JSON serialization.loader/data_loader.py– readsINPUT_FILEand populates the database.transaction/– defines runtime transaction objects with fields used by OCC or 2PL.occ/occ_manager.py– optimistic concurrency controller with timestamp validation.ccc/twopl_manager.py– conservative two-phase locking manager with wait‑die policy.workload/workload_parser.py– converts text templates into lightweight objects.workload/workload_executor.py– worker pool and execution logic (OCC and 2PL variants).workload/exec_helpers.py– utility functions for building READ/WRITE closures.metrics/metrics.py– collects commit/abort counts and response times.
The executor uses a queue of templates and spawns a fixed number of worker threads; each worker creates a new runtime transaction object from the template, assigns keys according to the configured contention level, and then executes operations sequentially while interacting with the chosen concurrency manager. 2PL transactions acquire locks before accessing records and release them on commit/abort; OCC transactions buffer reads/writes and validate at commit.

Workload files live under workload/ and use a simple format:
WORKLOAD
TRANSACTION 1 INPUTS: A_KEY, B_KEY
READ(A_KEY)
READ(B_KEY)
WRITE(A_KEY, {...})
WRITE(B_KEY, {...})
COMMIT
TRANSACTION 2 INPUTS: ...
...
END
Input files contain initial key insertions used to populate the database before execution.
Two example workloads are included:
workload/workload1/workload1.txtwithworkload1/input1.txtworkload/workload2/workload2.txtwithworkload2/input2.txt
Run either by pointing config.WORKLOAD_FILE/INPUT_FILE accordingly.
Happy experimenting! 🚀