


In a high-concurrency scenario, should the cache or the database be updated first?

##Cache aside
Cache asideThat is, Bypass cache
, yes More commonly used caching strategies.
Read requestCommon process The application will first determine whether the cache has the data. If the cache hits, the data will be returned directly. If the cache misses, the cache will penetrate to the database and retrieve the data from the database. Query the data and then write it back to the cache, and finally return the data to the client. (2) First update the database and then delete the data from the cache. After looking at the picture of the write request, some students may ask: Why do we need to delete the cache? Can't we just update it directly? There are several pitfalls involved here, let’s step through them step by step. If the Cache aside strategy is used incorrectly, you will encounter deep pits. Let’s step into them one by one. Pitfall 1: Update the database first, then update the cache If there are two The execution process as shown above: (1) (2) (3) (4) The expected result after execution is that the database age is 20, the cache age is 20, and the result cache age is 18. This causes the cache data to be not the latest and dirty data appears. Trap 2: Delete the cache first, then update the database If The execution process as shown above: (1) (2) (3) After the whole process, it was found that the age in Trap Three: Update the database first, then delete the cache In the actual system, for The execution process as shown above: (1) (2) (3) After the entire process, it was found that But if we think about it carefully, the probability of the above problem occurring is actually very low, because database update operations usually take several orders of magnitude more time than memory operations. The last step in the figure above is write-back caching (set age 18) It's very fast and usually completes before updating the database. What if this extreme scenario occurs? We have to think of a solution: In the Cache Aside update mode, the application code needs to maintain two data sources: one is the cache and the other is the database. Under the As shown above, the application only needs to interact with When performing a large number of reads, The cache remains consistent with the data source, and writes always reach the data source through the As shown above, when the application updates two data, the Cache Provider will write it to the cache immediately, but it will be written to the database in batches after a period of time. middle. This method has advantages and disadvantages: After learning so much, I believe everyone has a clear understanding of the cache update strategy. Finally, a little summary. There are three main strategies for cache update: Cache aside Usually the database is updated first, and then the cache is deleted. To protect the data, the cache time is usually set. Read/Write through generally provides read and write operations by a Cache Provider, and the application does not need to know whether the cache or the database is being operated. Write behind simply understands that it is delayed writing. Cache Provider will batch input the database every once in a while. The advantage is that the application writes very quickly. Okay, I’m here today. Have you learned it? Write request
Common process
Cache aside pitfalls
write requests at the same time
Data needs to be updated, each write request The database is updated first and then the cache is updated. Data inconsistency may occur in concurrent scenarios. Write request 1
Update the database, update the age field to 18; Write request 2
Update the database, update the age field to 20;Write request 2
Update cache, cache age is set to 20;Write request 1
Update cache, cache age is set to 18 ;write request
The processing flow isDelete the cache first and then update Database
, in a read request
and a write request
concurrent scenario, data inconsistency may occur. Write request
Delete cached data; Read request
Query cache miss (Hit Miss), then query the database and write the returned data back to the cache;Write request
Update database. database
was 20, and the age in cache
was 18. The cache and database data were inconsistent, and dirty data appeared in the cache. write requests
it is still recommended to update first The database then deletes the cache
, but there are still problems in theory, as shown in the following example. Read request
Query the cache first, if the cache is not hit, query the database to return data; Write request
Update the database and delete the cache; Read request
Write back cache; database age is 20
, cache age is 18
, that is, the database and cache are inconsistent, resulting in application The data read by the program from the cache is old data. Cache data setting expiration time
. Usually in the system, a small amount of data can be allowed to be inconsistent for a short period of time.
Read through
Read-Through
strategy, the application does not need to manage the cache and database, and only needs to entrust the synchronization of the database to the cache provider Cache Provider
. All data interactions are completed through the Abstract Cache Layer
. Cache Provider
, and does not need to care whether it is fetched from the cache or database. Read-Through
can reduce the load on the data source and is also resilient to cache service failures. If the cache service goes down, the cache provider can still operate by going directly to the data source. Read-Through is suitable for scenarios where the same data is requested multiple times
, which is very similar to the Cache-Aside strategy, but there are still some differences between the two, which are emphasized again:
Write through
Write-Through
strategy, when a data update (Write) occurs, the cache provider Cache Provider
Responsible for updating the underlying data source and cache. Abstract Cache Layer
. Cache Provider
It acts like a proxy.
Write behind
Write behind
In some places Also called Write back
, the simple understanding is: when the application updates data, it only updates the cache, Cache Provider
refreshes the data into the database at regular intervals. To put it bluntly, it is Delayed writing
.
Advantage
is that the data writing speed is very fast and is suitable for frequent writing scenarios. . Disadvantage
is that the cache and database are not strongly consistent, so use it with caution in systems with high consistency requirements.
To summarize
The above is the detailed content of In a high-concurrency scenario, should the cache or the database be updated first?. For more information, please follow other related articles on the PHP Chinese website!

Javaispopularforcross-platformdesktopapplicationsduetoits"WriteOnce,RunAnywhere"philosophy.1)ItusesbytecodethatrunsonanyJVM-equippedplatform.2)LibrarieslikeSwingandJavaFXhelpcreatenative-lookingUIs.3)Itsextensivestandardlibrarysupportscompr

Reasons for writing platform-specific code in Java include access to specific operating system features, interacting with specific hardware, and optimizing performance. 1) Use JNA or JNI to access the Windows registry; 2) Interact with Linux-specific hardware drivers through JNI; 3) Use Metal to optimize gaming performance on macOS through JNI. Nevertheless, writing platform-specific code can affect the portability of the code, increase complexity, and potentially pose performance overhead and security risks.

Java will further enhance platform independence through cloud-native applications, multi-platform deployment and cross-language interoperability. 1) Cloud native applications will use GraalVM and Quarkus to increase startup speed. 2) Java will be extended to embedded devices, mobile devices and quantum computers. 3) Through GraalVM, Java will seamlessly integrate with languages such as Python and JavaScript to enhance cross-language interoperability.

Java's strong typed system ensures platform independence through type safety, unified type conversion and polymorphism. 1) Type safety performs type checking at compile time to avoid runtime errors; 2) Unified type conversion rules are consistent across all platforms; 3) Polymorphism and interface mechanisms make the code behave consistently on different platforms.

JNI will destroy Java's platform independence. 1) JNI requires local libraries for a specific platform, 2) local code needs to be compiled and linked on the target platform, 3) Different versions of the operating system or JVM may require different local library versions, 4) local code may introduce security vulnerabilities or cause program crashes.

Emerging technologies pose both threats and enhancements to Java's platform independence. 1) Cloud computing and containerization technologies such as Docker enhance Java's platform independence, but need to be optimized to adapt to different cloud environments. 2) WebAssembly compiles Java code through GraalVM, extending its platform independence, but it needs to compete with other languages for performance.

Different JVM implementations can provide platform independence, but their performance is slightly different. 1. OracleHotSpot and OpenJDKJVM perform similarly in platform independence, but OpenJDK may require additional configuration. 2. IBMJ9JVM performs optimization on specific operating systems. 3. GraalVM supports multiple languages and requires additional configuration. 4. AzulZingJVM requires specific platform adjustments.

Platform independence reduces development costs and shortens development time by running the same set of code on multiple operating systems. Specifically, it is manifested as: 1. Reduce development time, only one set of code is required; 2. Reduce maintenance costs and unify the testing process; 3. Quick iteration and team collaboration to simplify the deployment process.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Atom editor mac version download
The most popular open source editor
