C # based on Redis achieve Distributed Lock

    [This blog belongs to the original, For reprint, please indicate the source: gdoujkzz]

  Recent studies related stocks oversold and so often the case in peak periods, according to the final use of distributed lock is achieved based on Redis, hereby out and share.

  Preparations: centos7, Redis, Nginx, and JMeter test tools.

  Traditional single architecture

    In the traditional process, we wrote the following simple code inventory operation is as follows:

    The following is based on a AspNetCore.WebAPI create the inventory interface operation (reduction), I believe that many comrades are able to write such a plus lock to ensure high concurrency when the stock does not appear oversold, performance of this approach problems, is beyond the scope of our article, we want to discuss is the situation in the end such an approach will not result in oversold happen? If this is the traditional enterprise application, the monomer structure, as shown below, can meet the demand;   

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using StackExchange.Redis;

namespace WebApplication1.Controllers
{
    [Route("api/inv")]
    [ApiController]
    public  class InvController: ControllerBase
    {

        private static readonly object LockObject = new object();
        private static readonly ConfigurationOptions Options = new ConfigurationOptions()
        {
            EndPoints = { { "192.168.232.132", 6379 } },
            Password = "123456"
        };


        [HttpGet]
        public ActionResult<string> Get()
        {
            string msg = null;
            lock (LockObject)
            {
                int invQty = GetInvQty();
                if (invQty > 0)
                {
                    invQty = invQty - 1;
                    SetInvQty(invQty);
                    MSG = $ " deduction succeeds, the current inventory: invQty {} " ;
                }
                else
                {
                    msg = " deduction fails, inventory shortage " ;
                }
            }
            Console.WriteLine(msg);
            return msg;
        }


        private int GetInvQty()
        {
            var qty = 0;
            using (var conn = ConnectionMultiplexer.Connect(Options))
            {
                var db = conn.GetDatabase();
                qty = Convert.ToInt32(db.StringGet("InvQty"));
            }
            return qty;
        }

        private void SetInvQty(int qty)
        {
            using (var conn = ConnectionMultiplexer.Connect(Options))
            {
                var db = conn.GetDatabase();
                db.StringSet("InvQty", qty);
            }
        }
    }
}

      With more and more complex business, this form of monomer architecture, has failed to meet the needs of our normal business, many companies evolved into following this architecture model;

   Distributed Architecture

    

    

  

Guess you like

Origin www.cnblogs.com/gdouzz/p/12097968.html