top of page
Writer's pictureThe Tech Platform

Couchbase Implementation With Net 5.0

Joker Series: It is a microservices project that I have learned and tried to implement the best approaches. I have decided to write the approaches with examples. Each microservices have own responsible domains. All services are running containers.

Mainly, Joker platform is location based campaign marketplace. Merchants publish own campaigns. User look for campaigns and like campaigns, follow any merchants to get notifications about published campaign.


Couchbase is an award-winning distributed NoSQL cloud database. It delivers unmatched versatility, performance, scalability, and financial value across cloud, on-premises, hybrid, distributed cloud, and edge computing deployments.

Of course I won’t deep dive into Couchbase, I’m not an expert in Couchbase. I’ll give some details.

  • It’s document based NoSQL database.

  • It supports sharding and replication.

  • It’s possible that query documents using N1QL (Nikel). N1QL is closely SQL syntax.

  • Documents are stored JSON or Binary data types.

  • It provide us to key value store, caching, full text search, map reduce views.

  • It provides us SDK for some programming languages such as .NET, Go, Java, Kotlin, Node, Php, Pyhton, Ruby, Scala.

In new versions, There are four organizational layers which are Bucket, Scope, Collection, Document. It allows us to group data hierarchically.

Let’s talk a little about indexes in Couchbase. There are some kind of index types which are Primary, Secondory, Full Text.

  • Primary Index, is based on unique key of documents. It’s used simple queries without any filter.

  • Secondory Index, can be scalar, object or array. It’s used quires with N1QL syntax.

  • Full Text Index, is used searching text in documents.


So!! After the details, let’s go to implement Couchbase to project. :)

I’ve implemented it to Favorite Service, User’s favorite campaign and favorite stores are stored in the service.

Before the implementation, let’s install Couchbase Server. Couchbase will run on container. Firstly, I’ve added the service to docker compose file with simple configuration. It will expose default ports.

docker-compose.insfrastructure.yml


Let’s configure couchbase server lauching Couchbase UI after the container is up.


I’ve specified cluster’s name and password. After that, I’ve accepted the terms and conditions. Depends on my local machine, I’ve written some values about quotas.

After installation, let’s code in project, implement to it. I’ve added it to application life cycle with connection string, username and password.



When the service start to up; Bucket, Scope, Collection, some indexes will be created automatically.

I’ve created the repository class which has simple methods such as insert document and get documents by filter.

The documents are stored in JSON data type. But It can be stored MessagePack creating custom transcoder for perfomance in future.

For the large documents, Couchbase provide us to get documents using streams. For now, I’ve implemented with simply way.

public class FavoriteCampaignRepository : IFavoriteCampaignRepository    
{
    private readonly IBucketProvider_bucketProvider;
    private readonly string _bucketName;
    
    public FavoriteCampaignRepository
            (IBucketProviderbucketProvider, IConfigurationconfiguration)        
    {
        _bucketProvider=bucketProvider;
        _bucketName=configuration.GetValue<string>
                         ("Couchbase:BucketName");        
    }
    
    public async TaskAddFavoriteCampaignAsync(FavoriteCampaigncampaign)        
    {
        var bucket = await _bucketProvider.GetBucketAsync(_bucketName);
        
        var collection = await bucket.CollectionAsync("campaign");
        
        await collection.InsertAsync(Guid.NewGuid().ToString(), campaign);        
     }
     
     public async Task<List<FavoriteCampaign>> 
                        GetCampaignsByCampaignIdAsync(string campaignId)        
     {
         var bucket = await _bucketProvider.GetBucketAsync(_bucketName);
         
         var scope = await bucket.DefaultScopeAsync();
         
         var favoriteCampaigns = await scope.QueryAsync<FavoriteCampaign>
         ($"SELECT c.* FROM  favorite._default.campaign c "+$"WHERE 
         c.campaign.id =\"{campaignId}\"");
         return await favoriteCampaigns.Rows.ToListAsync();        
     }    
 }

After I’ve inserted some documents, Documents have shown in Couchbase such as below.


Source: Medium - Mesut Atasoy


The Tech Platform

0 comments

Comments


bottom of page