Skip to main content

Ted Neward is using the actor model with Akka.

Show Notes:

Ted Neward is on Twitter.

Want to be on the next episode? You can! All you need is the willingness to talk about something technical.

Theme music is "Crosscutting Concerns" by The Dirty Truckers, check out their music on Amazon or iTunes.

This is a repost that originally appeared on the Couchbase Blog: Azure Functions and Lazy Initialization with Couchbase Server.

Azure Functions are still new to me, and I’m learning as I’m going. I blogged about my foray into Azure Functions with Couchbase over a month ago. Right after I posted that, I got some helpful feedback about the way I was instantiating a Couchbase cluster (and bucket).

I had (wrongly) assumed that there was no way to save state between Azure Function calls. This is why I created a GetCluster() method that was called each time the function ran. But, initializing a Couchbase Cluster object is an expensive operation. The less often you instantiate it, the better.

You can follow along with the updated source code for this blog post on Github.

Static state

I had a hard time finding documentation on whether I could use a static object for reuse between function calls. I suppose I should have experimented, like fellow Microsoft MVP Mark Heath did. Instead, I posed the question to StackOverflow.

In short: yes. A Cluster, instantiated and saved to a static member, can is reusable between function calls. According to Mark’s post above, there’s no guarantee how long this value will survive. But that’s an expected trade-off that you make when going "serverless".

Lazy initializing within Azure Functions

Simply using a static member would work, but it’s not thread-safe. There are a few ways to tackle that issue, but an easy way that’s built right into the .NET framework is to use Lazy Initialization with Lazy<T>.

Lazy Initialization in Azure Functions

First, I removed the GetBucket and GetCluster methods. Next, I created a Lazy<IBucket> property to replace them.

private static readonly Lazy<IBucket> Bucket = new Lazy<IBucket>(() =>
{
    var uri = ConfigurationManager.AppSettings["couchbaseUri"];
    var bucketName = ConfigurationManager.AppSettings["couchbaseBucketName"];
    var bucketPassword = ConfigurationManager.AppSettings["couchbaseBucketPassword"];
    var cluster = new Cluster(new ClientConfiguration
    {
        Servers = new List<Uri> { new Uri(uri) }
    });
    return cluster.OpenBucket(bucketName, bucketPassword);
});

I just made a single property for a bucket, since that’s all I need for this example. But if you need to use the cluster, you can easily make that its own Lazy property. (Once you have a cluster, getting a bucket is a relatively cheap operation).

Using a Lazy property

When you instantiate a Lazy<T> object, you supply it with an initialization lambda. That lambda won’t execute until the Value property is actually called for the first time.

var lazyObject = new Lazy<string>(() =>
{
    // this code won't be called until 'lazyObject.Value' is referenced
    // for the first time
    return "I'm lazy!";
});

For instance, notice the Value between Bucket and GetAsync in the updated version of my Azure Functions:

var doc = await Bucket.Value.GetAsync<MyDocument>(id);

If that’s the first time Value is used, the cluster will be initialized. Otherwise, it will use the already initialized cluster (try experimenting with a Guid instead of a Bucket).

Summary

State can be saved between Azure Function calls by using a static member. Make sure that it’s thread-safe (by using Lazy<T> or something like it). Don’t make any assumptions about how long that object will be around.

Anything else I missed? Are you using Azure Functions with Couchbase? I would love to hear from you. Please leave a comment below or ping me on Twitter @mgroves.

Eric Elliott is increasing code quality by leveraging pure functions and fast feedback.

Editors note: sorry for the audio quality; I had to use the Skype audio recording, and it's not as good as I'd like. But it's too good of an episode to just throw out!

Show Notes:

Eric Elliott is on Twitter. Special thanks to JS Cheerleader, who set up this interview!

Want to be on the next episode? You can! All you need is the willingness to talk about something technical.

Theme music is "Crosscutting Concerns" by The Dirty Truckers, check out their music on Amazon or iTunes.

Full month of podcasts with some AMAZING guests coming up in October. After that, I think I'll wrap for the year (with the exception of a special episode that I'm planning).

But, if you want to get on the show next year, please contact me!

Subscribe now!

Here's what's coming in October:

  • Eric Elliott on the benefits of TDD
  • Ted Neward on the actor model with Akka
  • Ted Neward (again) on (JavaScript) Transpilers, web assembly, and general alternatives to writing JS
  • Patrick Smacchia on NDepend, refactoring
  • Sam Nasr on the benefits of user group involvement

Subscribe now with your podcatcher of choice!

Want to be on the next episode? You can! All you need is the willingness to talk about something technical.

This is a repost that originally appeared on the Couchbase Blog: Powershell with the Couchbase REST API.

PowerShell is a scripting environment / command line that comes with Windows and is also available for Linux and within Azure.

Maybe you’ve used Postman or Fiddler to make HTTP requests. Those are great, but not necessarily the right tools for automation or scripting.

You may have heard of curl before. It’s a command line tool for making HTTP requests.

If you’re a .NET/Windows developer (like me), maybe you aren’t familiar with curl. I use PowerShell as my default command line every day (though I still consider myself a PowerShell neophyte). In this post, I’ll show you how you can use PowerShell’s Invoke-WebRequest to make HTTP requests (which you can use within PowerShell scripts for automation).

You can check out the PowerShell script I created on GitHub. Note: as of the time of writing this post, I’m using PowerShell 5.1 on Windows 10.

Couchbase REST API

Couchbase Server has a an extensive REST API that you can use to manage and administrate just about every aspect of Couchbase Server. For this blog post, I’m going to focus on the Full Text Search (FTS) API. I’m going to show this because:

  • Creating an FTS index is something you’ll eventually want to automate

  • You will probably want to share an FTS index you created with your team and/or check it into source control

  • Couchbase Console already shows you exactly how to do it with curl.

I’m not going to cover FTS in detail: I invite you to check out past blog posts on FTS, and this short video demonstrating full text search.

Full Text Search review

When you initially create an FTS index, you will probably use the built-in FTS UI in the Couchbase Console. This is fine when you are doing the initial development, but it’s not practical if you want to share this index with your team, automate deployment, or take advantage of source control.

Fortunately, you can use the "Show index definition JSON" feature to see the JSON data that makes up the index definition. You can also have Couchbase Console generate the curl method for you.

Generate FTS curl script

Well, if you’re using curl, that’s very convenient. Here’s an example:

curl -XPUT -H "Content-Type: application/json" http://localhost:8094/api/index/medical-condition -d '{ ... json payload ...}'

You can copy/paste that into a script, and check the script into source control. But what if you don’t use curl?

PowerShell version: Invoke-WebRequest

First, create a new PowerShell script. I called mine createFtsIndex.ps1. All this PowerShell script is going to do is create an FTS index on an existing bucket.

You can start by pasting the "curl" command into this file. The bulk of this command is the JSON definition, which will be exactly the same.

Let’s breakdown the rest of the curl command to see what’s happening:

  • -XPUT - This is telling curl to use the PUT verb with the HTTP request

  • -H "Content-Type: application/json" - Use a Content-Type header.

  • http://localhost:8094/api/index/medical-condition - This is the URL of the REST endpoint. The "localhost" will vary based on where Couchbase is running, and the "medical-condition" part is just the name of the FTS index.

  • -d '…​json payload…​' - The body of content that will be included in the HTTP request.

PowerShell’s Invoke-WebRequest can do all this stuff too, but the syntax is a bit different. Let’s step through the equivalents:

  • -Method PUT - This is telling Invoke-WebRequest to use the PUT verb with the HTTP request, so you can replace -XPUT

  • -Header @{ …​ } - Specify headers to use with the request (more on this later)

  • -Uri http://localhost:8094/api/index/medical-condition" - You just need to add "-Uri" in front

  • -Body '…​json payload…​' - The body of content is included this way instead of using curl’s -d

Headers

PowerShell expects a "dictionary" that contains headers. The syntax for a literal dictionary in PowerShell is:

@{"key1"="value1"; "key2"="value2"}

So then, to specify Content-Type:

-Headers @{"Content-Type"="application/json"}

One thing that the curl output did not generate is the authentication information that you need to make a request to the API. With curl, you can specify basic authentication by adding the username/password to the URL. It will then translate it into the appropriate Basic Auth headers.

With PowerShell, it appears you have to do that yourself. My local Couchbase Server has credentials "Administrator" and "password" (please don’t use those in production). Those need to be encoded into Base64 and added to the headers.

Then the full Headers dictionary looks like this:

-Headers @{"Authorization" = "Basic "+[System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes("Administrator:password")); "Content-Type"="application/json"}

You might think that’s a bit noisy, and I agree. If you know a cleaner way to do this, I’m dying to know. Please leave a comment.

Execute the PowerShell script

To execute the script, simply type .\createFtsIndex.ps1 at the PowerShell command line.

Execute PowerShell script

You’re now ready to make this a part of your deployment.

Summary

PowerShell is a handy tool for scripting and automation. It’s used by Azure, Octopus Deploy, and anywhere Windows is running. Use PowerShell to automate Couchbase REST API calls for things like Full Text Search indexes, and your team will thank you.

Need help? Reach out to me with questions by leaving a comment below or finding me on Twitter @mgroves.

Matthew D. Groves

About the Author

Matthew D. Groves lives in Central Ohio. He works remotely, loves to code, and is a Microsoft MVP.

Latest Comments

Twitter