36 \ May 2019 \ http://www.phparch.com
Memory Abstractions
Internal Apparatus
A few lines later, line A0255.985, we
have:
Lock out interrupts until the space
has been allocated
To put the problem in perspective,
let’s take a typical PHP/MySQL situa-
tion:
- Alice has 150 points.
- Alice, not knowing her point
balance, wants to transfer 100
points to Bob. - Alice also wants to transfer 100
points to Carl.
Alice, in her first web request, trans-
fers 100 points to Bob as follows. - Check Alice’s balance to ensure it
contains at least 100 points. It does.
At this point, suppose our server
begins processing the second web
request. Alice, in her second web
request, transfers 100 points to Carl as
follows. - Check Alice’s balance to ensure it
contains at least 100 points. It does.
2. Begin transaction.
3. Deduct 100 points from Alice’s
balance.
4. Add 100 points to Carl’s balance.
5. Commit transaction.
Now our server resumes processing
the first web request.- Begin transaction.
- Deduct 100 points from Alice’s
balance. - Add 100 points to Bob’s balance.
- Commit transaction.
We can see the result. Bob has 100
points, Carl has 100 points, and Alice
has -50 points. That’s a problem (it’s a
race condition). Do you see how to fix
the problem? Place the balance check
inside the transaction: - Begin transaction.
- Check Alice’s balance to ensure it
contains at least 100 points. It does. - Deduct 100 points from Alice’s
balance. - Add 100 points to Bob’s balance.
5. Commit transaction.
Assuming we are following correct
database protocol for table locks, when
we do the same procedure for transfer-
ring 100 points from Alice to Carl, the
balance check now fails. The transfer
won’t happen.
This sort of timing problem (two
things happening simultaneously)
happens all the time. The solution
locks the “critical region,” forcing one
event to happen after rather than at the
same time as the other. “Holding a lock,”
“holding a semaphore,” and “inside a
transaction” all work the same way.
They each force their events to happen
in orderly sequence instead of jumbled
on top of each other.
Here is the wrong way to handle the
“critical region”:
- Check/verify the prerequisite
conditions (Does Alice have 100
points available?) - Lock out other processes during the
update (begin transaction)
Docker for Developers, 2nd Edition
Tankersley
Chris Tankersley is a PHP Developer living in Northwest Ohio. He has been developing PHP applications for more than ten years, across
a wide variety of frameworks and business needs. In addition to programming, Chris manages hosting and server deployments for
developers that are looking for more than just what basic hosting provides. He currently spends most of his time working with Sculpin,
Zend Framework 2, and Drupal.
Chris is also an author of many PHP articles for php[architect], as well as a speaker, traveling around the US giving talks at many PHP-
based conferences. Chris also helped found the Northwest Ohio PHP User Group. Chris is the PHP FIG representative for Sculpin, a static
site generator written in PHP, and the lead developer for the PHP Mentoring website.
Docker For Developerswho are looking at Docker as a replacement for is designed for developers
development environments like virtualization, or devops people who want to see how to take
an existing application and integrate Docker into that work ow. is book covers not only how
to work with Docker, but how to make Docker work with your application.
is revised and expanded edition includes creating custom images, working with Docker Compose and
Docker Machine, managing Logs, and 12-factor applications.
You’ll learn how to work with containers, what they are, and how they can help you as a developer.
See how Docker simpli es building, testing, and deploying distributed applications. By running
Docker and separating out the di erent concerns of your application you will have a more robust, scalable
application.
Learn how to use Docker to deploy your application and make it a part of your deployment strategy,
helping not only ensure your environments are the same but also making it easier to package and deliver.
Chris Tankersley
2nd Edition
Docker For Developers is designed for developers looking at
Docker as a replacement for development environments like
virtualization, or devops people who want to see how to take
an existing application and integrate Docker into their work-
flow.
This revised and expanded edition includes:
- Creating custom images
- Working with Docker Compose and Docker Machine
- Managing logs
- 12-factor applications
Order Your Copy