Presents your XML AND WEB SERVICES E-NEWSLETTER for December 15, 2003 <-------------------------------------------> IMPROVE PERFORMANCE BY CACHING WEB SERVICE RESPONSES When building Web services as a public domain service, it's nearly impossible to know the volume of throughput that your Web service will attract. It could be one user per hour, or it could gain notoriety and attract thousands every minute. In this unpredictable scenario, it's advisable that you take certain performance precautions. Due to the fact that the Web service probably provides a fairly generic set of methods usable by various different application consumers, you may find your Web service bombarded by several identical requests. For example, you may provide a Web service that returns the current temperature of a given city somewhere in the world. Your system updates the values for these queries every hour, therefore, creating a redundant lookup. Because your system stores the same values for 60-minute intervals for a given city, there's no need to hit the database for every request. Visual Studio .NET provides a simple way to implement the caching of results from a Web service. Take a look at the CacheDuration property and a code sample that demonstrates its behavior. CACHEDURATION PROPERTY The CacheDuration property tells your ASP.NET application how long to store the results for identical requests for a specific Web service method. The usage is as follows: The value is specified in seconds. When the Web service method is invoked with an identical parameter set, Visual Studio .NET recognizes that the method has already been invoked with those parameters and checks if it already has the response cached. If not, it executes the body of the method and functions as a normal method and caches the response and tracks the time in order to begin the countdown of the CacheDuration property. If the Web method already has the response cached, the method body is bypassed and the cached response is immediately returned to the client, greatly improving performance. If the response is cached, but it's determined that the duration value has elapsed, the method is again re-executed and the response cached for the interval of the duration value once again. BEHAVIOR DEMONSTRATION In order to properly understand exactly how this property behaves, it's best to observe what happens through a code sample. The example below is simply a behavior demonstration and not a real-world example. The Web service defines a single Test method with a CacheDuration set to 60 seconds. After this method is invoked for the first time, the same response will be returned for every subsequent request for 60 seconds. Because it has no parameters, every request is identical to each other. LISTING A shows the Test method code: Listing A Public Function Test() As Byte() Dim fs As New FileStream("c:\test.txt", FileMode.Open) Try Dim arr(fs.Length) As Byte fs.Read(arr, 0, fs.Length) Return arr Catch ex As Exception fs.Close() End Try End Function After pasting this code into your Web service, create a new text file at the C: drive root called test.txt, and put some identifiable text in this file so that you can verify the results. The code in LISTING B is from the consumer of this Web service. Create a Windows Application and add the code below to the Page_Load event and add a Web reference to the Web service created above: Listing B Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim Proxy As New localhost.WSCache() Dim arr As Byte() = Proxy.Test() Dim fs As New FileStream("c:\testout.txt", FileMode.CreateNew) Try fs.Write(arr, 0, arr.Length) Finally fs.Close() End Try End Sub The code in LISTING B receives the result from the Web service and writes the results to a file called testout.txt. Theoretically, the content of the test.txt file read by the Web service should always match the content within the testout.txt file, but this isn't necessarily true. A change in the test.txt file, taking place before the expiration of the 60-second cache interval, will not be reflected in the testout.txt file. If you wait 60 seconds and rerun the Windows Application client again, you'll notice that the testout.txt file contains the content of the modified test.txt file. DANGERS Although the CacheDuration property can greatly increase performance in specific scenarios, it's very important not to blindly enable caching for all your Web methods, as it can result in adverse, unexpected results. In your engineering and architecting phase, carefully evaluate the estimated request throughput and parameter sets for each Web method in order to make a logical decision about whether to use response caching in your Web services. Kevin Koch is a senior software engineer with extensive experience in both Microsoft's .NET platform and J2EE technologies. He is a cofounder and president of Task Solutions Inc. ----------------------------------------