Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 373993
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T14:21:43+00:00 2026-05-12T14:21:43+00:00

I am in the process of designing a feature for a system where I

  • 0

I am in the process of designing a feature for a system where I strongly feel that there must be a pattern for this out there, that I should know about before diving into the code.

The scenario is this:

  • I have a pool of resources of which I have a limited number.
  • I have a variable number of consumers that use those resources; each consumer needs exactly one resource, and it may not use the same resource as any other consumer at a given time.
  • The consumers are divided into a fixed number of groups, and the system needs to guarantee that there is a minimum of one resource for each group.
  • The number of consumers in each group varies over time; they are allocated and deallocated as needed.

My current approach is to put the resources into two stacks at startup: one “emergency stack” and one “common stack”. The emergency stack will contain the same number of resources as there are groups (so, one for each group). The rest of the available resources go into the common stack.

When a new consumer is about to be created, the system will request a resource. If there are resources available in the common stack, one will be popped from it and returned to the caller. If the common stack is empty, a resource may be popped from the emergency stack instead, but only if there are no consumers within the same group that already has an emergency resource.

Whenever a consumer within a group can be deallocated, the associated resource will be returned, and pushed onto one of the resource stacks. The code responsible for deallocating consumers will make sure to always return any emergency resources first, so that the emergency stack is filled, before returned resources are being pushed onto the common stack.

I feel that this is the sort of problem for which there ought to exist a design pattern that has been tested and proven to work well, so I call out to the community: do you know of such a pattern? If so, I kindly ask you to enlighten me.

Update

The solution is now implemented, using bits and pieces from various answers to this question. I published a blog post about it.

  • 1 1 Answer
  • 3 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T14:21:44+00:00Added an answer on May 12, 2026 at 2:21 pm

    I’d probably have a single resource stack, an array/map keyed by group of counts, and a count of groups that have no resources.

    To allocate…

    if group-counts [group] == 0
      pop resource from stack
      increment group-counts [group]
      decrement reserved-count
    
    elseif reserved-count < stack.size
      pop resource from stack
      increment group-counts [group]
    
    else
      fail
    

    The key point is that the stack is never allowed to get smaller than the number of groups that still have the right to demand an immediate resource.

    One advantage to this approach is that you can make it a little more flexible if needed. Suppose one group has a special requirement, so it may need two resources at any point.

    if group-counts [group] < group-reserved [group]
      pop resource from stack
      increment group-counts [group]
      decrement reserved-count
    
    elseif reserved-count < stack.size
      pop resource from stack
      increment group-counts [group]
    
    else
      fail
    

    The reserved-count in this case starts as the sum of all group-reserved[] values.

    The release logic for this case is…

    push resource to stack
    decrement group-counts [group]
    if group-counts [group] < group-reserved [group]
      increment reserved-count
    

    For the simple case, use “if group-counts [group] == 0”.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am in process of designing my CMS that I am about to create.
I'm in the process of designing a system that will allow me to represent
In the process of designing an application we've come to the conclusion that the
I'm in a process of designing a multi-tier ASP.NET web application that is supposed
I'm designing a new web application and I'm aware that the process of registration
I'm in the process of designing a Java GUI driven application that runs a
I am in the process of designing a system where i need to keep
We are in the process of designing/creating restful web services that will be consumed
I'm designing a python program that'll talk to two other process at the same
I'm designing a web based php reporting system. It involves multiple pages that insert

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.