Skip to content
← Blog

web development

Building High-Performance Web Applications: Caching, Database Optimization, ACID Transactions, and Concurrency Control

zobir16 min read
Building High-Performance Web Applications: Caching, Database Optimization, ACID Transactions, and Concurrency Control

Modern web applications must handle thousands of requests, process large amounts of data, and deliver fast, reliable user experiences.

As applications grow, performance problems become more complex. Slow database queries, inefficient caching strategies, concurrent requests, and poorly managed transactions can significantly affect performance and reliability.

Building a high-performance application is not simply about making code execute faster. It requires understanding how databases, caching systems, transactions, and concurrent operations work together.

In this guide, we will explore essential backend performance concepts using practical examples from Laravel, PHP, MySQL, Redis, and Memcached.

1. Understanding Backend Performance

Backend performance refers to how efficiently a server processes requests, accesses data, executes business logic, and returns responses.

A typical web request follows this process:

  1. A user sends an HTTP request.

  2. The server receives and processes the request.

  3. The application executes business logic.

  4. The application reads or updates database records.

  5. The server returns an HTTP response.

Each step contributes to the total response time.

For example, consider an API endpoint that retrieves products from a database.

public function index()
{
    $products = Product::all();

    return response()->json($products);
}

This implementation might work for a small application.

However, retrieving every product becomes inefficient when the database contains hundreds of thousands of records.

A better solution uses pagination:

public function index()
{
    $products = Product::paginate(20);

    return response()->json($products);
}

Pagination reduces the amount of data retrieved and transferred per request.

Essential Backend Performance Metrics

Important metrics include:

  • Response Time: How long the server takes to respond.

  • Throughput: How many requests the application processes per second.

  • Latency: The delay experienced when processing requests.

  • CPU Usage: How much processing power the application consumes.

  • Memory Usage: How much RAM the application requires.

  • Database Query Time: How long database operations take.

  • Error Rate: The percentage of requests that fail.

Monitoring these metrics helps developers identify bottlenecks and make informed optimization decisions.


2. What Is Caching?

Caching is one of the most effective techniques for improving application performance.

Caching is the process of storing frequently accessed data in a faster storage layer to reduce repeated expensive operations.

Instead of executing the same database query every time, the application retrieves previously calculated or fetched results from a cache.

Example Without Caching

public function dashboard()
{
    $totalUsers = User::count();

    return response()->json([
        'total_users' => $totalUsers
    ]);
}

Every request executes a database query.

If thousands of users access the dashboard, the database repeatedly performs the same calculation.

Example With Laravel Caching

use Illuminate\Support\Facades\Cache;

public function dashboard()
{
    $totalUsers = Cache::remember(
        'total_users',
        now()->addMinutes(10),
        function () {
            return User::count();
        }
    );

    return response()->json([
        'total_users' => $totalUsers
    ]);
}

How It Works

  1. Laravel checks whether the requested value exists in the cache.

  2. If it exists, Laravel returns the cached value.

  3. If it does not exist, Laravel executes the database query.

  4. The result is stored in the cache.

  5. Subsequent requests can reuse the cached result.

This reduces unnecessary database queries and improves response times.

Cache Hits and Cache Misses

Cache Hit: The requested data exists in the cache.

Cache Miss: The requested data does not exist in the cache and must be retrieved or calculated.

A high cache hit rate generally indicates that frequently requested data is being served efficiently, although cache effectiveness also depends on data freshness and system requirements.


3. Redis vs. Memcached

Redis and Memcached are popular in-memory data stores used to accelerate applications.

Although both can be used for caching, they provide different capabilities.

Redis

Redis is an in-memory data store that supports several data structures.

These include:

  • Strings

  • Lists

  • Sets

  • Sorted Sets

  • Hashes

  • Streams

Redis can be used for caching, counters, queues, rate limiting, session storage, and coordination between application processes.

Redis Example in Laravel

Cache::store('redis')->put(
    'total_users',
    1500,
    600
);

$totalUsers = Cache::store('redis')->get(
    'total_users'
);

Redis is especially useful when applications need more than basic key-value caching.

Memcached

Memcached is a distributed in-memory caching system designed for simple and efficient key-value storage.

It is commonly used to cache:

  • Database query results

  • Frequently accessed objects

  • Computed values

  • Temporary application data

Redis vs. Memcached Comparison

Feature

Redis

Memcached

Primary use

General-purpose in-memory data store

Distributed object caching

Data structures

Multiple structures

Key-value items

Persistence

Optional

No built-in disk persistence

Atomic operations

Supported

Supported for selected operations

Pub/Sub

Supported

Not built in

Typical applications

Caching, queues, counters, coordination

Simple high-speed caching

Which One Should You Choose?

Choose Redis when your application requires advanced data structures, queues, atomic counters, or coordination mechanisms.

Choose Memcached when your primary requirement is straightforward, distributed, in-memory caching.

Both can significantly improve performance when implemented correctly.


4. Cache Expiration and Invalidation

Caching introduces an important challenge: keeping data fresh.

Imagine a dashboard displaying the total number of registered users.

The application caches the value:

Cache::put('total_users', 1500, 600);

A new user registers, but the cached value remains unchanged.

The dashboard may continue displaying outdated information until the cache expires.

This is known as stale cache data.

Solution: Cache Invalidation

When the underlying data changes, invalidate the related cache entry.

$user = User::create([
    'name' => $request->name,
    'email' => $request->email,
    'password' => Hash::make(
        $request->password
    ),
]);

Cache::forget('total_users');

The next request can recalculate the updated value.

In production applications, cache invalidation should be coordinated with successful database commits to avoid invalidating entries before a transaction has completed.

Common Cache Invalidation Strategies

Time-Based Expiration (TTL)

Cached values automatically expire after a defined period.

Event-Based Invalidation

Cache entries are invalidated when related data changes.

Versioned Cache Keys

Cache keys include a version identifier that changes when the underlying data is updated.

Choosing the correct strategy depends on how frequently the data changes and how important immediate freshness is.


5. Understanding Cache Stampede

A cache stampede occurs when many concurrent requests attempt to regenerate the same expired or missing cached value.

Consider a popular product page receiving thousands of requests.

The product information is cached for 10 minutes.

When the cache expires, hundreds of requests may simultaneously execute the same expensive database query.

This can overload the database.

Solution: Cache Locking

A common solution is to allow only one process to regenerate the cached value while other processes wait or use a suitable fallback.

Laravel supports atomic locks when using compatible cache drivers.

$products = Cache::remember(
    'popular_products',
    600,
    function () {
        return Cache::lock(
            'popular_products_lock',
            10
        )->block(5, function () {
            return Cache::remember(
                'popular_products',
                600,
                fn () => Product::popular()->get()
            );
        });
    }
);

In this example:

  1. Laravel checks the cache.

  2. A lock coordinates cache regeneration.

  3. The cache is checked again after acquiring the lock.

  4. The expensive query runs only if the value is still missing.

  5. Other requests can reuse the regenerated result.

Applications must also handle lock timeouts appropriately.

Distributed locks are useful for coordinating processes that run across multiple application servers.


6. Database Query Optimization

Caching is important, but it should not be used to hide inefficient database queries.

Optimizing the underlying database operations is equally essential.

Avoid the N+1 Query Problem

The N+1 problem occurs when an application executes one query to retrieve a collection and then executes additional queries for related records.

Inefficient Example

$users = User::all();

foreach ($users as $user) {
    echo $user->posts->count();
}

If the application retrieves 100 users, this code may execute an additional query for each user.

Optimized Solution

$users = User::withCount('posts')->get();

foreach ($users as $user) {
    echo $user->posts_count;
}

Using withCount() retrieves the relationship counts efficiently without issuing a separate query for every user.

Use Database Indexes

Indexes help databases locate records efficiently.

For example:

CREATE INDEX idx_users_email
ON users(email);

This index can improve searches involving the email column.

However, indexes also require storage and add maintenance overhead during writes.

Developers should analyze query patterns before creating indexes.

Retrieve Only Required Columns

Avoid retrieving unnecessary information.

Instead of:

$users = User::all();

Use:

$users = User::select(
    'id',
    'name',
    'email'
)->get();

Reducing unnecessary data retrieval can improve database, memory, and network efficiency.

Analyze Queries

MySQL provides the EXPLAIN statement to inspect query execution plans.

EXPLAIN
SELECT *
FROM users
WHERE email = 'user@example.com';

Query execution plans help identify inefficient access patterns and opportunities for optimization.


7. Database Transactions and ACID

Performance optimization must never compromise data correctness.

Database transactions help maintain reliable operations when multiple database changes must succeed together.

A transaction groups operations into a single logical unit of work.

Consider a banking application transferring money between two accounts.

START TRANSACTION;

UPDATE accounts
SET balance = balance - 200
WHERE id = 1;

UPDATE accounts
SET balance = balance + 200
WHERE id = 2;

COMMIT;

Both updates belong to the same transaction.

If an operation fails, the application can roll back the transaction rather than commit incomplete changes.

Understanding ACID Properties

ACID represents four fundamental transaction properties.

A — Atomicity

All operations succeed or none are committed.

Atomicity prevents partial transactions from being permanently saved.

C — Consistency

Transactions preserve database rules and constraints.

For example, a database constraint can prevent account balances from becoming negative.

CHECK (balance >= 0)

I — Isolation

Concurrent transactions execute with controlled interference.

Isolation helps protect database operations from concurrency-related anomalies.

The guarantees depend on the selected isolation level.

D — Durability

Committed changes survive system failures.

After a successful commit, the database is responsible for preserving committed data according to its durability guarantees.

Transactions in Laravel

Laravel provides a convenient transaction API.

DB::transaction(function () {

    Account::where('id', 1)
        ->decrement('balance', 200);

    Account::where('id', 2)
        ->increment('balance', 200);

});

Laravel automatically commits the transaction when the callback completes successfully.

If the callback throws an exception, Laravel rolls back the transaction.

For real financial operations, additional validation and concurrency safeguards are essential.


8. Concurrency and Race Conditions

Modern applications handle multiple requests concurrently.

Concurrency means multiple operations can make progress during overlapping periods.

However, concurrent operations can produce unexpected results when accessing shared resources.

What Is a Race Condition?

A race condition occurs when a result depends on the timing or execution order of concurrent operations.

Example: Product Inventory

Imagine an e-commerce application selling a product with only one unit remaining.

Two customers attempt to purchase the product simultaneously.

Both requests read:

Stock = 1

Both requests might conclude that the product is available.

Without proper concurrency control, the application could create two successful orders for one available item.

Lost Update Problem

A lost update occurs when one concurrent operation overwrites another operation's changes.

Example:

Initial stock = 10

Request A reads 10
Request B reads 10

Request A saves 9
Request B saves 9

Final stock = 9

Two purchases occurred, but the inventory decreased by only one unit.

This is a classic concurrency bug.


9. Pessimistic Locking

Pessimistic locking prevents conflicting operations by acquiring locks before modifying shared data.

In Laravel, the lockForUpdate() method can be used inside a database transaction.

DB::transaction(function () {

    $product = Product::where('id', 1)
        ->lockForUpdate()
        ->firstOrFail();

    if ($product->stock <= 0) {
        throw new Exception('Out of stock');
    }

    $product->decrement('stock');

    Order::create([
        'product_id' => $product->id,
    ]);

});

How It Works

  1. The transaction starts.

  2. The database acquires the required row lock.

  3. The application checks the available stock.

  4. The stock is updated.

  5. The order is created.

  6. The transaction commits and releases its locks.

Conflicting transactions may need to wait until the lock is released.

When to Use Pessimistic Locking

Pessimistic locking is useful for:

  • Financial transactions

  • Inventory management

  • Reservation systems

  • Operations requiring coordinated updates to shared records

However, excessive locking can increase latency and reduce throughput.

Locks should be held for the shortest practical time.


10. Optimistic Locking

Optimistic locking is another concurrency control technique.

Instead of locking a record before reading it, optimistic locking checks whether the record has changed before applying an update.

Example

Consider an account containing:

Balance = 1000
Version = 1

Two requests read the same record.

Request A successfully executes:

UPDATE accounts
SET balance = 800,
    version = version + 1
WHERE id = 1
AND version = 1;

The updated record becomes:

Balance = 800
Version = 2

Request B attempts another update using the old version:

UPDATE accounts
SET balance = 700,
    version = version + 1
WHERE id = 1
AND version = 1;

This update affects zero rows because the version no longer matches.

The application detects the conflict and can retry using fresh data.

Optimistic vs. Pessimistic Locking

Pessimistic Locking

Optimistic Locking

Acquires locks before modification

Checks for changes before saving

May block conflicting operations

Detects conflicts through version checks

Useful under high contention

Useful when conflicts are relatively rare

Requires careful lock management

Requires conflict handling and possible retries

The right approach depends on the application's workload and consistency requirements.


11. Atomic Database Operations

An atomic operation executes as one indivisible operation from the perspective of concurrent access.

For simple inventory updates, a conditional database update may be more efficient than a separate read followed by a write.

Example

$updated = Product::where('id', 1)
    ->where('stock', '>', 0)
    ->decrement('stock');

if ($updated === 0) {
    throw new Exception('Out of stock');
}

This approach combines the availability condition and the stock update in one database statement.

It helps prevent concurrent requests from decrementing inventory below zero.

However, if the business operation also requires creating an order, updating related records, or recording a payment, a database transaction may still be necessary.


12. Understanding Database Deadlocks

A deadlock occurs when two or more transactions wait for each other to release locks.

Example

Transaction A:

Locks Account 1
Attempts to lock Account 2
Waits

Transaction B:

Locks Account 2
Attempts to lock Account 1
Waits

Each transaction is waiting for a resource held by the other.

Neither can continue without resolving the conflict.

How MySQL Handles Deadlocks

MySQL InnoDB can detect deadlocks and roll back one of the transactions to allow the other to continue.

Preventing Deadlocks

Several techniques can reduce deadlock risk:

  • Acquire locks in a consistent order.

  • Keep transactions short.

  • Use appropriate database indexes.

  • Avoid unnecessary locks.

  • Retry transactions when appropriate.

  • Minimize operations performed while holding locks.

Laravel Deadlock Retry

Laravel supports retry attempts for transactions affected by deadlocks.

DB::transaction(function () {

    // Database operations

}, 3);

The second argument specifies the maximum number of attempts.

Retrying must be designed carefully, particularly when transactions interact with external services or produce non-database side effects.


13. Isolation Levels

Database isolation levels define how concurrent transactions interact with one another.

The four standard isolation levels are:

READ UNCOMMITTED

Allows dirty reads, where a transaction can observe another transaction's uncommitted changes.

READ COMMITTED

Prevents dirty reads.

However, repeated reads within the same transaction may return different committed values.

REPEATABLE READ

Provides stable repeated reads of previously accessed records within a transaction.

MySQL InnoDB uses REPEATABLE READ as its default isolation level.

Its MVCC implementation provides additional protections for consistent reads, although behavior differs between ordinary reads and locking reads.

SERIALIZABLE

Provides the strongest standard isolation guarantees.

Transactions behave as though they executed in some serial order.

This can reduce concurrency and increase contention.

Isolation Level Comparison

Level

Dirty Reads

Non-Repeatable Reads

Phantom Reads

READ UNCOMMITTED

Possible

Possible

Possible

READ COMMITTED

Prevented

Possible

Possible

REPEATABLE READ

Prevented

Prevented

Depends on DBMS implementation

SERIALIZABLE

Prevented

Prevented

Prevented

Choosing an isolation level requires balancing concurrency, performance, and correctness.


14. Redis Persistence: RDB and AOF

Redis stores data primarily in memory, but it can also persist data to disk.

Two important Redis persistence mechanisms are RDB and AOF.

RDB — Redis Database Snapshots

RDB creates point-in-time snapshots of the dataset.

Advantages include:

  • Compact snapshot files

  • Efficient backups

  • Relatively fast recovery for suitable datasets

However, changes made after the most recent snapshot may be lost if the server crashes.

AOF — Append Only File

AOF records write operations so Redis can reconstruct the dataset during recovery.

Depending on configuration, AOF can provide stronger durability than infrequent snapshots.

However, durability is not absolute under every configuration.

RDB vs. AOF

RDB

AOF

Saves snapshots

Records write operations

Compact backup format

More granular persistence

May lose changes after the last snapshot

Potential data loss depends on synchronization settings

Useful for backups and recovery

Useful when stronger persistence is needed

Redis also supports configurations that combine persistence mechanisms.

The correct setup depends on recovery requirements and acceptable data loss.


15. Monitoring and Performance Testing

Optimization without measurement can lead to unnecessary complexity.

Before changing an application, developers should identify the actual bottleneck.

Useful monitoring approaches include:

Application Performance Monitoring

Measure:

  • Request duration

  • Slow endpoints

  • Error rates

  • Memory consumption

  • CPU utilization

Database Monitoring

Identify:

  • Slow queries

  • Missing indexes

  • Lock contention

  • Deadlocks

  • Long-running transactions

Cache Monitoring

Track:

  • Cache hit rate

  • Cache misses

  • Memory usage

  • Evictions

  • Cache latency

Load Testing

Load testing evaluates how an application behaves under increasing traffic.

Tools such as k6 and Apache JMeter can simulate concurrent users.

Developers should compare metrics before and after implementing optimizations.

For production systems, percentiles such as p95 and p99 latency are often more informative than average response time alone.


16. Practical Laravel Performance Best Practices

When building scalable Laravel applications, consider the following practices.

Database Optimization

  • Avoid N+1 queries.

  • Use pagination for large datasets.

  • Add indexes based on real query patterns.

  • Select only necessary columns.

  • Analyze slow queries.

  • Keep transactions short.

Caching

  • Use Redis or Memcached when appropriate.

  • Cache expensive computations.

  • Define suitable expiration times.

  • Invalidate cache entries when data changes.

  • Protect expensive cache regeneration from stampedes.

Concurrency and Reliability

  • Use database transactions for related writes.

  • Understand ACID principles.

  • Apply locking when shared data requires protection.

  • Use atomic updates for simple counters and inventory changes.

  • Handle deadlocks and transaction retries.

  • Use appropriate database constraints.

Application Architecture

  • Move slow background work into queues.

  • Avoid unnecessary external API calls during requests.

  • Minimize synchronous processing.

  • Monitor performance continuously.

  • Test under realistic traffic conditions.

Frontend and Delivery Performance

Backend optimization is only part of application performance.

Frontend improvements can include:

  • Reducing unnecessary JavaScript.

  • Optimizing images.

  • Using efficient rendering strategies.

  • Compressing and caching static assets.

  • Delivering assets through a CDN.

  • Measuring Core Web Vitals, including LCP, CLS, and INP.

A fast backend and an optimized frontend work together to provide a better user experience.


17. Performance vs. Scalability vs. Reliability

These three concepts are related but different.

Performance

How efficiently an application handles a workload.

Example: An API responds in 100 milliseconds.

Scalability

How effectively an application handles increasing workloads by using additional resources or improving its architecture.

Example: A system continues to serve users efficiently as traffic grows.

Reliability

How consistently an application performs its intended functions correctly.

Example: A payment system prevents duplicate transactions and preserves committed records.

An application can be fast but unreliable.

For example, removing transaction safeguards might reduce latency while introducing serious data consistency problems.

Strong backend engineering requires balancing all three.


Conclusion

Building high-performance web applications requires a deeper understanding of how backend systems operate.

Caching reduces repetitive work. Redis and Memcached accelerate data access. Database query optimization improves resource efficiency. Transactions and ACID properties protect data integrity.

Concurrency control, atomic operations, and locking mechanisms help applications handle simultaneous requests correctly, while proper deadlock handling improves resilience.

The most important principle is that performance optimization should never come at the expense of correctness.

A successful architecture combines speed, scalability, reliability, and maintainability.

At PixCoders, we focus on these engineering principles when building modern websites, APIs, SaaS platforms, and custom web applications.

Learn more about our software development services at PixCoders.


Frequently Asked Questions

What is the best way to improve Laravel performance?

Start by identifying performance bottlenecks. Common improvements include optimizing database queries, fixing N+1 problems, introducing caching, using queues, and reducing unnecessary work during HTTP requests.

Is Redis faster than MySQL?

Redis can provide extremely fast access to data stored in memory. However, Redis and MySQL serve different purposes and should not be treated as interchangeable databases in every application.

What is the difference between caching and database optimization?

Caching reduces repeated work by reusing stored results. Database optimization improves the efficiency of retrieving and modifying the underlying data.

What are ACID properties in databases?

ACID stands for Atomicity, Consistency, Isolation, and Durability. These properties describe guarantees that help database transactions remain reliable.

What is a race condition?

A race condition occurs when the result of an operation depends on the timing or order of concurrent operations, potentially creating incorrect results.

How can Laravel prevent race conditions?

Laravel applications can use database transactions, pessimistic locking, atomic updates, database constraints, and application-level optimistic locking to manage concurrent operations.

What causes database deadlocks?

Deadlocks occur when transactions hold locks while waiting for resources locked by one another. They can be reduced by consistent lock ordering and short transactions.

What is the difference between Redis RDB and AOF?

RDB stores periodic snapshots of the Redis dataset, while AOF records write operations for recovery. Each has different performance and durability characteristics.

Why is concurrency control important?

Concurrency control protects shared data when multiple requests operate simultaneously. It helps prevent lost updates, inconsistent data, and conflicting business operations.